当前位置: 首页 > article >正文

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岁
  • 客户所在地:河南省郑州市

http://www.kler.cn/news/341548.html

相关文章:

  • RK3568驱动指南|第十六篇 SPI-第190章 配置模式下寄存器的配置
  • 《Spring Microservices in Action, 2nd Edition》读后总结
  • vue 权限分组
  • 实景三维赋能矿山安全风险监测预警
  • “万万没想到”,“人工智能”获得2024年诺贝尔物理学奖
  • 比较三组迭代次数的变化
  • ElasticSearch之网络配置
  • 独享动态IP是什么?它有什么独特优势吗?
  • 低级语言和高级语言、大小写敏感、静态语言和动态语言、链接
  • ArgoWorkflow教程(六)---无缝实现步骤间参数传递
  • Unity3d使用JsonUtility.FromJson读取json文件
  • MySQL联合索引、索引下推Demo
  • C++笔试题合集-第一阶段
  • 基于STM32的简易交通灯proteus仿真设计(仿真+程序+设计报告+讲解视频)
  • HBase中的Write-Ahead Log 详解
  • 从0打造本地聊天机器人:如何实现大模型流式输出?OpenAI+Ollama 实战
  • 如何设置LTE端到端系统
  • SpringBoot 整合 阿里云 OSS图片上传
  • 04-SpringBootWeb案例(中)
  • SQL Server—通配符(模糊查询)详解