【LangChain系列 7】Prompt模版——少样本prompt模版(一)

原文地址:【LangChain系列 7】Prompt模版------少样本prompt模版(一)

本文速读:

  • prompt样本集合

  • prompt样本选择器

少样本模版的意思是:在prompt中包含一些样本,这样LLM就可以根据这些样本,更好的理解prompt,从而给出更加符合我们需要的回答。

下面我们将介绍两种方式实现 少样本prompt模版:

  • prompt样本集合

  • prompt样本选择器

01 Prompt样本集合


Prompt样本集合是指:将所有给定的样本集合作为prompt的一部分,输入给LLM。比如说样本如下:

python 复制代码
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
"""
  }
]

example_prompt = PromptTemplate(input_variables=["question", "answer"], template="Question: {question}\n{answer}")

print(example_prompt.format(**examples[0]))

输出结果:

python 复制代码
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

下面我们创建FewShotPromptTemplate实例,并将所有样本通过参数examples传入给模版:

python 复制代码
prompt = FewShotPromptTemplate(
    examples=examples, 
    example_prompt=example_prompt, 
    suffix="Question: {input}", 
    input_variables=["input"]
)

print(prompt.format(input="Who was the father of Mary Ball Washington?"))

输出结果:

python 复制代码
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?

以上就是通过 prompt样本集合 的方式创建了少样本prompt模版。

02 Prompt样本选择器


prompt样本集合将所有的样本作为prompt的一部分,这种方式有两个问题:

  1. 有些样本可能和我们的提问的相关性不大

  2. 所有的样本太多,超过了LLM的token限制

为了解决这个问题,LangChain提供了ExampleSelector,这样我们就可以选择部分更加合适的样本输入给LLM。

下面我们用SemanticSimilarityExampleSelector重写上面的代码。

  1. 定义example_selector​​​​​​​
python 复制代码
from langchain.prompts.example_selector import SemanticSimilarityExampleSelector
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings


example_selector = SemanticSimilarityExampleSelector.from_examples(
    # This is the list of examples available to select from.
    examples,
    # This is the embedding class used to produce embeddings which are used to measure semantic similarity.
    OpenAIEmbeddings(),
    # This is the VectorStore class that is used to store the embeddings and do a similarity search over.
    Chroma,
    # This is the number of examples to produce.
    k=1
)

# Select the most similar example to the input.
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}")
  1. 创建FewShotPromptTemplate实例​​​​​​​
python 复制代码
prompt = FewShotPromptTemplate(
    example_selector=example_selector, 
    example_prompt=example_prompt, 
    suffix="Question: {input}", 
    input_variables=["input"]
)

print(prompt.format(input="Who was the father of Mary Ball Washington?"))

输出结果:​​​​​​​

python 复制代码
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?

通过ExampleSelector的方式选择合适的部分样本 作为prompt的一部分,这种方式称为 prompt样本选择器,一方面解决样本过多的问题,另一方面可以提高prompt的质量,从而得到更加符合我们要求的回答。

本文小结

通过介绍 prompt样本集合prompt样本选择器 两种创建少样本prompt模版的方式,我们对 少样本prompt模版 有了基本的认识,可以根据实际的业务需求创建自己的 少样本prompt模版 了。

更多最新文章,请关注公众号:大白爱爬山

相关推荐
xiaoxiaoxiaolll40 分钟前
期刊速递 | 《Light Sci. Appl.》超宽带光热电机理研究,推动碳纳米管传感器在制药质控中的实际应用
人工智能·学习
练习两年半的工程师1 小时前
AWS TechFest 2025: 风险模型的转变、流程设计的转型、生成式 AI 从实验走向实施的三大关键要素、评估生成式 AI 用例的适配度
人工智能·科技·金融·aws
Elastic 中国社区官方博客3 小时前
Elasticsearch:智能搜索的 MCP
大数据·人工智能·elasticsearch·搜索引擎·全文检索
stbomei3 小时前
从“能说话”到“会做事”:AI Agent如何重构日常工作流?
人工智能
yzx9910134 小时前
生活在数字世界:一份人人都能看懂的网络安全生存指南
运维·开发语言·网络·人工智能·自动化
许泽宇的技术分享5 小时前
LangGraph深度解析:构建下一代智能Agent的架构革命——从Pregel到现代AI工作流的技术飞跃
人工智能·架构
乔巴先生245 小时前
LLMCompiler:基于LangGraph的并行化Agent架构高效实现
人工智能·python·langchain·人机交互
静西子6 小时前
LLM大语言模型部署到本地(个人总结)
人工智能·语言模型·自然语言处理
cxr8286 小时前
基于Claude Code的 规范驱动开发(SDD)指南
人工智能·hive·驱动开发·敏捷流程·智能体
Billy_Zuo6 小时前
人工智能机器学习——决策树、异常检测、主成分分析(PCA)
人工智能·决策树·机器学习