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β
β³
PromptTemplate
Implementsβ
Constructorsβ
constructorβ
β’ new PromptTemplate(input
)
Parametersβ
Name | Type |
---|---|
input | PromptTemplateInput |
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β
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β
Name | Type | Description |
---|---|---|
values | InputValues | A 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β
Name | Type |
---|---|
values | InputValues |
Returnsβ
Promise
<BasePromptValue
>
A formatted PromptValue.
Inherited fromβ
BaseStringPromptTemplate.formatPromptValue
Defined inβ
langchain/src/prompts/base.ts:152
mergePartialAndUserVariablesβ
βΈ mergePartialAndUserVariables(userVariables
): Promise
<InputValues
>
Parametersβ
Name | Type |
---|---|
userVariables | InputValues |
Returnsβ
Promise
<InputValues
>
Inherited fromβ
BaseStringPromptTemplate.mergePartialAndUserVariables
Defined inβ
langchain/src/prompts/base.ts:69
partialβ
βΈ partial(values
): Promise
<PromptTemplate
>
Parametersβ
Name | Type |
---|---|
values | PartialValues |
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β
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β
Name | Type |
---|---|
data | SerializedPromptTemplate |
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β
Name | Type | Default value | Description |
---|---|---|---|
examples | string [] | undefined | List of examples to use in the prompt. |
suffix | string | undefined | String to go after the list of examples. Should generally set up the user's input. |
inputVariables | string [] | undefined | A list of variable names the final prompt template will expect |
exampleSeparator | string | "\n\n" | The separator to use in between examples |
prefix | string | "" | String that should go before any examples. Generally includes examples. |
Returnsβ
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β
Name | Type |
---|---|
template | string |
Β«destructuredΒ» | Omit <PromptTemplateInput , "template" | "inputVariables" > |
Returnsβ
Defined inβ
langchain/src/prompts/prompt.ts:119