少量样本提示模板
少量样本提示是一种提示技巧,它为大型语言模型 (LLM) 提供一个示例列表,然后要求 LLM 生成一些文本,以遵循所提供示例的引导。
以下是一个示例
假设您希望您的 LLM 以特定格式响应。您可以使用少量样本提示 LLM 一系列问答对,以便它知道要响应的格式。
Respond to the users question in the with the following format:
Question: What is your name?
Answer: My name is John.
Question: What is your age?
Answer: I am 25 years old.
Question: What is your favorite color?
Answer:
这里我们留下了最后一个 Answer:
未定义,以便 LLM 可以填写它。然后,LLM 将生成以下内容
Answer: I don't have a favorite color; I don't have preferences.
用例
在下面的示例中,我们使用少量样本提示 LLM 将问题改写为更通用的查询。
我们提供了两组带有特定问题的示例,以及改写后的通用问题。FewShotChatMessagePromptTemplate
将使用我们的示例,并且当调用 .format
时,我们会看到这些示例被格式化为一个字符串,我们可以将其传递给 LLM。
import {
ChatPromptTemplate,
FewShotChatMessagePromptTemplate,
} from "langchain/prompts";
const examples = [
{
input: "Could the members of The Police perform lawful arrests?",
output: "what can the members of The Police do?",
},
{
input: "Jan Sindel's was born in what country?",
output: "what is Jan Sindel's personal history?",
},
];
const examplePrompt = ChatPromptTemplate.fromTemplate(`Human: {input}
AI: {output}`);
const fewShotPrompt = new FewShotChatMessagePromptTemplate({
examplePrompt,
examples,
inputVariables: [], // no input variables
});
const formattedPrompt = await fewShotPrompt.format({});
console.log(formattedPrompt);
[
HumanMessage {
lc_namespace: [ 'langchain', 'schema' ],
content: 'Human: Could the members of The Police perform lawful arrests?\n' +
'AI: what can the members of The Police do?',
additional_kwargs: {}
},
HumanMessage {
lc_namespace: [ 'langchain', 'schema' ],
content: "Human: Jan Sindel's was born in what country?\n" +
"AI: what is Jan Sindel's personal history?",
additional_kwargs: {}
}
]
然后,如果我们将其与另一个问题一起使用,LLM 将根据我们的意愿改写问题。
- npm
- Yarn
- pnpm
npm install @langchain/openai
yarn add @langchain/openai
pnpm add @langchain/openai
import { ChatOpenAI } from "@langchain/openai";
const model = new ChatOpenAI({});
const examples = [
{
input: "Could the members of The Police perform lawful arrests?",
output: "what can the members of The Police do?",
},
{
input: "Jan Sindel's was born in what country?",
output: "what is Jan Sindel's personal history?",
},
];
const examplePrompt = ChatPromptTemplate.fromTemplate(`Human: {input}
AI: {output}`);
const fewShotPrompt = new FewShotChatMessagePromptTemplate({
prefix:
"Rephrase the users query to be more general, using the following examples",
suffix: "Human: {input}",
examplePrompt,
examples,
inputVariables: ["input"],
});
const formattedPrompt = await fewShotPrompt.format({
input: "What's France's main city?",
});
const response = await model.invoke(formattedPrompt);
console.log(response);
AIMessage {
lc_namespace: [ 'langchain', 'schema' ],
content: 'What is the capital of France?',
additional_kwargs: { function_call: undefined }
}
使用函数进行少量样本提示
您也可以使用函数进行部分应用。这样做的用例是当您有一个变量时,您知道总是希望以通用方式获取它。一个典型的例子是日期或时间。假设您有一个提示,您总是希望它包含当前日期。您不能将它硬编码在提示中,并且将它与其他输入变量一起传递可能很繁琐。在这种情况下,能够使用一个始终返回当前日期的函数对提示进行部分应用非常方便。
const getCurrentDate = () => {
return new Date().toISOString();
};
const prompt = new FewShotChatMessagePromptTemplate({
template: "Tell me a {adjective} joke about the day {date}",
inputVariables: ["adjective", "date"],
});
const partialPrompt = await prompt.partial({
date: getCurrentDate,
});
const formattedPrompt = await partialPrompt.format({
adjective: "funny",
});
console.log(formattedPrompt);
// Tell me a funny joke about the day 2023-07-13T00:54:59.287Z
少量样本与聊天少量样本
聊天和非聊天少量样本提示模板以类似的方式工作。以下示例将演示使用聊天和非聊天,以及它们输出的差异。
import {
FewShotPromptTemplate,
FewShotChatMessagePromptTemplate,
} from "langchain/prompts";
const examples = [
{
input: "Could the members of The Police perform lawful arrests?",
output: "what can the members of The Police do?",
},
{
input: "Jan Sindel's was born in what country?",
output: "what is Jan Sindel's personal history?",
},
];
const prompt = `Human: {input}
AI: {output}`;
const examplePromptTemplate = PromptTemplate.fromTemplate(prompt);
const exampleChatPromptTemplate = ChatPromptTemplate.fromTemplate(prompt);
const chatFewShotPrompt = new FewShotChatMessagePromptTemplate({
examplePrompt: exampleChatPromptTemplate,
examples,
inputVariables: [], // no input variables
});
const fewShotPrompt = new FewShotPromptTemplate({
examplePrompt: examplePromptTemplate,
examples,
inputVariables: [], // no input variables
});
console.log("Chat Few Shot: ", await chatFewShotPrompt.formatMessages({}));
/**
Chat Few Shot: [
HumanMessage {
lc_namespace: [ 'langchain', 'schema' ],
content: 'Human: Could the members of The Police perform lawful arrests?\n' +
'AI: what can the members of The Police do?',
additional_kwargs: {}
},
HumanMessage {
lc_namespace: [ 'langchain', 'schema' ],
content: "Human: Jan Sindel's was born in what country?\n" +
"AI: what is Jan Sindel's personal history?",
additional_kwargs: {}
}
]
*/
console.log("Few Shot: ", await fewShotPrompt.formatPromptValue({}));
/**
Few Shot:
Human: Could the members of The Police perform lawful arrests?
AI: what can the members of The Police do?
Human: Jan Sindel's was born in what country?
AI: what is Jan Sindel's personal history?
*/
在这里我们可以看到 FewShotChatMessagePromptTemplate
和 FewShotPromptTemplate
之间的主要区别:输入和输出值。
FewShotChatMessagePromptTemplate
通过获取一系列 ChatPromptTemplate
作为示例来工作,并且它的输出是 BaseMessage
实例列表。
另一方面,FewShotPromptTemplate
通过获取 PromptTemplate
作为示例来工作,并且它的输出是一个字符串。
非聊天模型
LangChain 还提供了一个用于非聊天模型的少样本提示格式化类:FewShotPromptTemplate
。API 大致相同,但输出格式不同(聊天消息与字符串)。
带有函数的部分
import {
ChatPromptTemplate,
FewShotChatMessagePromptTemplate,
} from "langchain/prompts";
const examplePrompt = PromptTemplate.fromTemplate("{foo}{bar}");
const prompt = new FewShotPromptTemplate({
prefix: "{foo}{bar}",
examplePrompt,
inputVariables: ["foo", "bar"],
});
const partialPrompt = await prompt.partial({
foo: () => Promise.resolve("boo"),
});
const formatted = await partialPrompt.format({ bar: "baz" });
console.log(formatted);
boobaz\n
带有函数和示例选择器
import {
ChatPromptTemplate,
FewShotChatMessagePromptTemplate,
} from "langchain/prompts";
const examplePrompt = PromptTemplate.fromTemplate("An example about {x}");
const exampleSelector = await LengthBasedExampleSelector.fromExamples(
[{ x: "foo" }, { x: "bar" }],
{ examplePrompt, maxLength: 200 }
);
const prompt = new FewShotPromptTemplate({
prefix: "{foo}{bar}",
exampleSelector,
examplePrompt,
inputVariables: ["foo", "bar"],
});
const partialPrompt = await prompt.partial({
foo: () => Promise.resolve("boo"),
});
const formatted = await partialPrompt.format({ bar: "baz" });
console.log(formatted);
boobaz
An example about foo
An example about bar