Skip to main content

少量镜头提示模板

 少量镜头提示模板


在本教程中,我们将学习如何创建使用少量示例的提示模板。少量镜头提示模板可以从一组示例或示例选择器对象中创建。


使用实例


在本教程中,我们将为带有搜索功能的自问自答配置少量镜头示例。


使用示例集


创建示例集


要开始使用,请创建一个包含少量示例的列表。每个示例都应是一个字典,键是输入变量,值是这些输入变量的值。

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]))

    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 对象。该对象接收少量镜头示例和少量镜头示例的格式。

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

我们将重复使用上一节中的示例集和格式化器。不过,我们不会将示例直接输入到FewShotPromptTemplate对象中,而是将其输入到一个示例选择器(ExampleSelector)对象中。

在本教程中,我们将使用SemanticSimilarityExampleSelector(语义相似性示例选择器)类。该类根据与输入内容的相似度来选择少量的示例。它使用嵌入模型来计算输入和少数镜头示例之间的相似性,并使用向量存储来执行近邻搜索。

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

将示例选择器输入 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?"))

    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?

Comments

Popular posts from this blog

Think in 2B SaaS

这数年2B SaaS创业经历,让我自己从一个对2B SaaS有着盲目疯狂热情的人,变的逐渐趋于冷静。如何推动建立一个健康的SaaS产品和公司,成了我特别想去思考和解决的问题。 这几年,个人和公司一直都是在野蛮生长,销售驱动的模型下,产品,销售,交付,客户成功都在疲于奔命完成财务业绩。在没有坚持跑出一套聚焦于SaaS领域,可以合力的运作体系下,争执和冲突便成了家常便饭。 销售往往因产品没有某个功能,吐槽产品不给力。因为研发不能按时做出一个客户要的功能,吐槽产品无能,因为找不到客户,所以开始盯着老客户的增购业绩,盯着传统实施项目规模带来的业绩收益。 交付因为销售的过度承诺,卖出产品没有的功能,或者实施过程中细节点产品不支持的能力,吐槽销售,吐槽研发,实施过程变得举步维艰。 CS因为交付质量,产品功能,系统服务的稳定性,导致客户关系紧张,出问题后花费大量时间灭火。或者处理因为bug带来的系统数据不一致的问题。在发生了集成对接的情况下,甚至要协调下游各个系统修复数据,带来的服务成本的增长。增购也就自然而然变得困难。 产研觉得产品功能自己都无法进行规划管理,总是被动的被销售和交付过程中客户的紧急需求干扰,仓促设计的结果就是应用架构设计上的疏忽,开发的bug,业务场景的缺失。 上述的过程,可能很多初始的SaaS创业团队都会碰到,特别是2B领域的。协同的不好,矛盾冲突屡屡发生,不产生合力的情况下,为了维持高业绩增长的要求,内部的负担愈发严重,跑的越来越慢,整体的服务质量情况却没有一个明显的提升。由此引发的各个职能的一线的员工的工作感受也越来越差。推诿,委屈,困惑,气忿等负面情绪也与日俱增。 因此,如何科学的构建一个可以上下协同的组织,如何管理需求,如何管理迭代,如何找到目标客户,如何快速的实施落地,如何更好的保持客户粘性,给客户带来持续不断的新鲜感,配合客户业务的发展需要,就需要进行一套科学的管理办法。无序和杂乱只会让增长越来越缓慢。

MAC Homebrew安装了zookeeper 但是stop时报错

执行下面的命令的时候报错,找不到对应路径下的pid文件。 zkserver stop /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/etc/zookeeper/zoo.cfg Stopping zookeeper ... no zookeeper to stop (could not find file /usr/local/var/run/zookeeper/data/zookeeper_server.pid) 查了网上很多地方,很多人说是data路径中的空格问题,但是查了配置文件发现并不是。 错误信息里面说是pid文件找不到路径,所以,怀疑是读写权限或者是别的什么异常原因导致当时pid文件没有生成。 试着用sudo权限重新执行 sudo zkserver restart Password: /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/etc/zookeeper/zoo.cfg /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/etc/zookeeper/zoo.cfg Stopping zookeeper ... no zookeeper to stop (could not find file /usr/local/var/run/zookeeper/data/zookeeper_server.pid) /usr/bin/java ZooKeeper JMX enabled by default 结果在目录下找到了对应的pid文件。 查看pid文件内容,发现就是一个进程id。这时候考虑修改pid文件的内容,把当前停不下来的进程编号写进去。 然后重新执行了zkserver stop命令。 sudo zkserver stop /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/etc/zookeeper/zoo.cfg Stopping zookeeper ... STOPPED 搞定...

FAISS Vector DB 学习笔记(一)

 1. 安装Faiss: 按照Faiss官方资源库提供的安装说明,确保你有必要的依赖和兼容的硬件(如果使用GPU加速)。 2. 2.准备好你的数据: Faiss使用密集向量工作,所以确保你的数据以浮点数组或矩阵的形式表示,其中每一行对应一个向量。确保你的向量的尺寸是一致的。 3. 导入必要的模块: 在你的Python脚本中,导入所需的Faiss模块: ``python import faiss import numpy as np ``` 4. 创建一个索引: 根据你的需要,选择适当的索引类型。下面是一个创建IndexFlatL2索引的例子: ```python d = 128  # Dimension of your vectors index = faiss.IndexFlatL2(d)  # Initialize the index ``` 你可以探索其他的索引类型,比如IndexIVFFlat或IndexHNSW,这取决于你愿意做出的折衷。 5. 为索引添加向量: 一旦你初始化了你的索引,使用`add'方法将你的向量添加到它里面。传入一个NumPy数组或一个兼容的数据结构: ``python vectors = np.random.random((1000, d)).astype('float32')  # Example vectors index.add(vectors)  # Add the vectors to the index ``` 6. 进行相似性搜索: 要为一个查询向量找到最近的邻居,可以使用`search`方法。传入查询向量和需要检索的近邻数量: ``python k = 5  # Number of nearest neighbors to retrieve query_vector = np.random.random((1, d)).astype('float32')  # Example query vector D, I = index.search(query_vector, k)  # Perform the search ``` 返回的`D`数组包含与最近的邻居的距离,`I`数组包含它们在原始数据集中的相应索引。 7. 自定义和优化: Faiss...