提示模板
¥Prompt Templates
提示模板有助于将用户输入和参数转换为语言模型的指令。这可用于指导模型的响应,帮助其理解上下文并生成相关且连贯的基于语言的输出。
¥Prompt templates help to translate user input and parameters into instructions for a language model. This can be used to guide a model's response, helping it understand the context and generate relevant and coherent language-based output.
提示模板以对象作为输入,其中每个键代表提示模板中需要填写的变量。
¥Prompt Templates take as input an object, where each key represents a variable in the prompt template to fill in.
提示模板输出一个 PromptValue。此 PromptValue 可以传递给 LLM 或 ChatModel,也可以转换为字符串或消息列表。此 PromptValue 存在的原因是为了方便在字符串和消息之间切换。
¥Prompt Templates output a PromptValue. This PromptValue can be passed to an LLM or a ChatModel, and can also be cast to a string or a list of messages. The reason this PromptValue exists is to make it easy to switch between strings and messages.
有几种不同类型的提示模板:
¥There are a few different types of prompt templates:
字符串提示模板
¥String PromptTemplates
这些提示模板用于格式化单个字符串,通常用于更简单的输入。例如,构建和使用 PromptTemplate 的常用方法如下:
¥These prompt templates are used to format a single string, and generally are used for simpler inputs. For example, a common way to construct and use a PromptTemplate is as follows:
import { PromptTemplate } from "@langchain/core/prompts";
const promptTemplate = PromptTemplate.fromTemplate(
"Tell me a joke about {topic}"
);
await promptTemplate.invoke({ topic: "cats" });
StringPromptValue {
value: 'Tell me a joke about cats'
}
ChatPromptTemplates
这些提示模板用于格式化消息列表。这些 "templates" 本身由一个模板列表组成。例如,构建和使用 ChatPromptTemplate 的常用方法如下:
¥These prompt templates are used to format a list of messages. These "templates" consist of a list of templates themselves. For example, a common way to construct and use a ChatPromptTemplate is as follows:
import { ChatPromptTemplate } from "@langchain/core/prompts";
const promptTemplate = ChatPromptTemplate.fromMessages([
["system", "You are a helpful assistant"],
["user", "Tell me a joke about {topic}"],
]);
await promptTemplate.invoke({ topic: "cats" });
ChatPromptValue {
messages: [
SystemMessage {
"content": "You are a helpful assistant",
"additional_kwargs": {},
"response_metadata": {}
},
HumanMessage {
"content": "Tell me a joke about cats",
"additional_kwargs": {},
"response_metadata": {}
}
]
}
在上面的示例中,此 ChatPromptTemplate 在调用时将构造两条消息。第一步是系统消息,它没有需要格式化的变量。第二个参数是 HumanMessage,将由用户传入的 topic
变量格式化。
¥In the above example, this ChatPromptTemplate will construct two messages when called.
The first is a system message, that has no variables to format.
The second is a HumanMessage, and will be formatted by the topic
variable the user passes in.
MessagesPlaceholder
此提示模板负责在特定位置添加消息列表。在上面的 ChatPromptTemplate 中,我们看到了如何格式化两条消息,每条消息都是一个字符串。但是,如果我们希望用户传入一个消息列表,我们会将其插入到特定位置,该怎么办?这就是使用 MessagesPlaceholder 的方式。
¥This prompt template is responsible for adding a list of messages in a particular place. In the above ChatPromptTemplate, we saw how we could format two messages, each one a string. But what if we wanted the user to pass in a list of messages that we would slot into a particular spot? This is how you use MessagesPlaceholder.
import {
ChatPromptTemplate,
MessagesPlaceholder,
} from "@langchain/core/prompts";
import { HumanMessage } from "@langchain/core/messages";
const promptTemplate = ChatPromptTemplate.fromMessages([
["system", "You are a helpful assistant"],
new MessagesPlaceholder("msgs"),
]);
await promptTemplate.invoke({ msgs: [new HumanMessage("hi!")] });
ChatPromptValue {
messages: [
SystemMessage {
"content": "You are a helpful assistant",
"additional_kwargs": {},
"response_metadata": {}
},
HumanMessage {
"content": "hi!",
"additional_kwargs": {},
"response_metadata": {}
}
]
}
这将生成一个包含两条消息的列表,第一条是系统消息,第二条是我们传入的 HumanMessage。如果我们传入了 5 条消息,那么总共会产生 6 条消息(系统消息加上传入的 5 条消息)。这对于将消息列表插入特定位置很有用。
¥This will produce a list of two messages, the first one being a system message, and the second one being the HumanMessage we passed in. If we had passed in 5 messages, then it would have produced 6 messages in total (the system message plus the 5 passed in). This is useful for letting a list of messages be slotted into a particular spot.
另一种无需显式使用 MessagesPlaceholder
类即可完成相同操作的方法是:
¥An alternative way to accomplish the same thing without using the MessagesPlaceholder
class explicitly is:
const promptTemplate = ChatPromptTemplate.fromMessages([
["system", "You are a helpful assistant"],
["placeholder", "{msgs}"], // <-- This is the changed part
]);
有关如何使用输出解析器的详细信息,请参阅 相关操作指南。
¥For specifics on how to use prompt templates, see the relevant how-to guides here.