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

LangChain —— Prompt Templates —— How to use few shot examples in chat models

文章目录

  • 一、概述
  • 二、固定示例 Fixed Example


一、概述

 本指南介绍了如何使用示例输入和输出提示 chat model。为模型提供几个这样的例子被称为 few-shotting,这是一种简单而强大的方法来指导生成,在某些情况下可以大大提高模型性能。

 对于如何最好地进行 few-shot 提示,似乎没有达成一致意见,最佳提示编译可能会因模型而异。因此,langchain 提供 few-shot 提示模板,如 FewShotChatMessagePromptTemplate,作为一个灵活的起点,我们可以根据需要修改或替换它们

 few-shot 提示模板的目标是根据输入动态选择示例,然后在最终提示中格式化示例以提供模型。

 注意,以下代码示例仅适用于 chat model,因为 FewShotChatMessagePromptTemplates 旨在输出格式化的 chat message,而不是纯字符串。


二、固定示例 Fixed Example

 最基本 (也是最常见) 的 few-shot 提示技术是使用 固定提示示例。通过这种方式,您可以选择一条链,对其进行评估,并避免在生产中担心额外的 moving parts。
 模板的基本组件包括:

  1. examples:要包含在最终提示中的词典示例列表。
  2. example_prompt:通过其 format_messages 方法将每个示例转换为 1条或多条 消息。一个常见的例子是将每个示例转换为一条 human message 和一条 AI message 响应,或者一条 human message 后跟一条 function call message。

 下面是一个简单的演示。首先,定义要包含的示例。让我们给 LLM 一个不熟悉的数学运算符,用“🦜”表情符号。如果我们尝试问模型,该运算的结果是什么,这将会失败:

from langchain_openai import ChatOpenAImodel = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0.0)model.invoke("What is 2 🦜 9?")
"""
AIMessage(content='The expression "2 🦜 9" 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})
"""

 现在让我们看看如果我们给LLM一些例子来使用会发生什么。我们将在下面定义一些:

from langchain_core.prompts import ChatPromptTemplate, FewShotChatMessagePromptTemplateexamples = [{"input": "2 🦜 2", "output": "4"},{"input": "2 🦜 3", "output": "5"},
]

 接下来,将它们组装到 few-shot 提示模板中。

# This is a prompt template used to format each individual example.
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')]
"""

 最后,我们组装如下所示的最终提示,将 few_shot_prompt 直接传递给 from_messages 工厂方法,并将其与模型一起使用:

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

 现在让我们问模型最初的问题,看看它是如何做到的:

from langchain_openai import ChatOpenAIchain = final_prompt | modelchain.invoke({"input": "What is 2 🦜 9?"})
"""
AIMessage(content='11', response_metadata={'token_usage': {'completion_tokens': 1, 'prompt_tokens': 60, 'total_tokens': 61}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-5ec4e051-262f-408e-ad00-3f2ebeb561c3-0', usage_metadata={'input_tokens': 60, 'output_tokens': 1, 'total_tokens': 61})
"""

 我们可以看到,模型现在已经从给定的几个热门例子中推断出鹦鹉表情符号意味着添加!

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • AWS-WAF-Log S3存放,通过Athena查看
  • 51单片机4(reg52头文件介绍)
  • Camunda如何通过外部任务与其他系统自动交互
  • C:数据结构---算法
  • Flask 用 Redis 缓存键值对-实例
  • 【数据结构】数据结构中树的结构:理解与应用
  • 基于RAG大模型的变电站智慧运维-第十届Nvidia Sky Hackathon参赛作品
  • 从课本上面开始学习的51单片机究竟有什么特点,在现在的市场上还有应用吗?
  • C++类和对象基础笔记总结(默认成员函数)
  • Apache Doris:下一代实时数据仓库
  • 阿里云Linux中安装MySQL,并使用navicat连接以及报错解决
  • EasyCVR视频技术:城市电力抢险的“千里眼”,助力抢险可视化
  • SpinalHDL之VHDL 和 Verilog 生成
  • 【2024_CUMCM】时间序列1
  • 【TOOLS】Chrome扩展开发
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • es6(二):字符串的扩展
  • github指令
  • nodejs实现webservice问题总结
  • PHP变量
  • Vim Clutch | 面向脚踏板编程……
  • 番外篇1:在Windows环境下安装JDK
  • 工作手记之html2canvas使用概述
  • 观察者模式实现非直接耦合
  • 看完九篇字体系列的文章,你还觉得我是在说字体?
  • 一个普通的 5 年iOS开发者的自我总结,以及5年开发经历和感想!
  • 栈实现走出迷宫(C++)
  • ​Benvista PhotoZoom Pro 9.0.4新功能介绍
  • # 利刃出鞘_Tomcat 核心原理解析(二)
  • #LLM入门|Prompt#1.7_文本拓展_Expanding
  • #pragma 指令
  • (01)ORB-SLAM2源码无死角解析-(66) BA优化(g2o)→闭环线程:Optimizer::GlobalBundleAdjustemnt→全局优化
  • (CPU/GPU)粒子继承贴图颜色发射
  • (Python) SOAP Web Service (HTTP POST)
  • (安全基本功)磁盘MBR,分区表,活动分区,引导扇区。。。详解与区别
  • (待修改)PyG安装步骤
  • (二)正点原子I.MX6ULL u-boot移植
  • (二十九)STL map容器(映射)与STL pair容器(值对)
  • (分享)自己整理的一些简单awk实用语句
  • (论文阅读26/100)Weakly-supervised learning with convolutional neural networks
  • (牛客腾讯思维编程题)编码编码分组打印下标题目分析
  • (七)MySQL是如何将LRU链表的使用性能优化到极致的?
  • (求助)用傲游上csdn博客时标签栏和网址栏一直显示袁萌 的头像
  • ./include/caffe/util/cudnn.hpp: In function ‘const char* cudnnGetErrorString(cudnnStatus_t)’: ./incl
  • .Net Memory Profiler的使用举例
  • .vimrc 配置项
  • :如何用SQL脚本保存存储过程返回的结果集
  • [ solr入门 ] - 利用solrJ进行检索
  • [100天算法】-不同路径 III(day 73)
  • [Algorithm][动态规划][路径问题][不同路径][不同路径Ⅱ][珠宝的最高价值]详细讲解
  • [Angular] 笔记 20:NgContent
  • [Angular] 笔记 8:list/detail 页面以及@Input
  • [C#]使用深度学习算法opencvsharp部署RecRecNet广角图像畸变矫正校正摄像广角镜头畸变图像
  • [C#基础]说说lock到底锁谁?
  • [C/C++]关于C++11中的std::move和std::forward