AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


一文看懂LangChain中ChatModel怎么用Few-Shot
发布日期:2024-07-20 15:02:39 浏览次数: 1713


    本文将介绍如何在聊天模型中使用示例的输入和输出。为模型提供一些fewshot示例,这样可以简单而直观的指导模型来生成我们想要的结构,在很多情况下这种方法都能显著的提升模型性能。
    本文将通过例子来证明其效果,后面通过环境介绍、模型加载、模板构建、问答对话来展开本文。让我们开始吧。

模型列表

本文采用的模型包括大语言模型和向量模型,语言模型选择的是Qwen2-0.5B-Instruct,向量模型选择的是bge-m3。模型配置如下:

BAAI/bge-m3Qwen/Qwen2-0.5B-Instruct

环境配置

本文将用到的安装包有:

langchain==0.2.10 langchain-core torch faiss-cpu langchain_community

加载模型

我们加载模型用langchain上面的一个问题What is 2 ? 4?,问它看能得到什么结果?

加载OpenAI模型

from langchain_openai import ChatOpenAI, OpenAIEmbeddings
openai_model = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0.0)embeddings = OpenAIEmbeddings()
openai_model.invoke("What is 2 ? 4?")
AIMessage(content='The expression "2 ? 4" is not a standard mathematical operation or equation. It appears to be a combination of the number 2 and the parrot emoji ? followed by the number 9. It does not have a specific mathematical meaning.', response_metadata={'token_usage': {'completion_tokens': 54, 'prompt_tokens': 17, 'total_tokens': 71}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-aad12dda-5c47-4a1e-9949-6fe94e03242a-0', usage_metadata={'input_tokens': 17, 'output_tokens': 54, 'total_tokens': 71})

得到的结果如下:The expression "2 ? 4" is not a standard mathematical operation or equation. It appears to be a combination of the number 2 and the parrot emoji ? followed by the number 9. It does not have a specific mathematical meaning.

加载本地模型:

感兴趣可以参考RAG:Langchain中使用自己的LLM大模型 RAG:在LangChain中使用本地向量embedding模型

from utils import load_custom_model, CustomEmbedding
chat_model = load_custom_model("./Qwen/Qwen2-0.5B-Instruct")embeddings = CustomEmbedding("./ckpt/bge-m3")chat_model.invoke("What is 2 ? 4?")
chat_model.invoke("What is 2 ? 4?")# AIMessage(content='It seems like you're using emojis, but I'm not sure what you mean by "2 ? 4". Could you please provide more context or clarify your question?', id='run-df7ad53b-4423-45f0-9e4e-b88a146f5658-0')chat_model.invoke("中国的春城是哪里?")# AIMessage(content='中国的春城是指昆明。', id='run-5b4ba399-0f87-43f4-b0d3-b45c854a1572-0')

得到的结果如下:It seems like you're using emojis, but I'm not sure what you mean by "2 ? 4". Could you please provide more context or clarify your question?

两个回答都还可以,我们看看fewshot将会产生什么效果呢?

如何加入FewShot

如我们知道,ChatModel的prompt模板是ChatPromptTemplate,当我们需要将fewshot示例插入进去时,我们需要用到FewShotChatMessagePromptTemplate类,它也支持两种加入示例的方法:一种是指定列表examples;一种是示例选择工具example_selector,可以自由选择。

方法一 examples

整体思路是,先将examples拼接成messages,再送入到chatprompttemplate中。直接上代码:

from langchain_core.prompts import ChatPromptTemplate, FewShotChatMessagePromptTemplate
# 示例examples = [{"input": "2 ? 2", "output": "4"},{"input": "2 ? 3", "output": "5"},{"input": "4 ? 5", "output": "9"},{"input": "3 ? 8", "output": "11"}]# 示例的prompt模板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'), AIMessage(content='4'), HumanMessage(content='2 ? 3'), AIMessage(content='5')]
# 示例加入到整个大模型中final_prompt = ChatPromptTemplate.from_messages([("system", "You are a wondrous wizard of math."),few_shot_prompt,("human", "{input}"),])
# 下面是What is 3 ? 5?拼接的结果# ChatPromptValue(messages=[SystemMessage(content='You are a wondrous wizard of math.'), HumanMessage(content='2 ? 2'), AIMessage(content='4'), HumanMessage(content='2 ? 3'), AIMessage(content='5'), HumanMessage(content='4 ? 5'), AIMessage(content='9'), HumanMessage(content='3 ? 8'), AIMessage(content='11'), HumanMessage(content='What is 3 ? 5?')])
chain = final_prompt | chat_model
print(chain.invoke({"input": "What is 2 ? 5?"}))# content='7' id='run-367961cf-ea5b-4c99-bd85-a8494fd3bfe8-0'print(chain.invoke({"input": "What is 3 ? 4?"}))# content='7' id='run-56465db6-4ebc-4efe-9b2c-b9da7ebdec4c-0'print(chain.invoke({"input": "What is 3 ? 3?"}))# content='6' id='run-abc9a040-10fc-41a2-9cd5-8ed365f62b89-0'print(chain.invoke({"input": "What is 3 ? 9?"}))# content='6' id='run-443df1ee-3380-489c-9900-1c43e1e35bb5-0'

看上面的结果模型知道了这个表达是在干什么,可以正确输出答案了。但是0.5B的模型还是有点傻缺,错误的也挺多,大家可以自测试一下。

方法二 example_selector

这个方法与方法一相比,多了一个示例检索的步骤,因此我们需要创建一个检索器。方法复杂一点,但是可以在示例中添加与问题相关的例子。

from langchain_community.vectorstores import FAISSfrom langchain_core.example_selectors import SemanticSimilarityExampleSelector
# 示例examples = [{"input": "2 ? 2", "output": "4"},{"input": "2 ? 3", "output": "5"},{"input": "2 ? 4", "output": "6"},{"input": "中国的首都是哪里?", "output": "北京"},{"input": "中国经济中心是哪里", "output": "上海"}]
# 创建向量检索库to_vectorize = [" ".join(example.values()) for example in examples]vectorstore = FAISS.from_texts(to_vectorize, embeddings, metadatas=examples)
# 创建示例检索器example_selector = SemanticSimilarityExampleSelector(vectorstore=vectorstore,    k=2, # 示例个数)
print(example_selector.select_examples({"input": "中国的春城是哪里"}))# [{'input': '中国的首都是哪里?', 'output': '北京'}, {'input': '中国经济中心是哪里', 'output': '上海'}]
# Create prompt templatefrom langchain_core.prompts import ChatPromptTemplate, FewShotChatMessagePromptTemplate
# Define the few-shot prompt.few_shot_prompt = FewShotChatMessagePromptTemplate(    # 定义example_selector的输入字段为inputinput_variables=["input"],example_selector=example_selector,    # 示例的prompt模板example_prompt=ChatPromptTemplate.from_messages([("human", "{input}"), ("ai", "{output}")]),)
final_prompt = ChatPromptTemplate.from_messages([("system", "你是一个精通中国的ai助手"),few_shot_prompt,("human", "{input}"),]
)

检索结果展示:

print(example_selector.select_examples({"input": "中国的春城是哪里?"}))# [{'input': '中国的首都是哪里?', 'output': '北京'}, {'input': '中国经济中心是哪里', 'output': '上海'}]
print(few_shot_prompt.invoke(input="中国的春城是哪里?").to_messages())# [HumanMessage(content='中国的首都是哪里?'), AIMessage(content='北京'), HumanMessage(content='中国经济中心是哪里'), AIMessage(content='上海')]

对话结果展示

原始模型结果(使用原生的模型):

chat_model.invoke("中国的春城是哪里?")# AIMessage(content='中国的春城是指昆明。', id='run-731aca8a-3a25-41a7-a5f8-52c30a53af2f-0')
chat_model.invoke("东方明珠在哪里")# AIMessage(content='东方明珠位于中国上海市浦东新区,是上海的标志性建筑之一。它高约468米,是世界上最高的电视塔。', id='run-116fe79d-9b3e-4796-9d8e-ee9d2edf9306-0')

使用Few-Shot后的模型:

chain = final_prompt | chat_model
chain.invoke({"input": "中国的春城是哪里?"})# AIMessage(content='昆明', id='run-159c892d-43ff-4350-85bc-7e11e8882b9d-0')
chain.invoke({"input": "东方明珠在哪里"})# AIMessage(content='上海', id='run-3225ba64-23db-4b6b-96ce-a72d49de8af3-0')

从上面的例子可以发现,使用Few-Shot后模型的结果更接近我们的设定了。Few-Shot可以直观的告诉模型,我们想要做什么,要达到什么目的,有效提升模型效果,大家跑起来吧。


53AI,企业落地应用大模型首选服务商

产品:大模型应用平台+智能体定制开发+落地咨询服务

承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

与创始人交个朋友

回到顶部

 
扫码咨询