如何在聊天模型中使用少量示例
本指南介绍如何通过提供示例输入和输出来提示聊天模型。向模型提供几个这样的示例,称为少样本提示,这是一种简单但强大的引导生成方式,在某些情况下可以显著提升模型性能。
目前尚无明确共识说明如何最佳地进行少样本提示,且最优的提示编译方式可能因模型而异。因此,我们提供了如 FewShotChatMessagePromptTemplate 这样的少样本提示模板作为灵活的起点,您可以根据需要对其进行修改或替换。
少样本提示模板的目标是根据输入动态选择示例,然后将这些示例格式化为最终的提示,以提供给模型。
注意: 以下代码示例仅适用于聊天模型,因为 FewShotChatMessagePromptTemplates 设计用于输出格式化的 聊天消息,而不是纯字符串。有关与补全模型(LLMs)兼容的纯字符串模板的类似少样本提示示例,请参阅 少样本提示模板 指南。
固定示例
最基础(也是最常见的)少样本提示技术是使用固定的提示示例。这样你可以选择一个链,进行评估,并避免在生产环境中担心额外的变动因素。
模板的基本组成部分是:
examples: 一个字典示例列表,将包含在最终提示中。example_prompt:通过其format_messages方法将每个示例转换为一个或多个消息。一个常见的例子是将每个示例转换为一条人类消息和一条AI消息回复,或者一条人类消息后跟一条函数调用消息。
以下是一个简单的演示。首先,定义您想要包含的示例。让我们给大语言模型一个不熟悉的数学运算符,用“🦜”表情符号表示:
%pip install -qU langchain langchain-openai langchain-chroma
import os
from getpass import getpass
if "OPENAI_API_KEY" not in os.environ:
os.environ["OPENAI_API_KEY"] = getpass()
如果我们尝试让模型回答这个表达式的结果,它将会失败:
from langchain_openai import ChatOpenAI
model = ChatOpenAI(model="gpt-4o-mini", temperature=0.0)
model.invoke("What is 2 🦜 9?")
AIMessage(content='The expression "2 🦜 9" is not a standard mathematical operation or equation. It appears to be a combination of the number 2 and the parrot emoji 🦜 followed by the number 9. It does not have a specific mathematical meaning.', response_metadata={'token_usage': {'completion_tokens': 54, 'prompt_tokens': 17, 'total_tokens': 71}, 'model_name': 'gpt-4o-mini', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-aad12dda-5c47-4a1e-9949-6fe94e03242a-0', usage_metadata={'input_tokens': 17, 'output_tokens': 54, 'total_tokens': 71})
现在我们来看看,如果给大型语言模型一些示例,会发生什么情况。我们将在下面定义一些示例:
from langchain_core.prompts import ChatPromptTemplate, FewShotChatMessagePromptTemplate
examples = [
{"input": "2 🦜 2", "output": "4"},
{"input": "2 🦜 3", "output": "5"},
]
接下来,将它们组合成少样本提示模板。
# This is a prompt template used to format each individual example.
example_prompt = ChatPromptTemplate.from_messages(
[
("human", "{input}"),
("ai", "{output}"),
]
)
few_shot_prompt = FewShotChatMessagePromptTemplate(
example_prompt=example_prompt,
examples=examples,
)
print(few_shot_prompt.invoke({}).to_messages())
[HumanMessage(content='2 🦜 2'), AIMessage(content='4'), HumanMessage(content='2 🦜 3'), AIMessage(content='5')]
最后,我们如下面所示组装最终的提示,直接将 few_shot_prompt 传递给 from_messages 工厂方法,并将其与模型一起使用:
final_prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a wondrous wizard of math."),
few_shot_prompt,
("human", "{input}"),
]
)
现在,让我们向模型提出初始问题,看看它的表现如何:
from langchain_openai import ChatOpenAI
chain = final_prompt | model
chain.invoke({"input": "What is 2 🦜 9?"})
AIMessage(content='11', response_metadata={'token_usage': {'completion_tokens': 1, 'prompt_tokens': 60, 'total_tokens': 61}, 'model_name': 'gpt-4o-mini', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-5ec4e051-262f-408e-ad00-3f2ebeb561c3-0', usage_metadata={'input_tokens': 60, 'output_tokens': 1, 'total_tokens': 61})
我们可以看到,模型现在已经从提供的少量示例中推断出,鹦鹉表情符号代表加法!
动态少样本提示
有时,您可能希望根据输入仅从整体示例集中选择少数几个示例进行展示。为此,您可以将传入 FewShotChatMessagePromptTemplate 的 examples 替换为 example_selector。其他组件与上文保持不变!我们的动态少样本提示模板将如下所示:
example_selector: 负责为给定输入选择少量示例(以及它们返回的顺序)。这些实现 BaseExampleSelector 接口。一个常见示例是基于向量存储的 SemanticSimilarityExampleSelectorexample_prompt: 通过其format_messages方法将每个示例转换为一个或多个消息。常见的示例是将每个示例转换为一条用户消息和一条AI回复消息,或者一条用户消息后跟一条函数调用消息。
这些消息可以与其他消息和聊天模板组合,以构建最终的提示。
让我们通过一个使用 SemanticSimilarityExampleSelector 的示例来逐步讲解。由于此实现使用向量存储根据语义相似性选择示例,因此我们首先需要填充存储。这里的基本思路是,我们希望搜索并返回与输入文本最相似的示例,因此我们会对提示示例的 values 进行嵌入,而不是考虑键:
from langchain_chroma import Chroma
from langchain_core.example_selectors import SemanticSimilarityExampleSelector
from langchain_openai import OpenAIEmbeddings
examples = [
{"input": "2 🦜 2", "output": "4"},
{"input": "2 🦜 3", "output": "5"},
{"input": "2 🦜 4", "output": "6"},
{"input": "What did the cow say to the moon?", "output": "nothing at all"},
{
"input": "Write me a poem about the moon",
"output": "One for the moon, and one for me, who are we to talk about the moon?",
},
]
to_vectorize = [" ".join(example.values()) for example in examples]
embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_texts(to_vectorize, embeddings, metadatas=examples)
创建 example_selector
创建向量存储后,我们可以创建 example_selector。在这里,我们将它单独调用,并设置 k,以仅获取与输入最接近的两个示例。
example_selector = SemanticSimilarityExampleSelector(
vectorstore=vectorstore,
k=2,
)
# The prompt template will load examples by passing the input do the `select_examples` method
example_selector.select_examples({"input": "horse"})
[{'input': 'What did the cow say to the moon?', 'output': 'nothing at all'},
{'input': '2 🦜 4', 'output': '6'}]
创建提示模板
我们现在组装提示模板,使用上面创建的 example_selector。
from langchain_core.prompts import ChatPromptTemplate, FewShotChatMessagePromptTemplate
# Define the few-shot prompt.
few_shot_prompt = FewShotChatMessagePromptTemplate(
# The input variables select the values to pass to the example_selector
input_variables=["input"],
example_selector=example_selector,
# Define how each example will be formatted.
# In this case, each example will become 2 messages:
# 1 human, and 1 AI
example_prompt=ChatPromptTemplate.from_messages(
[("human", "{input}"), ("ai", "{output}")]
),
)
print(few_shot_prompt.invoke(input="What's 3 🦜 3?").to_messages())
[HumanMessage(content='2 🦜 3'), AIMessage(content='5'), HumanMessage(content='2 🦜 4'), AIMessage(content='6')]
我们可以将这个少样本聊天消息提示模板传递给另一个聊天提示模板:
final_prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a wondrous wizard of math."),
few_shot_prompt,
("human", "{input}"),
]
)
print(few_shot_prompt.invoke(input="What's 3 🦜 3?"))
messages=[HumanMessage(content='2 🦜 3'), AIMessage(content='5'), HumanMessage(content='2 🦜 4'), AIMessage(content='6')]
与聊天模型一起使用
最后,你可以将你的模型连接到少样本提示中。
chain = final_prompt | ChatOpenAI(model="gpt-4o-mini", temperature=0.0)
chain.invoke({"input": "What's 3 🦜 3?"})
AIMessage(content='6', response_metadata={'token_usage': {'completion_tokens': 1, 'prompt_tokens': 60, 'total_tokens': 61}, 'model_name': 'gpt-4o-mini', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-d1863e5e-17cd-4e9d-bf7a-b9f118747a65-0', usage_metadata={'input_tokens': 60, 'output_tokens': 1, 'total_tokens': 61})
下一步
你现在已学会如何向聊天提示中添加少量示例。
接下来,查看本节中关于提示模板的其他操操作指南、相关操操作指南“使用文本补全模型进行少量示例”,或其它“示例选择器操操作指南”。