【LangChain系列 9】Prompt模版——MessagePromptTemplate

news2025/5/25 0:06:58

原文地址:【LangChain系列 9】Prompt模版——MessagePromptTemplate

本文速读:

  • MessagePromptTemplate

  • MessagesPlaceholder

在对话模型(chat model) 中, prompt主要是封装在Message中,LangChain提供了一些MessagePromptTemplate,方便我们直接使用Message生成prompt。

01 MessagePromptTemplate


LangChain提供了几种类别的MessagePromptTemplate,比较常见的有:

  • AIMessagePromptTemplate

  • SystemMessagePromptTemplate

  • HumanMessagePromptTemplate

上面3种分别表示固定某种角色的Message模版,如果你需要自己来指定任意角色的话可以用ChatMessagePromptTemplate,这样就可以指定角色的名称,比如下面的代码,指定了角色名称为 Jedi。

from langchain.prompts import ChatMessagePromptTemplate

prompt = "May the {subject} be with you"

chat_message_prompt = ChatMessagePromptTemplate.from_template(role="Jedi", template=prompt)
chat_message_prompt.format(subject="force")
ChatMessage(content='May the force be with you', additional_kwargs={}, role='Jedi')

02 MessagesPlaceholder


同时,LangChain还为Message提供了占用符,我们可以使用MessagesPlaceholder来作为Message在占位符,这样我们可以根据实际的需要,在格式化prompt的时候动态地插入Message。

from langchain.prompts import MessagesPlaceholder

human_prompt = "Summarize our conversation so far in {word_count} words."
human_message_template = HumanMessagePromptTemplate.from_template(human_prompt)

chat_prompt = ChatPromptTemplate.from_messages([MessagesPlaceholder(variable_name="conversation"), human_message_template])

human_message = HumanMessage(content="What is the best way to learn programming?")
ai_message = AIMessage(content="""\
1. Choose a programming language: Decide on a programming language that you want to learn.

2. Start with the basics: Familiarize yourself with the basic programming concepts such as variables, data types and control structures.

3. Practice, practice, practice: The best way to learn programming is through hands-on experience\
""")

chat_prompt.format_prompt(conversation=[human_message, ai_message], word_count="10").to_messages()
[HumanMessage(content='What is the best way to learn programming?', additional_kwargs={}),
 AIMessage(content='1. Choose a programming language: Decide on a programming language that you want to learn. \n\n2. Start with the basics: Familiarize yourself with the basic programming concepts such as variables, data types and control structures.\n\n3. Practice, practice, practice: The best way to learn programming is through hands-on experience', additional_kwargs={}),
 HumanMessage(content='Summarize our conversation so far in 10 words.', additional_kwargs={})]

比如在上述代码中,在chat_prompt中定义了一个名为conversation的Message占位符,然后当chat_prompt调用format方法的时候,动态地将human_message,ai_message插入到占位符位置,从而替换占位符。

本文小结

MessagePromptTemplate在对话模型有着非常重要的作用,可以通过它来生成prompt;同时还可以通过MessagesPlaceholder实现占位符功能。

 更多最新文章,请关注公众号:大白爱爬山

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1010619.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

入门必读:Python try except异常处理详解

Python 中,用try except语句块捕获并处理异常,其基本语法结构如下所示: try: 可能产生异常的代码块 except [ (Error1, Error2, ... ) [as e] ]: 处理异常的代码块1 except [ (Error3, Error4, ... ) [as e] ]: 处理异常的代码块…

CSAPP Bomb Lab

CSAPP Bomb Lab 芜湖,完成实验来记录一下啦~ 这个lab看的我真是眼皮发麻,框框就是反汇编->c语言形式->优化c语言 最后我才能写出来,整的我睡觉都是 mov… lea… 难道适中,感觉只要耐心一点就没问题,好了现在开…

【鸿蒙(HarmonyOS)】List列表、ArkUI资源组数据类型

