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岁
  • 客户所在地:河南省郑州市
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值