LangChain —— Prompt Templates —— How to use few shot examples in chat models

文章目录

  • 一、概述
  • [二、固定示例 Fixed Example](#二、固定示例 Fixed Example)

一、概述

 本指南介绍了如何使用示例输入和输出提示 char model。为模型提供几个这样的例子被称为 few-shotting,这是一种简单而强大的方法来指导生成,在某些情况下可以大大提高模型性能。

 对于如何最好地进行 few-shot 提示,似乎没有达成一致意见,最佳提示编译可能会因模型而异。因此,langchain 提供 few-shot 提示模板,如 FewShotChatMessagePromptTemplate,作为一个灵活的起点,我们可以根据需要修改或替换它们

 few-shot 提示模板的目标是根据输入动态选择示例,然后在最终提示中格式化示例以提供模型。



 注意,以下代码示例仅适用于 chat model,因为 FewShotChatMessagePromptTemplates 旨在输出格式化的 chat message,而不是纯字符串。


二、固定示例 Fixed Example

 最基本 (也是最常见) 的 few-shot 提示技术是使用 固定提示示例 。通过这种方式,您可以选择一条链,对其进行评估,并避免在生产中担心额外的 moving parts。

 模板的基本组件包括:

  1. examples:要包含在最终提示中的词典示例列表。
  2. example_prompt:通过其 format_messages 方法将每个示例转换为 1条或多条 消息。一个常见的例子是将每个示例转换为一条 human message 和一条 AI message 响应,或者一条 human message 后跟一条 function call message。

 下面是一个简单的演示。首先,定义要包含的示例。让我们给 LLM 一个不熟悉的数学运算符,用"🦜"表情符号。如果我们尝试问模型,该运算的结果是什么,这将会失败:

python 复制代码
from langchain_openai import ChatOpenAI

model = ChatOpenAI(model="gpt-3.5-turbo-0125", 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-3.5-turbo-0125', '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})
"""

 现在让我们看看如果我们给LLM一些例子来使用会发生什么。我们将在下面定义一些:

python 复制代码
from langchain_core.prompts import ChatPromptTemplate, FewShotChatMessagePromptTemplate

examples = [
    {"input": "2 🦜 2", "output": "4"},
    {"input": "2 🦜 3", "output": "5"},
]

 接下来,将它们组装到 few-shot 提示模板中。

python 复制代码
# 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 工厂方法,并将其与模型一起使用:

python 复制代码
final_prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a wondrous wizard of math."),
        few_shot_prompt,
        ("human", "{input}"),
    ]
)

 现在让我们问模型最初的问题,看看它是如何做到的:

python 复制代码
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-3.5-turbo-0125', '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})
"""

 我们可以看到,模型现在已经从给定的几个热门例子中推断出鹦鹉表情符号意味着添加!

相关推荐
Boop_wu1 小时前
[LangChain]LangChain聊天模型
langchain
湿韵轮回4 小时前
LangChain LCEL,用 | 串联AI的魔法语言
服务器·人工智能·langchain
吃饱了得干活7 小时前
别只会传 temperature!一文挖透 LangChain 模型配置:Profile、全参数与运行时动态覆盖
python·langchain
龙腾亚太8 小时前
基础差/零基础人员具身智能学习路线
langchain·qlora·大模型培训·智能体培训·具身智能培训·llm 实战教程
haluhalu.10 小时前
prompts.chat:02-anatomy-of-effective-prompt
jvm·prompt
xywww16814 小时前
大模型 API 选型实战:GPT、Gemini、Claude 接入时该看哪些指标?
运维·服务器·人工智能·python·gpt·langchain
城事漫游Molly1 天前
用AI提炼研究问题与假设的完整提示词——从模糊想法到精准RQ/Hypothesis,AI辅助精化全流程
人工智能·机器学习·prompt·ai for science·博士生必读·应用语言学·研究问题
糖果店的幽灵1 天前
从 GPT Researcher 学习 LangChain
gpt·学习·langchain
吃饱了得干活1 天前
LangChain 模型调用方案:invoke、stream、batch 的同步与异步
langchain·llm
codingPower1 天前
提示词(Prompt)介绍:核心概念、主流框架与权威认证
prompt