LangChain 提示模板之少样本示例(二)

https://python.langchain.com.cn/docs/modules/model_io/prompts/prompt_templates/few_shot_examples

一、先明确原文核心目标(Use Case)

原文开篇就定了任务:为"自问自答与搜索"配置少样本示例 ------简单说,就是让LLM遇到问题时,先判断"是否需要追问中间问题",再一步步推导最终答案(比如问"谁活得更长",先追问"两人各自活了多少岁",再对比)。

所有代码和步骤,都是为了实现这个任务。

二、第一部分:直接使用示例集(原文第一大板块)

原文第一步讲"手动准备好所有示例,直接传给模板",分3个具体步骤,每个步骤都对应原文代码。

步骤1:创建少样本示例集(原文核心代码)

原文说"每个示例是包含'输入变量'的字典",这里的输入变量是question(问题)和answer(带追问步骤的答案),代码完全照搬原文:

python 复制代码
# 原文代码:少样本示例列表(4个自问自答案例)
from langchain.prompts.few_shot import FewShotPromptTemplate
from langchain.prompts.prompt import PromptTemplate

examples = [
  {
    "question": "Who lived longer, Muhammad Ali or Alan Turing?",
    "answer": 
"""
Are follow up questions needed here: Yes.
Follow up: How old was Muhammad Ali when he died?
Intermediate answer: Muhammad Ali was 74 years old when he died.
Follow up: How old was Alan Turing when he died?
Intermediate answer: Alan Turing was 41 years old when he died.
So the final answer is: Muhammad Ali
"""
  },
  {
    "question": "When was the founder of craigslist born?",
    "answer": 
"""
Are follow up questions needed here: Yes.
Follow up: Who was the founder of craigslist?
Intermediate answer: Craigslist was founded by Craig Newmark.
Follow up: When was Craig Newmark born?
Intermediate answer: Craig Newmark was born on December 6, 1952.
So the final answer is: December 6, 1952
"""
  },
  {
    "question": "Who was the maternal grandfather of George Washington?",
    "answer":
"""
Are follow up questions needed here: Yes.
Follow up: Who was the mother of George Washington?
Intermediate answer: The mother of George Washington was Mary Ball Washington.
Follow up: Who was the father of Mary Ball Washington?
Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
So the final answer is: Joseph Ball
"""
  },
  {
    "question": "Are both the directors of Jaws and Casino Royale from the same country?",
    "answer":
"""
Are follow up questions needed here: Yes.
Follow up: Who is the director of Jaws?
Intermediate Answer: The director of Jaws is Steven Spielberg.
Follow up: Where is Steven Spielberg from?
Intermediate Answer: The United States.
Follow up: Who is the director of Casino Royale?
Intermediate Answer: The director of Casino Royale is Martin Campbell.
Follow up: Where is Martin Campbell from?
Intermediate Answer: New Zealand.
So the final answer is: No
"""
  }
]
  • 原文关键说明:每个字典里的questionanswer是"固定输入变量名",后面的模板会用这两个名字取数据,不能随便改。

步骤2:创建"单个示例的格式化程序"(原文叫 example_prompt)

原文说这一步是"定义单个示例要怎么显示"------把每个examples里的字典(question+answer)转成固定格式的文本,代码照搬原文:

python 复制代码
# 原文代码:单个示例的格式化模板
example_prompt = PromptTemplate(
    input_variables=["question", "answer"],  # 必须和示例字典的key对应
    template="Question: {question}\n{answer}"  # 单个示例的显示格式:先问题,再答案
)

# 原文测试:打印第一个示例的格式化结果
print(example_prompt.format(**examples[0]))
  • 原文运行结果(你会看到的内容):

    Question: Who lived longer, Muhammad Ali or Alan Turing?

    复制代码
      Are follow up questions needed here: Yes.
      Follow up: How old was Muhammad Ali when he died?
      Intermediate answer: Muhammad Ali was 74 years old when he died.
      Follow up: How old was Alan Turing when he died?
      Intermediate answer: Alan Turing was 41 years old when he died.
      So the final answer is: Muhammad Ali
  • 难点解释:**examples[0]是"解包字典"------把examples[0]里的questionanswer自动传给example_promptinput_variables,不用手动写question=examples[0]["question"], answer=examples[0]["answer"]

步骤3:创建少样本提示模板(FewShotPromptTemplate)

