引言
在自然语言处理领域,少样本提示(Few-shot prompting)是一种强大且简单的方法,用于指导生成模型。在这篇文章中,我们将探讨如何在聊天模型中使用少样本提示,并通过代码示例展示其应用。我们还将讨论可能遇到的挑战及解决方案,并提供进一步学习的资源。
主要内容
固定示例
最基本的少样本提示技术是使用固定的提示示例。这种方法可以评估链条并避免生产中的额外移动部分。模板的基本组件包括:
- examples: 要包含在最终提示中的字典示例列表。
- example_prompt: 通过其
format_messages方法将每个示例转换为一个或多个消息。
示例代码:
# 使用API代理服务提高访问稳定性
%pip install -qU langchain langchain-openai langchain-chroma
import os
from getpass import getpass
os.environ["OPENAI_API_KEY"] = getpass()
from langchain_openai import ChatOpenAI
model = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0.0)
response = model.invoke("What is 2 🦜 9?")
print(response.content)
未提供示例时,模型无法正确解释新的符号。我们可以通过以下方式添加示例:
from langchain_core.prompts import ChatPromptTemplate, FewShotChatMessagePromptTemplate
examples = [
{"input": "2 🦜 2", "output": "4"},
{"input": "2 🦜 3", "output": "5"},
]
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())
final_prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a wondrous wizard of math."),
few_shot_prompt,
("human", "{input}"),
]
)
chain = final_prompt | model
response = chain.invoke({"input": "What is 2 🦜 9?"})
print(response.content)
动态少样本提示
在某些情况下,您可能希望根据输入动态选择几个示例。此时,可以将 examples 替换为 example_selector,其余组件保持不变。我们将使用 SemanticSimilarityExampleSelector 示例来展示动态选择示例的过程。
示例代码:
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 = SemanticSimilarityExampleSelector(
vectorstore=vectorstore,
k=2,
)
selected_examples = example_selector.select_examples({"input": "horse"})
print(selected_examples)
构建提示模板并连接聊天模型:
from langchain_core.prompts import ChatPromptTemplate, FewShotChatMessagePromptTemplate
few_shot_prompt = FewShotChatMessagePromptTemplate(
input_variables=["input"],
example_selector=example_selector,
example_prompt=ChatPromptTemplate.from_messages([("human", "{input}"), ("ai", "{output}")]),
)
final_prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a wondrous wizard of math."),
few_shot_prompt,
("human", "{input}"),
]
)
chain = final_prompt | ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0.0)
response = chain.invoke({"input": "What's 3 🦜 3?"})
print(response.content)
常见问题和解决方案
1. 模型性能不稳定
解决方案: 确保提供的示例足够相关,并使用API代理服务以提高访问的稳定性。
2. 动态示例选择不准确
解决方案: 确保示例集足够多样,并使用适当的相似性度量方法。
总结和进一步学习资源
少样本提示是一种有效的方法,可显著提升聊天模型的性能。通过固定或动态选择示例,可以指导模型更好地理解和生成响应。建议进一步阅读以下资源以深入了解:
参考资料
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力! ---END---