Langchain文档 少样本提示模板

316 阅读4分钟

在这个教程中,我们将学习如何创建一个使用few shot examples的prompt模板。Few shot prompt模板可以从一组例子或Example Selector对象构建。

用例 在本教程中,我们将配置few shot examples以进行自我问答搜索。

使用一个例子集合 创建例子集合 首先,创建一个few shot examples的列表。每个例子应该是一个字典,键是输入变量,值是这些输入变量的值。

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"""
  }
]

创建格式化器用于few shot examples 配置一个格式化器,将few shot examples格式化为字符串。这个格式化器应该是一个PromptTemplate对象。

example_prompt = PromptTemplate(input_variables=["question", "answer"], 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

将例子和格式化器提供给FewShotPromptTemplate 最后,创建一个FewShotPromptTemplate对象。这个对象接受few shot examples和few shot examples的格式化器。

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?"))

输出:

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?

使用ExampleSelector来选择例子 将例子提供给ExampleSelector 我们将重用前面部分的例子集合和格式化器。但是,不再直接将例子提供给FewShotPromptTemplate对象,而是将它们提供给一个ExampleSelector对象。

在本教程中,我们将使用SemanticSimilarityExampleSelector类。这个类根据输入与few shot examples之间的相似度选择few shot examples。它使用嵌入模型计算输入与few shot examples之间的相似度,并使用向量存储库执行最近邻搜索。

from langchain.prompts.example_selector import SemanticSimilarityExampleSelector
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings

example_selector = SemanticSimilarityExampleSelector.from_examples(
    # 这是可以选择的例子列表。
    examples,
    # 这是用于产生嵌入的嵌入类,用于衡量语义相似度。
    OpenAIEmbeddings(),
    # 这是用于存储嵌入和执行相似度搜索的向量存储库类。
    Chroma,
    # 这是要生成的例子数量。
    k=1
)
#选择与输入最相似的例子 
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}")

输出:


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

将ExampleSelector提供给FewShotPromptTemplate 最后,创建一个FewShotPromptTemplate对象。这个对象接受ExampleSelector和few shot examples的格式化器。

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?"))

输出:

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?