Skip to main content

提示模板

PromptTemplate 允许您使用模板生成提示。当您想在多个地方使用相同的提示概要,但更改某些值时,这非常有用。 如下所示,PromptTemplate对LLM和聊天模型都有支持:

import {
ChatPromptTemplate,
HumanMessagePromptTemplate,
PromptTemplate,
SystemMessagePromptTemplate,
} from "langchain/prompts";

export const run = async () => {
// A `PromptTemplate` consists of a template string and a list of input variables.
const template = "What is a good name for a company that makes {product}?";
const promptA = new PromptTemplate({ template, inputVariables: ["product"] });

// We can use the `format` method to format the template with the given input values.
const responseA = await promptA.format({ product: "colorful socks" });
console.log({ responseA });
/*
{
responseA: 'What is a good name for a company that makes colorful socks?'
}
*/

// We can also use the `fromTemplate` method to create a `PromptTemplate` object.
const promptB = PromptTemplate.fromTemplate(
"What is a good name for a company that makes {product}?"
);
const responseB = await promptB.format({ product: "colorful socks" });
console.log({ responseB });
/*
{
responseB: 'What is a good name for a company that makes colorful socks?'
}
*/

// For chat models, we provide a `ChatPromptTemplate` class that can be used to format chat prompts.
const chatPrompt = ChatPromptTemplate.fromPromptMessages([
SystemMessagePromptTemplate.fromTemplate(
"You are a helpful assistant that translates {input_language} to {output_language}."
),
HumanMessagePromptTemplate.fromTemplate("{text}"),
]);

// The result can be formatted as a string using the `format` method.
const responseC = await chatPrompt.format({
input_language: "English",
output_language: "French",
text: "I love programming.",
});
console.log({ responseC });
/*
{
responseC: '[{"text":"You are a helpful assistant that translates English to French."},{"text":"I love programming."}]'
}
*/

// The result can also be formatted as a list of `ChatMessage` objects by returning a `PromptValue` object and calling the `toChatMessages` method.
// More on this below.
const responseD = await chatPrompt.formatPromptValue({
input_language: "English",
output_language: "French",
text: "I love programming.",
});
const messages = responseD.toChatMessages();
console.log({ messages });
/*
{
messages: [
SystemChatMessage {
text: 'You are a helpful assistant that translates English to French.'
},
HumanChatMessage { text: 'I love programming.' }
]
}
*/
};

深入了解