跳过正文
  1. Posts/

深入了解 langchain prompts 二!

·434 字·3 分钟· loading
langchain langchain python LLM
demo007x
作者
demo007x
目录

深入了解 langchain prompts 二(FewShotPromptTemplate)
#

FewShotPromptTemplate
#

在本篇文章中我们配置一些用于自我询问和搜索的示例从而介绍提示模板相关的内容。

创建示例
#

首先,创建一个少量示例的列表。每个示例都应该是一个字典,其中键是输入变量,值是这些输入变量的值。

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

配置一个格式化程序,将少量示例格式化为字符串。这个格式化程序应该是一个PromptTemplate对象。

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

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

这样我们就构造出了拥有 4 条对话历史的 prompt.

将格式化后的example 提供给FewShotPromptTemplate
#

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

将示例输入到 ExampleSelector
#

我们将重用上面的示例集和格式化程序。但是我们不会将示例直接输入到 FewShotPromptTemplate对象中,而是将它们输入到 ExampleSelector对象中。

我们将使用该类SemanticSimilarityExampleSelector 。此类根据与输入的相似性来选择少数样本。它使用嵌入模型来计算输入和少数样本之间的相似度,并使用向量存储来执行最近邻搜索。

from langchain.prompts.example_selector import SemanticSimilarityExampleSelector
from langchain_community.vectorstores import Chroma
from langchain_openai 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}")

示例选择器输入到FewShotPromptTemplate
#

最后,创建一个FewShotPromptTemplate对象。该对象接受示例选择器和少数示例的格式化程序。

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

相关文章

LLM 中什么是Prompts?如何使用 LangChain 快速实现Prompts 一
langchain langchain python LLM
Prompt是一种基于自然语言处理的交互方式,它通过机器对自然语言的解析,实现用户与机器之间的沟通。 Prompt主要实现方式是通过建立相应的语料库和语义解析模型,来将自然语言转换为机器可识别的指令。 Prompt是一种计算机编程语言,它被广泛用于自然语言处理(NLP)和人工智能(AI)领域。
机器学习和人工智能之间的区别
Notes 机器学习 人工智能 LLM
机器学习和人工智能之间的区别
什么是机器学习,机器学习与人工智能(二)机器学习的类型?
Notes 机器学习 人工智能 LLM
什么是机器学习,机器学习与人工智能(二)机器学习的类型?