原文说这是"把示例集和格式化程序组合起来",再加上"最终要问的问题",代码照搬原文:

python 复制代码
# 原文代码:创建完整的少样本提示模板
prompt = FewShotPromptTemplate(
    examples=examples,  # 步骤1准备的所有示例
    example_prompt=example_prompt,  # 步骤2定义的单个示例格式化模板
    suffix="Question: {input}",  # 示例之后的"最终问题"({input}是用户要传的问题)
    input_variables=["input"]  # 告诉模板:用户需要传的参数是"input"(即最终问题)
)

# 原文测试:传入最终问题,生成完整提示
print(prompt.format(input="Who was the father of Mary Ball Washington?"))
  • 原文运行结果(你会看到的内容):
    模板会自动把所有示例按example_prompt格式拼好,最后加上你传的问题:

    Question: Who lived longer, Muhammad Ali or Alan Turing?

    复制代码
      Are follow up questions needed here: Yes.
      Follow up: How old was Muhammad Ali when he died?
      Intermediate answer: Muhammad Ali was 74 years old when he died.
      Follow up: How old was Alan Turing when he died?
      Intermediate answer: Alan Turing was 41 years old when he died.
      So the final answer is: Muhammad Ali
      
      
      Question: When was the founder of craigslist born?
      
      Are follow up questions needed here: Yes.
      Follow up: Who was the founder of craigslist?
      Intermediate answer: Craigslist was founded by Craig Newmark.
      Follow up: When was Craig Newmark born?
      Intermediate answer: Craig Newmark was born on December 6, 1952.
      So the final answer is: December 6, 1952
      
      
      Question: Who was the maternal grandfather of George Washington?
      
      Are follow up questions needed here: Yes.
      Follow up: Who was the mother of George Washington?
      Intermediate answer: The mother of George Washington was Mary Ball Washington.
      Follow up: Who was the father of Mary Ball Washington?
      Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
      So the final answer is: Joseph Ball
      
      
      Question: Are both the directors of Jaws and Casino Royale from the same country?
      
      Are follow up questions needed here: Yes.
      Follow up: Who is the director of Jaws?
      Intermediate Answer: The director of Jaws is Steven Spielberg.
      Follow up: Where is Steven Spielberg from?
      Intermediate Answer: The United States.
      Follow up: Who is the director of Casino Royale?
      Intermediate Answer: The director of Casino Royale is Martin Campbell.
      Follow up: Where is Martin Campbell from?
      Intermediate Answer: New Zealand.
      So the final answer is: No
      
      
      Question: Who was the father of Mary Ball Washington?
  • 原文关键说明:suffix是"示例的后缀",也就是所有示例显示完后,最后要问的问题;input_variables=["input"]表示你调用format时,必须传input参数(即你的问题)。

三、第二部分:使用示例选择器(原文第二大板块)

原文说这是"进阶用法"------不把所有示例都放进提示,而是"根据用户问题,选最相似的1个示例"(减少提示长度,让LLM更聚焦),分3个步骤,代码完全对应原文。

步骤1:创建示例选择器(SemanticSimilarityExampleSelector)

原文用"语义相似度"选示例(比如用户问"Mary Ball Washington的父亲是谁",选和"George Washington外祖父"最像的示例),代码照搬原文:

python 复制代码
# 原文代码:导入需要的工具(向量存储、嵌入模型、示例选择器)
from langchain.prompts.example_selector import SemanticSimilarityExampleSelector
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings

# 原文代码:创建示例选择器
example_selector = SemanticSimilarityExampleSelector.from_examples(
    examples,  # 还是步骤1的示例集(所有可选的示例)
    OpenAIEmbeddings(),  # 用OpenAI的嵌入模型(把文字转成"能算相似度的数字")
    Chroma,  # 用Chroma向量存储(存嵌入后的数字,方便找相似)
    k=1  # 只选"最相似的1个示例"
)
  • 难点解释:
    • "嵌入模型(OpenAIEmbeddings)":把"问题文字"转成一串数字(比如"苹果"转成[0.1, 0.2,...]),数字越像,文字语义越近;
    • "Chroma":专门存这些数字的工具,能快速找出和"用户问题的数字"最像的"示例数字";
    • "k=1":只选1个最像的示例,原文用k=1,你也可以改k=2选2个。

步骤2:测试示例选择器(选最相似的示例)

原文用"Who was the father of Mary Ball Washington?"这个问题测试,看选哪个示例,代码照搬原文:

