少样本提示模板
少样本提示是一种提示技术,它为大型语言模型 (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:
这里我们将最后一个 答案:
留空,以便 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 @langchain/core
yarn add @langchain/openai @langchain/core
pnpm add @langchain/openai @langchain/core
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