Skip to main content

Class: PromptTemplate

prompts.PromptTemplate

Schema to represent a basic prompt for an LLM.

Example

import { PromptTemplate } from "langchain/prompts";

const prompt = new PromptTemplate({
inputVariables: ["foo"],
template: "Say {foo}",
});

Hierarchy​

Implements​

Constructors​

constructor​

β€’ new PromptTemplate(input)

Parameters​

NameType
inputPromptTemplateInput

Overrides​

BaseStringPromptTemplate.constructor

Defined in​

langchain/src/prompts/prompt.ts:61

Properties​

inputVariables​

β€’ inputVariables: string[]

A list of variable names the prompt template expects

Implementation of​

PromptTemplateInput.inputVariables

Inherited from​

BaseStringPromptTemplate.inputVariables

Defined in​

langchain/src/prompts/base.ts:51


outputParser​

β€’ Optional outputParser: BaseOutputParser

How to parse the output of calling an LLM on this formatted prompt

Implementation of​

PromptTemplateInput.outputParser

Inherited from​

BaseStringPromptTemplate.outputParser

Defined in​

langchain/src/prompts/base.ts:53


partialVariables​

β€’ Optional partialVariables: InputValues

Partial variables

Implementation of​

PromptTemplateInput.partialVariables

Inherited from​

BaseStringPromptTemplate.partialVariables

Defined in​

langchain/src/prompts/base.ts:55


template​

β€’ template: string

The propmt template

Implementation of​

PromptTemplateInput.template

Defined in​

langchain/src/prompts/prompt.ts:55


templateFormat​

β€’ templateFormat: TemplateFormat = "f-string"

The format of the prompt template. Options are 'f-string', 'jinja-2'

Default Value

'f-string'

Implementation of​

PromptTemplateInput.templateFormat

Defined in​

langchain/src/prompts/prompt.ts:57


validateTemplate​

β€’ validateTemplate: boolean = true

Whether or not to try validating the template on initialization

Default Value

true

Implementation of​

PromptTemplateInput.validateTemplate

Defined in​

langchain/src/prompts/prompt.ts:59

Methods​

_getPromptType​

β–Έ _getPromptType(): "prompt"

Return the string type key uniquely identifying this class of prompt template.

Returns​

"prompt"

Overrides​

BaseStringPromptTemplate._getPromptType

Defined in​

langchain/src/prompts/prompt.ts:80


format​

β–Έ format(values): Promise<string>

Format the prompt given the input values.

Example

prompt.format({ foo: "bar" });

Parameters​

NameTypeDescription
valuesInputValuesA dictionary of arguments to be passed to the prompt template.

Returns​

Promise<string>

A formatted prompt string.

Overrides​

BaseStringPromptTemplate.format

Defined in​

langchain/src/prompts/prompt.ts:84


formatPromptValue​

β–Έ formatPromptValue(values): Promise<BasePromptValue>

Format the prompt given the input values and return a formatted prompt value.

Parameters​

NameType
valuesInputValues

Returns​

Promise<BasePromptValue>

A formatted PromptValue.

Inherited from​

BaseStringPromptTemplate.formatPromptValue

Defined in​

langchain/src/prompts/base.ts:152


mergePartialAndUserVariables​

β–Έ mergePartialAndUserVariables(userVariables): Promise<InputValues>

Parameters​

NameType
userVariablesInputValues

Returns​

Promise<InputValues>

Inherited from​

BaseStringPromptTemplate.mergePartialAndUserVariables

Defined in​

langchain/src/prompts/base.ts:69


partial​

β–Έ partial(values): Promise<PromptTemplate>

Parameters​

NameType
valuesPartialValues

Returns​

Promise<PromptTemplate>

Overrides​

BaseStringPromptTemplate.partial

Defined in​

langchain/src/prompts/prompt.ts:141


serialize​

β–Έ serialize(): SerializedPromptTemplate

Return a json-like object representing this prompt template.

Returns​

SerializedPromptTemplate

Overrides​

BaseStringPromptTemplate.serialize

Defined in​

langchain/src/prompts/prompt.ts:153


deserialize​

β–Έ Static deserialize(data): Promise<PromptTemplate>

Load a prompt template from a json-like object describing it.

Remarks

Deserializing needs to be async because templates (e.g. FewShotPromptTemplate) can reference remote resources that we read asynchronously with a web request.

Parameters​

NameType
dataSerializedPromptTemplate

Returns​

Promise<PromptTemplate>

Overrides​

BaseStringPromptTemplate.deserialize

Defined in​

langchain/src/prompts/prompt.ts:167


fromExamples​

β–Έ Static fromExamples(examples, suffix, inputVariables, exampleSeparator?, prefix?): PromptTemplate

Take examples in list format with prefix and suffix to create a prompt.

Intendend to be used a a way to dynamically create a prompt from examples.

Parameters​

NameTypeDefault valueDescription
examplesstring[]undefinedList of examples to use in the prompt.
suffixstringundefinedString to go after the list of examples. Should generally set up the user's input.
inputVariablesstring[]undefinedA list of variable names the final prompt template will expect
exampleSeparatorstring"\n\n"The separator to use in between examples
prefixstring""String that should go before any examples. Generally includes examples.

Returns​

PromptTemplate

The final prompt template generated.

Defined in​

langchain/src/prompts/prompt.ts:102


fromTemplate​

β–Έ Static fromTemplate(template, Β«destructuredΒ»?): PromptTemplate

Load prompt template from a template f-string

Parameters​

NameType
templatestring
Β«destructuredΒ»Omit<PromptTemplateInput, "template" | "inputVariables">

Returns​

PromptTemplate

Defined in​

langchain/src/prompts/prompt.ts:119