python 复制代码
# 原文代码:定义要测试的问题
question = "Who was the father of Mary Ball Washington?"

# 原文代码:根据问题选最相似的示例
selected_examples = example_selector.select_examples({"question": question})

# 原文代码:打印选中的示例
print(f"Examples most similar to the input: {question}")
for example in selected_examples:
    print("\n")
    for k, v in example.items():
        print(f"{k}: {v}")
  • 原文运行结果(你会看到的内容):
    会选中"George Washington外祖父"的示例,因为两个问题都和"Mary Ball Washington的亲属"相关:

    Running Chroma using direct local API.
    Using DuckDB in-memory for database. Data will be transient.
    Examples most similar to the input: Who was the father of Mary Ball Washington?

    question: Who was the maternal grandfather of George Washington?
    answer:
    Are follow up questions needed here: Yes.
    Follow up: Who was the mother of George Washington?
    Intermediate answer: The mother of George Washington was Mary Ball Washington.
    Follow up: Who was the father of Mary Ball Washington?
    Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
    So the final answer is: Joseph Ball

步骤3:用示例选择器创建少样本模板

原文说"把之前的examples参数换成example_selector",其他不变,代码照搬原文:

python 复制代码
# 原文代码:创建少样本模板(用示例选择器代替所有示例)
prompt = FewShotPromptTemplate(
    example_selector=example_selector,  # 步骤1创建的示例选择器
    example_prompt=example_prompt,  # 还是步骤2的单个示例格式化模板
    suffix="Question: {input}",  # 还是最终问题的格式
    input_variables=["input"]  # 还是用户传"input"参数
)

# 原文测试:传入问题,生成完整提示
print(prompt.format(input="Who was the father of Mary Ball Washington?"))
  • 原文运行结果(你会看到的内容):
    这次只显示"最相似的1个示例",而不是所有4个,最后加问题:

    Question: Who was the maternal grandfather of George Washington?

    复制代码
      Are follow up questions needed here: Yes.
      Follow up: Who was the mother of George Washington?
      Intermediate answer: The mother of George Washington was Mary Ball Washington.
      Follow up: Who was the father of Mary Ball Washington?
      Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
      So the final answer is: Joseph Ball
      
      
      Question: Who was the father of Mary Ball Washington?
  • 原文关键说明:这种方式的好处是"提示更短",LLM不用看无关示例,回答更准、更快。

四、原文核心总结(只提炼原文内容)

  1. 少样本提示模板的两种用法
    • 直接用examples:把所有示例都放进提示,适合示例少的场景;
    • example_selector:按语义相似度选最相关的示例,适合示例多的场景。
  2. 必须有的3个核心组件
    • 示例集(examples):字典列表,每个字典含input_variables对应的key;
    • 单个示例格式化模板(example_prompt):定义单个示例的显示格式;
    • 少样本模板(FewShotPromptTemplate):组合示例、格式化模板和最终问题。
  3. 示例选择器的核心工具
    • SemanticSimilarityExampleSelector:按语义选示例;
    • OpenAIEmbeddings:把文字转成相似度数字;
    • Chroma:存储数字并找相似。
相关推荐
大数据追光猿17 小时前
LangChain / LangGraph / AutoGPT / CrewAI / AutoGen 五大框架对比
经验分享·笔记·python·langchain·agent
core5121 天前
LangChain实现Text2SQL
langchain·大模型·qwen·text2sql
吴佳浩1 天前
LangChain v1 重大更新讲解⚠⚠⚠
python·langchain·agent
FreeCode2 天前
LangGraph智能体开发快速入门
后端·langchain·agent
组合缺一2 天前
Solon AI 开发学习3 - chat - 模型配置与请求选项
java·学习·ai·chatgpt·langchain·solon
菥菥爱嘻嘻2 天前
langchain学习-RAG+prompt+OutPutParse
学习·langchain·prompt
FreeCode2 天前
LangSmith Studio 调试智能体
python·langchain·agent
喜欢吃豆3 天前
LangChain v1.0 技术研究报告:架构范式向智能体中间件与图运行时的演进
中间件·架构·langchain·大模型
百***97643 天前
LangChain-08 Query SQL DB 通过GPT自动查询SQL
数据库·sql·langchain
小陈phd3 天前
RAG从入门到精通(四)——结构化数据读取与导入
人工智能·langchain