当前位置: 首页 > news >正文

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

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

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

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

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

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

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

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

# 原文代码:少样本示例列表(4个自问自答案例)
from langchain.prompts.few_shot import FewShotPromptTemplate
from langchain.prompts.prompt import PromptTemplateexamples = [{"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)转成固定格式的文本,代码照搬原文:

# 原文代码:单个示例的格式化模板
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)

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

# 原文代码:创建完整的少样本提示模板
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 AliQuestion: 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, 1952Question: 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 BallQuestion: 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: NoQuestion: Who was the father of Mary Ball Washington?
  • 原文关键说明:suffix是“示例的后缀”,也就是所有示例显示完后,最后要问的问题;input_variables=["input"]表示你调用format时,必须传input参数(即你的问题)。

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

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

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

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

# 原文代码:导入需要的工具(向量存储、嵌入模型、示例选择器)
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?”这个问题测试,看选哪个示例,代码照搬原文:

# 原文代码:定义要测试的问题
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”,其他不变,代码照搬原文:

# 原文代码:创建少样本模板(用示例选择器代替所有示例)
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 BallQuestion: 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:存储数字并找相似。
http://www.dtcms.com/a/549091.html

相关文章:

  • Product Hunt 每日热榜 | 2025-10-30
  • Spring MVC核心概念
  • 鸿蒙HDF框架源码分析
  • Springboot旅游管理系统8cx8xy5m(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
  • ppt免费制作网站如何建设网站山东济南兴田德润官网
  • 如何获取网站根目录链接诸城做网站的
  • GPT-0: Attention+Transformer+可视化
  • 告别“人眼扫描”:EasyGBS智能搜索功能助力重塑海量视频监控管理效率
  • 【ubuntu】ubuntu系统如何快速删除当前用户的配置
  • dz地方门户网站制作南昌seo招聘信息
  • 灵犀科技网站开发湖南网站建设的公司排名
  • 沈阳装修公司网站建设做网站只用前端知识可以吗
  • ELK es+logstash
  • Java 大视界 -- Java 大数据在智能医疗远程康复数据管理与康复方案个性化定制实战(430)
  • 【C#】XtraMessageBox(DevExpress)与MessageBox(WinForms 标准库)的区别
  • 石家庄物流网站建设北京公司网站建设价格
  • 网络编程入门
  • 每日一个C语言知识:C 错误处理
  • 基础建设的网站有哪些内容网页制作基础教程费
  • 建站工具哪个好用深圳网站建设定制平台
  • 2048游戏笔记3 游戏开始与结束 cocos3.8.7
  • 【AI WorkFow】n8n 源码分析-核心结构体设计思考(六)
  • 网站诊断内容工程建设教育培训
  • Opencv(四):自适应二值化
  • GitHub 热榜项目 - 日榜(2025-10-30)
  • 网络:4.应用层自定义协议与序列化
  • Word崩溃打不开?实测三款Word文档修复工具!
  • 哪个网站做签约插画师好网站域名过期后续费多长时间生效
  • 《R for Data Science (2e)》免费中文翻译 (第11章) --- Communication(2)
  • 扩展阅读:CSV格式的目标检测(Object Detection)标注文件示例