3.langchain中的prompt模板 (few shot examples in chat models)

本教程将介绍如何使用LangChain库和智谱清言的 GLM-4-Plus 模型来理解和推理一个自定义的运算符(例如使用鹦鹉表情符号🦜)。我们将通过一系列示例来训练模型,使其能够理解和推断该运算符的含义。

环境准备

首先,确保你已经安装了以下库:

  • langchain_openai
  • langchain_core
  • langchain_chroma
  • langchain_community

你可以使用以下命令进行安装:

bash 复制代码
pip install langchain_openai langchain_core langchain_chroma langchain_community

第一步:初始化模型

首先,初始化OpenAI的Chat模型。

python 复制代码
from langchain_openai import ChatOpenAI

model = ChatOpenAI(
    temperature=0,
    model="GLM-4-Plus",
    openai_api_key="your api key",
    openai_api_base="https://open.bigmodel.cn/api/paas/v4/"
)

第二步:初步测试模型

测试一下模型对自定义运算符的理解。

python 复制代码
response = model.invoke("What is 2 🦜 9?")
print(response.content)

输出结果:

复制代码
The notation "2 🦜 9" is not a standard mathematical or widely recognized symbol. It appears to be using an emoji (a parrot) in place of a traditional operator. Without additional context or clarification, it's difficult to determine the intended meaning.

However, if we consider the parrot emoji as a playful or whimsical substitute for a standard mathematical operation, we might speculate on a few possibilities:

1. **Repetition or Iteration**: Parrots are known for repeating sounds. If the parrot emoji is meant to imply repetition, "2 🦜 9" could be interpreted as repeating the number 2 nine times, resulting in "222222222".

2. **Multiplication**: If the parrot is whimsically standing in for a multiplication sign, "2 🦜 9" could mean \(2 \times 9 = 18\).

3. **Concatenation**: If the parrot is meant to indicate combining the numbers, "2 🦜 9" could simply mean concatenating 2 and 9 to form the number 29.

Without further context, these are just educated guesses. If you have more information about the context or the specific rules governing the use of the parrot emoji in this notation, please provide it for a more accurate interpretation.

第三步:构建示例提示

为了让模型更好地理解自定义运算符,提供一些示例。

python 复制代码
from langchain_core.prompts import ChatPromptTemplate, FewShotChatMessagePromptTemplate

examples = [
    {"input": "2 🦜 2", "output": "4"},
    {"input": "2 🦜 3", "output": "5"},
]

example_prompt = ChatPromptTemplate.from_messages(
    [
        ("human", "{input}"),
        ("ai", "{output}"),
    ]
)
few_shot_prompt = FewShotChatMessagePromptTemplate(
    example_prompt=example_prompt,
    examples=examples,
)

print(few_shot_prompt.invoke({}).to_messages())

输出结果:

复制代码
[HumanMessage(content='2 🦜 2', additional_kwargs={}, response_metadata={}), AIMessage(content='4', additional_kwargs={}, response_metadata={}), HumanMessage(content='2 🦜 3', additional_kwargs={}, response_metadata={}), AIMessage(content='5', additional_kwargs={}, response_metadata={})]

第四步:构建最终提示

将示例提示与系统提示结合起来,构建最终的提示。

python 复制代码
final_prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a wondrous wizard of math."),
        few_shot_prompt,
        ("human", "{input}"),
    ]
)

第五步:链式调用模型

我们将最终提示与模型结合起来,进行链式调用。

python 复制代码
chain = final_prompt | model

response = chain.invoke({"input": "What is 2 🦜 9?"})
print(response.content)

输出结果:

复制代码
The symbol "🦜" seems to represent an operation, and based on the previous examples:

- 2 🦜 2 = 4
- 2 🦜 3 = 5

It appears that the operation "🦜" adds the second number to the first number. So, following this pattern:

2 🦜 9 = 2 + 9 = 11

Therefore, 2 🦜 9 is 11.

第六步:使用语义相似性选择示例

为了进一步提高模型的推理能力,我们可以使用语义相似性来选择最相关的示例。

python 复制代码
from langchain_chroma import Chroma
from langchain_core.example_selectors import SemanticSimilarityExampleSelector

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

from langchain_community.embeddings import ZhipuAIEmbeddings

