LangChain使用few shot template
1.导包
from langchain_openai import ChatOpenAI
from langchain.schema import HumanMessage
from langchain.prompts import FewShotChatMessagePromptTemplate, ChatPromptTemplate
2.设置提示模板
example_prompt = ChatPromptTemplate.from_messages(
[
("human", "格式化以下客户信息:\n姓名 -> {customer_name}\n年龄 -> {customer_age}\n 城市 -> {customer_city}"),
("ai", "##客户信息\n- 客户姓名:{formatted_name}\n- 客户年龄:{formatted_age}\n- 客户所在地:{formatted_city}")
]
)
3.输入值
examples = [
{
"customer_name": "张三",
"customer_age": "27",
"customer_city": "长沙",
"formatted_name": "张三",
"formatted_age": "27岁",
"formatted_city": "湖南省长沙市"
},
{
"customer_name": "李四",
"customer_age": "42",
"customer_city": "广州",
"formatted_name": "李四",
"formatted_age": "42岁",
"formatted_city": "广东省广州市"
},
]
4.输入值进模板
few_shot_template = FewShotChatMessagePromptTemplate(
example_prompt=example_prompt,
examples=examples,
)
5.小样本模板
few_shot_template
FewShotChatMessagePromptTemplate(examples=[{‘customer_name’: ‘张三’, ‘customer_age’: ‘27’, ‘customer_city’: ‘长沙’, ‘formatted_name’: ‘张三’, ‘formatted_age’: ‘27岁’, ‘formatted_city’: ‘湖南省长沙市’}, {‘customer_name’: ‘李四’, ‘customer_age’: ‘42’, ‘customer_city’: ‘广州’, ‘formatted_name’: ‘李四’, ‘formatted_age’: ‘42岁’, ‘formatted_city’: ‘广东省广州市’}], example_prompt=ChatPromptTemplate(input_variables=[‘customer_age’, ‘customer_city’, ‘customer_name’, ‘formatted_age’, ‘formatted_city’, ‘formatted_name’], messages=[HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=[‘customer_age’, ‘customer_city’, ‘customer_name’], template=‘格式化以下客户信息:\n姓名 -> {customer_name}\n年龄 -> {customer_age}\n 城市 -> {customer_city}’)), AIMessagePromptTemplate(prompt=PromptTemplate(input_variables=[‘formatted_age’, ‘formatted_city’, ‘formatted_name’], template=‘##客户信息\n- 客户姓名:{formatted_name}\n- 客户年龄:{formatted_age}\n- 客户所在地:{formatted_city}’))]))
6.设置最终模板
final_prompt_template = ChatPromptTemplate.from_messages(
[
few_shot_template,
("human", "{input}"),
]
)
7.输出最终模板
final_prompt_template
ChatPromptTemplate(input_variables=[‘input’], messages=[FewShotChatMessagePromptTemplate(examples=[{‘customer_name’: ‘张三’, ‘customer_age’: ‘27’, ‘customer_city’: ‘长沙’, ‘formatted_name’: ‘张三’, ‘formatted_age’: ‘27岁’, ‘formatted_city’: ‘湖南省长沙市’}, {‘customer_name’: ‘李四’, ‘customer_age’: ‘42’, ‘customer_city’: ‘广州’, ‘formatted_name’: ‘李四’, ‘formatted_age’: ‘42岁’, ‘formatted_city’: ‘广东省广州市’}], example_prompt=ChatPromptTemplate(input_variables=[‘customer_age’, ‘customer_city’, ‘customer_name’, ‘formatted_age’, ‘formatted_city’, ‘formatted_name’], messages=[HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=[‘customer_age’, ‘customer_city’, ‘customer_name’], template=‘格式化以下客户信息:\n姓名 -> {customer_name}\n年龄 -> {customer_age}\n 城市 -> {customer_city}’)), AIMessagePromptTemplate(prompt=PromptTemplate(input_variables=[‘formatted_age’, ‘formatted_city’, ‘formatted_name’], template=‘##客户信息\n- 客户姓名:{formatted_name}\n- 客户年龄:{formatted_age}\n- 客户所在地:{formatted_city}’))])), HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=[‘input’], template=‘{input}’))])
8.为最终模板设置值
final_prompt = final_prompt_template.invoke({"input": "格式化以下客户信息:\n姓名 -> 王五\n年龄 -> 31\n 城市 -> 郑州'"})
9.输出最终模板
final_prompt.messages
[HumanMessage(content=‘格式化以下客户信息:\n姓名 -> 张三\n年龄 -> 27\n 城市 -> 长沙’),
AIMessage(content=‘##客户信息\n- 客户姓名:张三\n- 客户年龄:27岁\n- 客户所在地:湖南省长沙市’),
HumanMessage(content=‘格式化以下客户信息:\n姓名 -> 李四\n年龄 -> 42\n 城市 -> 广州’),
AIMessage(content=‘##客户信息\n- 客户姓名:李四\n- 客户年龄:42岁\n- 客户所在地:广东省广州市’),
HumanMessage(content=“格式化以下客户信息:\n姓名 -> 王五\n年龄 -> 31\n 城市 -> 郑州’”)]
10.输入模型
model = ChatOpenAI(model="gpt-3.5-turbo",base_url="https://api.chatanywhere.tech/v1")
response = model.invoke(final_prompt)
print(response.content)
##客户信息
- 客户姓名:王五
- 客户年龄:31岁
- 客户所在地:河南省郑州市