langchain-hs-0.0.1.0: Haskell implementation of Langchain
Copyright(c) 2025 Tushar Adhatrao
LicenseMIT
MaintainerTushar Adhatrao <tusharadhatrao@gmail.com>
Stabilityexperimental
Safe HaskellSafe-Inferred
LanguageHaskell2010

Langchain.PromptTemplate

Description

This module provides types and functions for working with prompt templates in Langchain. Prompt templates are used to structure inputs for language models, allowing for dynamic insertion of variables into predefined text formats. They are essential for creating flexible and reusable prompts that can be customized based on input data.

The main types are:

  • PromptTemplate: A simple template with placeholders for variables.
  • FewShotPromptTemplate: A template that includes few-shot examples for better context, useful in scenarios like few-shot learning.

These types are designed to be compatible with the Langchain Python library's prompt template functionality: Langchain PromptTemplate.

Examples

See the documentation for renderPrompt and renderFewShotPrompt for usage examples.

Synopsis

Core Types

newtype PromptTemplate Source #

Represents a prompt template with a template string. The template string can contain placeholders of the form {key}, where key is a sequence of alphanumeric characters and underscores.

Constructors

PromptTemplate 

Fields

data FewShotPromptTemplate Source #

Represents a few-shot prompt template with examples. This type allows for creating prompts that include example inputs and outputs, which can be useful for few-shot learning scenarios.

Constructors

FewShotPromptTemplate 

Fields

Rendering Functions

renderPrompt :: PromptTemplate -> Map Text Text -> Either String Text Source #

Render a prompt template with the given variables. Returns either an error message if a variable is missing or the rendered template.

Using renderPrompt

To render a prompt template with variables:

let template = PromptTemplate "Hello, {name}! Welcome to {place}."
vars = HM.fromList [("name", Alice), ("place", Wonderland)]
result <- renderPrompt template vars
-- Result: Right "Hello, Alice! Welcome to Wonderland."

If a variable is missing:

let vars = HM.fromList [("name", Alice)]
result <- renderPrompt template vars
-- Result: Left "Missing variable: place"

renderFewShotPrompt :: FewShotPromptTemplate -> Either String Text Source #

Render a few-shot prompt template with the given input variables. Returns either an error message if interpolation fails or the fully rendered prompt.

Using renderFewShotPrompt

To render a few-shot prompt template:

let fewShotTemplate = FewShotPromptTemplate
      { fsPrefix = "Examples of {type}:n"
      , fsExamples =
          [ HM.fromList [("input", Hello), ("output", Bonjour)]
          , HM.fromList [("input", Goodbye), ("output", "Au revoir")]
          ]
      , fsExampleTemplate = "Input: {input}nOutput: {output}n"
      , fsExampleSeparator = "n"
      , fsSuffix = "Now translate: {query}"
      }
result <- renderFewShotPrompt fewShotTemplate
-- Result: Right "Examples of {type}:nInput: HellonOutput: BonjournnInput: GoodbyenOutput: Au revoirnNow translate: {query}"