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

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

原文地址:【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 ChatMessagePromptTemplateprompt = "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 MessagesPlaceholderhuman_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.lryc.cn/news/166343.html

相关文章:

  • ROS2的学习路径
  • Maintaining leader role through timed lease mechanism
  • Mysql InnoDB引擎 的hash索引
  • 23年PMP考试如何备考?
  • mysql数据库增量备份方案、备份计划(InsCode AI 创作助手)
  • 【Flink】FlinkCDC获取mysql数据时间类型差8小时时区解决方案
  • Javas | DecimalFormat类、BigDecimal类、Random类
  • 机器学习 实战系列 总目录
  • 机器学习——贝叶斯(三种分布)/鸢尾花分类分界图/文本分类应用
  • SOLIDWORKS Composer位置关键帧的使用
  • PostgreSQL 流复制搭建与维护
  • 【Redis】关于过期数据清除的一些策略
  • 动态SQL
  • uniapp:OCR识别身份证上传原图失败,问题解决
  • shell循环和函数
  • 京东详情api
  • MySQL最新版8.1.0安装配置教程
  • 5G试题_1
  • 正规股票配资网站的三个明显特点分析
  • 质疑苹果5G信号造假成为闹剧,反而将运营商置于尴尬境地
  • vue 预览zip
  • 人先自辱,而后人辱之
  • web端三维重建算法-colmap++
  • MyBatisPlus(二)基础Mapperr接口:增删改查
  • 基础项目实用案例
  • sprngboot整合kabana
  • PostgreSQL 数据备份恢复
  • 线性代数的本质(七)——特征值和特征向量
  • c语言进阶部分详解(指针初阶)
  • Socks5代理IP在跨境电商与网络游戏中的网络安全应用