【LangChain系列 8】Prompt模版——少样本prompt模版(二)

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

本文速读:

  • 固定少样本prompt模版

  • 动态少样本prompt模版

在上篇文章中介绍了少样本模版的基本用法,本文将介绍 对话模型(chat model) 中 少样prompt模版的用法。LangChain封装了一些像FewShotChatMessagePromptTemplate的少样本prompt模版,在此基础上我们可以灵活的设计我们需要的 少样本prompt模版。少样本prompt模版 的目标就是可以根据输入去动态地选择样本,然后将选择的样本格式化到最后的prompt中去。

01 固定少样本prompt模版

最基本的少样本prompt技术是使用固定的prompt样本,所以对于一个少样本prompt模版至少要包含:

  • examples:用于prompt的样本数据

  • example_prompt:将每个样本数据转换成message

话不多说,下面将通过一个示例来介绍 固定少样本prompt模版 的使用。

  1. 导入相关模块
javascript 复制代码
from langchain.prompts import (
    FewShotChatMessagePromptTemplate,
    ChatPromptTemplate,
)
  1. 定义examples
ini 复制代码
examples = [
    {"input": "2+2", "output": "4"},
    {"input": "2+3", "output": "5"},
]
  1. 定义example_prompt
ini 复制代码
# This is a prompt template used to format each individual example.
example_prompt = ChatPromptTemplate.from_messages(
    [
        ("human", "{input}"),
        ("ai", "{output}"),
    ]
)
few_shot_prompt = FewShotChatMessagePromptTemplate(
    example_prompt=example_prompt,
    examples=examples,
)

print(few_shot_prompt.format())

执行代码,输出结果:

yaml 复制代码
  Human: 2+2  AI: 4  Human: 2+3  AI: 5
  1. 拼装最终的prompt
css 复制代码
final_prompt = ChatPromptTemplate.from_messages(
    [        ("system", "You are a wondrous wizard of math."),        few_shot_prompt,        ("human", "{input}"),    ]
)

from langchain.chat_models import ChatAnthropic

chain = final_prompt | ChatAnthropic(temperature=0.0)

chain.invoke({"input": "What's the square of a triangle?"})

ChatAnthropic是一个对话大语言模型,将最终的prompt输入给它,然后得到一个回答。执行代码,输出结果:

vbnet 复制代码
AIMessage(content=' Triangles do not have a "square". A square refers to a shape with 4 equal sides and 4 right angles. Triangles have 3 sides and 3 angles.\n\nThe area of a triangle can be calculated using the formula:\n\nA = 1/2 * b * h\n\nWhere:\n\nA is the area \nb is the base (the length of one of the sides)\nh is the height (the length from the base to the opposite vertex)\n\nSo the area depends on the specific dimensions of the triangle. There is no single "square of a triangle". The area can vary greatly depending on the base and height measurements.', additional_kwargs={}, example=False)

02 动态少样本prompt模版

有时我们可能需要从所有样本中动态选择更加符合要求的样本,此时我们只需要把examples替换成example_selector就可以了;那么动态的少样本prompt模版包含:

  • example_selector:根据输入从所有样本中选择部分样本数据

  • example_prompt:将每个样本数据转换成message

下面将介绍如何使用动态少样本prompt模版。

  1. 导入相关包
javascript 复制代码
from langchain.prompts import SemanticSimilarityExampleSelector
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Chroma
  1. 定义example,并向量化
ini 复制代码
examples = [
    {"input": "2+2", "output": "4"},
    {"input": "2+3", "output": "5"},
    {"input": "2+4", "output": "6"},
    {"input": "What did the cow say to the moon?", "output": "nothing at all"},
    {
        "input": "Write me a poem about the moon",
        "output": "One for the moon, and one for me, who are we to talk about the moon?",
    },
]

to_vectorize = [" ".join(example.values()) for example in examples]
embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_texts(to_vectorize, embeddings, metadatas=examples)
  1. 创建example_selector
bash 复制代码
example_selector = SemanticSimilarityExampleSelector(
    vectorstore=vectorstore,
    k=2,
)

# The prompt template will load examples by passing the input do the `select_examples` method
example_selector.select_examples({"input": "horse"})

    [{'input': 'What did the cow say to the moon?', 'output': 'nothing at all'},
     {'input': '2+4', 'output': '6'}]
  1. 创建prompt模版
ini 复制代码
from langchain.prompts import (
    FewShotChatMessagePromptTemplate,
    ChatPromptTemplate,
)

# Define the few-shot prompt.
few_shot_prompt = FewShotChatMessagePromptTemplate(
    # The input variables select the values to pass to the example_selector
    input_variables=["input"],
    example_selector=example_selector,
    # Define how each example will be formatted.
    # In this case, each example will become 2 messages:
    # 1 human, and 1 AI
    example_prompt=ChatPromptTemplate.from_messages(
        [("human", "{input}"), ("ai", "{output}")]
    ),
)

输出结果:

yaml 复制代码
  Human: 2+3  AI: 5  Human: 2+2  AI: 4

few_shot_prompt会根据input,动态地从examples中去选择合适的example数据。

  1. 拼装最终的prompt
css 复制代码
final_prompt = ChatPromptTemplate.from_messages(
    [        ("system", "You are a wondrous wizard of math."),        few_shot_prompt,        ("human", "{input}"),    ]
)



from langchain.chat_models import ChatAnthropic

chain = final_prompt | ChatAnthropic(temperature=0.0)

chain.invoke({"input": "What's 3+3?"})

运行代码,输出结果:

ini 复制代码
AIMessage(content=' 3 + 3 = 6', additional_kwargs={}, example=False)

本文小结

本文主要介绍了在对话模型(chat model)中,使用少样本prompt模版的两种方式:固定样本和动态样本。动态样本可以根据用户输入,动态地选择合适的样本,最终组成prompt输入给LLM,从而LLM可以更好地理解prompt,给出更加符合要求的答案。

相关推荐
冷水鱼16 小时前
Qoder,不止是编程agent,也是文档神器
人工智能·ai编程
路旁的码农16 小时前
使用LangExtract进行医疗数据提取
人工智能
德育处主任16 小时前
讲真,文心一言X1.1出来后,我骗不到它了!
人工智能·llm·aigc
一如年少模样丶16 小时前
GPT Server 文档
openai·agent·asr·vllm·sglang·lmdeploy·gpt_server
用户479492835691516 小时前
每天都在用大模型,但是你知道temperature、top_p、top_k这些常见参数是做什么的吗?
人工智能·面试·llm
z千鑫16 小时前
【OpenAI】性价比极高的轻量级多模态模型GPT-4.1-mini介绍 + API KEY的使用教程!
人工智能·gpt·ai·语言模型·chatgpt
机器之心16 小时前
苹果发布会:耳机测心率、手表听音乐、iPhone Air超级薄
人工智能·openai
中科岩创16 小时前
某排水涵洞结构安全自动化监测
人工智能·物联网·自动化
mit6.82417 小时前
[网络入侵AI检测] 模型性能评估与报告
人工智能
黄焖鸡能干四碗17 小时前
智慧教育,智慧校园,智慧安防学校建设解决方案(PPT+WORD)
java·大数据·开发语言·数据库·人工智能