一、引言 描述:List列表在移动端设备中最为常见。比如:通讯录、短信、聊天软件等都,都拥有他的身影。难度:简单知识点:1、列表组件的使用2、认识ArkUI资源组数据类型 二、列表List 1、发现问题(Bug&…

2023-9-14 石子合并

题目链接&#xff1a;石子合并 #include <iostream> #include <algorithm>using namespace std;const int N 310, INF 1e9;int n; // 前缀和 int s[N]; int f[N][N];int main() {cin >> n;for(int i 1; i < n; i ) cin >> s[i];for(int i 1; i …

模式分类与“组件协作模式”

1. GOF-23 模式分类 从目的来看&#xff1a; 创建型&#xff08;Creational&#xff09;模式&#xff1a;将对象的部分创建工作延迟到子类或者其他对象&#xff0c;从而应对需求变化为对象创建时具体类型实现引来的冲击。结构型&#xff08;Structural&#xff09;模式&#…

CS5366 typec转HDMI 4k60拓展坞方案芯片,带PD快充功能

CS5366是一个高性能的USB类型-c/显示端口到HDMI2.0转换器方案芯片&#xff0c;CS5366集成了一个DP1.4兼容的接收器&#xff0c;一个HDMI2.0兼容的发射器。此外&#xff0c;还包括两个CC控 制器&#xff0c;用于CC通信&#xff0c;以实现DP Alt模式和电源传输功能&#xff0c;一…

[设计模式] 浅谈SOLID设计原则

目录 单一职责原则开闭原则里氏替换原则接口隔离原则依赖倒转原则 SOLID是一个缩写词&#xff0c;代表以下五种设计原则 单一职责原则 Single Responsibility Principle, SRP开闭原则 Open-Closed Principle, OCP里氏替换原则 Liskov Substitution Principle, LSP接口隔离原则 …

怎样做一个简易而温馨的原木风居室空间

由 balbek bureau 设计的 Relogged 是一座重新设计的私人住宅&#xff0c;位于乌克兰河岸的绿化区。顾名思义&#xff0c;该项目重新诠释了木屋的概念&#xff0c;并与充满自然气息的环境相呼应&#xff0c;营造出宁静舒适的生活氛围。在探索重新设计的木屋实例时&#xff0c;建…

【rar技巧】安装了WinRAR,右键没有压缩选项,怎么回事?

我们安装了WinRAR之后想要压缩文件&#xff0c;但是右键点击文件之后发现并没有WinRAR压缩选项&#xff0c;这应该如何设置才能出现右键带有压缩选项呢&#xff1f;方法如下&#xff1a; 首先打开WinRAR&#xff0c;在上面功能中点击选项 – 设置 然后我们在设置界面中切换到集…

unity C#客户端与服务器程序

客户端和服务器公共的脚本 OSC.cs // This is version 1.01(2015.05.27) // Tested in Unity 4 // Most of the code is based on a library for the Make Controller Kit1/* using UnityEngine; using System; using System.Collections; using System.Threading; using Syst…

webpack 基础配置

常见配置 文件打包的出口和入口webpack如何开启一台服务webpack 如何打包图片&#xff0c;静态资源等。webpack 配置 loader配置 plugin配置sourceMap配置 babel 语法降级等 接下来 &#xff0c; 我们先从webpack的基本配置 开始吧&#xff01; 在准备 配置之前 , 搭建一个 …

PCL 生成空间三角形面点云

系列文章目录 第一章&#xff1a;PCL生成线段点云 第二章&#xff1a;PCL创建圆柱面点云 文章目录 系列文章目录前言一、三角形面是什么&#xff1f;二、三角形面点云创建步骤1.引入库2.创建三角形面点云 总结 前言 点云库 (PCL) 是一个独立的、大规模的、开放的 2D/3D 图像和…

nginx部署vue后显示500 Internal Server Error解决方案

今天部署vue前端项目一直报错500&#xff0c;无法显示出主页面。 一个以为是自己的dist位置没有访问正确或者nginx.conf的位置写的不对&#xff0c;&#xff0c;最后才发现&#xff01;是权限的问题&#xff01;&#xff01; 我要访问/home/ubuntu/myapp/ruoyi/ruoyi-ui/dist的…

python环境安装(windows)

1. python安装 一、官网下载安装包 打开python的 官网链接 https://www.python.org鼠标点击Downloads按钮 点击downloads后&#xff0c;下拉&#xff0c;找到如下界面&#xff0c;里面就是python的各个历史版本 找到想要下载的版本&#xff0c;点击Download后&#xff0c;下拉…

AI绘画Stable Diffusion原理之扩散模型DDPM

前言 传送门&#xff1a; stable diffusion&#xff1a;Git&#xff5c;论文 stable-diffusion-webui&#xff1a;Git Google Colab Notebook部署stable-diffusion-webui&#xff1a;Git kaggle Notebook部署stable-diffusion-webui&#xff1a;Git AI绘画&#xff0c;输入一段…

热缩膜机效果不好怎么办?

热缩膜机也称热收缩机或收缩包装机&#xff0c;主要是采用加热的原理对物品表面的薄膜进行加热收缩&#xff0c;多用于食品、药品、化妆品、纸箱等产品的外包装&#xff0c;使用过收缩机的用户都知道&#xff0c;热收缩机效果不好是常出现的问题&#xff0c;下面就从3个方面和您…

EndNote+有道

EndNote里面有划线翻译的功能&#xff0c;前提是你的电脑里面安装了有道翻译或者百度翻译的客户端。 我更喜欢有道&#xff0c;所以...... 然后点击“快速安装”。完了之后打开如下&#xff1a; 实现翻译的操作如下&#xff1a; ok。

OJ练习第172题——可以攻击国王的皇后

可以攻击国王的皇后 力扣链接&#xff1a;1222. 可以攻击国王的皇后 题目描述 在一个 8x8 的棋盘上&#xff0c;放置着若干「黑皇后」和一个「白国王」。 给定一个由整数坐标组成的数组 queens &#xff0c;表示黑皇后的位置&#xff1b;以及一对坐标 king &#xff0c;表…

Element UI 表单验证规则动态失效问题

Element 版本&#xff1a;v2.15.3 问题背景 如下代码所示&#xff1a;有一个上传文件的 input 组件&#xff0c;在更新的时候&#xff0c;如果不上传文件表示不更新&#xff0c;如果要更新则点击 「重新上传」按钮将上传组件显示出来 <el-form ref"form" :mode…

计算机网络:三次握手与四次挥手

摘取作者&#xff1a;拓跋阿秀 三次握手 三次握手&#xff08;Three-way Handshake&#xff09;其实就是指建立一个TCP连接时&#xff0c;需要客户端和服务器总共发送3个包。进行三次握手的主要作用就是为了确认双方的接收能力和发送能力是否正常、指定自己的初始化序列号为后…