to_vectorize = [" ".join(example.values()) for example in examples]
embeddings = ZhipuAIEmbeddings(
    model="embedding-3",
    api_key="your api key",
)
vectorstore = Chroma.from_texts(to_vectorize, embeddings, metadatas=examples)

example_selector = SemanticSimilarityExampleSelector(
    vectorstore=vectorstore,
    k=2,
)

selected_examples = example_selector.select_examples({"input": "What's 3 🦜 3?"})
print(selected_examples)

输出结果:

复制代码
[{'input': '2 🦜 3', 'output': '5'}, {'input': '2 🦜 4', 'output': '6'}]

第七步:构建新的提示并调用模型

使用选定的示例构建新的提示,并调用模型。

python 复制代码
from langchain_core.prompts import ChatPromptTemplate, FewShotChatMessagePromptTemplate

# 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,

    example_prompt=ChatPromptTemplate.from_messages(
        [("human", "{input}"), ("ai", "{output}")]
    ),
)

print(few_shot_prompt.invoke(input="What's 3 🦜 3?").to_messages())

输出结果:

复制代码
[HumanMessage(content='2 🦜 3', additional_kwargs={}, response_metadata={}), AIMessage(content='5', additional_kwargs={}, response_metadata={}), HumanMessage(content='2 🦜 4', additional_kwargs={}, response_metadata={}), AIMessage(content='6', additional_kwargs={}, response_metadata={})]
python 复制代码
final_prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a wondrous wizard of math."),
        few_shot_prompt,
        ("human", "{input}"),
    ]
)

print(final_prompt.invoke(input="What's 3 🦜 3?").to_messages())

输出结果:

复制代码
[SystemMessage(content='You are a wondrous wizard of math.', additional_kwargs={}, response_metadata={}), HumanMessage(content='2 🦜 3', additional_kwargs={}, response_metadata={}), AIMessage(content='5', additional_kwargs={}, response_metadata={}), HumanMessage(content='2 🦜 4', additional_kwargs={}, response_metadata={}), AIMessage(content='6', additional_kwargs={}, response_metadata={}), HumanMessage(content="What's 3 🦜 3?", additional_kwargs={}, response_metadata={})]
python 复制代码
chain = final_prompt | model

print(chain.invoke({"input": "What's 3 🦜 3?"}).content)

输出结果:

复制代码
It seems like the "🦜" symbol is being used as an operation, but its specific rules aren't standard in mathematics. Based on the previous examples:

- 2 🦜 3 = 5
- 2 🦜 4 = 6

One possible interpretation is that "🦜" might represent an operation where you add the two numbers together. Following this pattern:

- 3 🦜 3 would be 3 + 3 = 6

So, 3 🦜 3 = 6. However, if there's a different rule or context for this operation, please provide more details for a precise answer!

参考链接:https://python.langchain.com/docs/how_to/few_shot_examples_chat/

希望这个教程对你有所帮助!如果有任何问题,欢迎随时提问。

相关推荐
BIOS菜鸟工程师几秒前
UEFI Spec 学习笔记---12 - Protocols —CONSOLE SUPPORT(一)
人工智能·笔记·学习
王会举40 分钟前
让SQL飞起来:搭建企业AI应用的SQL性能优化实战
数据库·人工智能·ai·性能优化
大海里的番茄40 分钟前
告别昂贵语音合成服务!用GPT-SoVITS生成你的个性化AI语音
人工智能·gpt
LitchiCheng1 小时前
Qwen2.5-VL视觉大语言模型复现过程,没碰到什么坑
人工智能·语言模型·自然语言处理
白熊1881 小时前
【计算机视觉】OpenCV实战项目-AdvancedLaneDetection 车道检测
人工智能·opencv·计算机视觉
Ac157ol1 小时前
《基于神经网络实现手写数字分类》
人工智能·深度学习·神经网络·机器学习·cnn
好看资源平台1 小时前
神经隐写术与量子加密:AI生成图像的隐蔽传输——突破数字水印新维度
人工智能
Hongs_Cai1 小时前
机器学习简介
人工智能·机器学习
机器之心1 小时前
Jeff Dean演讲回顾LLM发展史,Transformer、蒸馏、MoE、思维链等技术都来自谷歌
人工智能
强化学习与机器人控制仿真1 小时前
ROS & ROS2 机器人深度相机激光雷达多传感器标定工具箱入门教程(一)
开发语言·人工智能·stm32·深度学习·机器人·自动驾驶