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

Prompts for Chat Models in LangChain

https://python.langchain.com.cn/docs/modules/model_io/models/chat/how_to/prompts

Prompts for Chat Models in LangChain

This content is based on LangChain’s official documentation (langchain.com.cn) and explains prompts for chat models—which are built around messages (not just plain text)—in simplified terms. It strictly preserves all original source codes, examples, and knowledge points without any additions or modifications.

1. Key Feature of Chat Model Prompts

Prompts for chat models are structured around messages (e.g., system messages, human messages, AI messages) rather than single blocks of text.

  • Use MessagePromptTemplate (and its subclasses like SystemMessagePromptTemplate, HumanMessagePromptTemplate) to create reusable message templates.
  • Combine multiple MessagePromptTemplates into a ChatPromptTemplate.
  • Use ChatPromptTemplate.format_prompt() to generate a PromptValue, which can be converted to a string or message objects (for chat models).

2. Step 1: Import Required Modules

The code below imports all necessary classes—exactly as in the original documentation:

from langchain import PromptTemplate
from langchain.prompts.chat import (ChatPromptTemplate,SystemMessagePromptTemplate,AIMessagePromptTemplate,HumanMessagePromptTemplate,
)

Note: A chat model (e.g., ChatOpenAI) is required to run the final step, but it is not imported in the original documentation—we will reference it as chat (consistent with the original code).

3. Method 1: Create Message Templates with from_template

This is a concise way to build MessagePromptTemplates directly from template strings.

Step 3.1: Create System and Human Message Templates

# System message template: Defines the assistant's role (translator)
template = "You are a helpful assistant that translates {input_language} to {output_language}."
system_message_prompt = SystemMessagePromptTemplate.from_template(template)# Human message template: Defines the user's input (text to translate)
human_template = "{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)

Step 3.2: Combine into ChatPromptTemplate

chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])

Step 3.3: Format and Run the Prompt

Use format_prompt() to fill in the placeholders, convert to messages, and pass to the chat model. The original code and output are preserved exactly:

# Get formatted messages and pass to the chat model
chat(chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming."
).to_messages())

Output (exact as original):

AIMessage(content="J'adore la programmation.", additional_kwargs={})

4. Method 2: Create Message Templates with External PromptTemplate

For more flexibility, you can first define a PromptTemplate and then pass it to SystemMessagePromptTemplate.

Step 4.1: Create an External PromptTemplate

prompt = PromptTemplate(template="You are a helpful assistant that translates {input_language} to {output_language}.",input_variables=["input_language", "output_language"],  # Explicitly list variables
)

Step 4.2: Wrap into SystemMessagePromptTemplate

system_message_prompt = SystemMessagePromptTemplate(prompt=prompt)

Note: You can combine this system message prompt with the same human_message_prompt (from Method 1) into a ChatPromptTemplate and run it—same as Step 3.2 and 3.3.

Key Takeaways

  • Chat model prompts are built with message templates (e.g., SystemMessagePromptTemplate).
  • Two ways to create message templates: from_template (concise) or external PromptTemplate (flexible).
  • ChatPromptTemplate.from_messages() combines multiple message templates.
  • format_prompt().to_messages() converts the template to chat-model-compatible messages.
http://www.dtcms.com/a/605816.html

相关文章:

  • 【音视频】AI自适应均衡器的调节精度提升方法
  • vscode编译C语言 | 在VSCode中配置编译环境与常见问题解决
  • 「腾讯云NoSQL」技术之向量数据库篇:腾讯云向量数据库如何实现召回不变,成本减半?
  • Window系统,Unity编辑器中播放HEVC/H265编码的视频报错问题:WindowsVideoMedia error OxcOOdOO1a
  • 专业集团门户网站建设重庆网站备案必须到核验点
  • PotPlayer播放器下载安装教程 - 媒体观影神器
  • 仿生机器人的SLAM导航
  • Sequelize vs Prisma:现代 Node.js ORM 深度技术解析与实战指南
  • 中国关键词网站广告公司注册条件
  • 第11章、事务
  • Sass环境搭建及使用测试(windows学习环境)
  • Java数据结构:二叉树
  • RabbitMq消费延迟衰减重试实现思路
  • 欧拉22.03系统安装RabbitMQ-3.6.10
  • C语言编译器的选择与优化技巧 | 如何选择适合的C语言编译器以提高代码性能
  • 高通移动:编译成功后,用Unpacking Tool打包,再烧录
  • 文档切片(Document Chunking)
  • AEC-Q100 stress实验详解#5——PTC(功率温度循环)
  • MacOS彻底清除docker及image
  • 【3ds Max动画】烟花:超级喷射粒子,荧光粒子效果
  • 做网站的内容资源广告装饰 技术支持 东莞网站建设
  • 脑机接口核心产业链研发实力:翔宇医疗、三博脑科、汉威科技、科大讯飞、创新医疗,5家龙头公司研发实力深度数据
  • AI驱动与人才争夺战:互联网行业步入新一轮扩张期
  • Java-171 Neo4j 备份与恢复 + 预热与执行计划实战
  • 《信息存储与管理》完整复习手册
  • 西门子1500PLC(模拟器)与Matlab经由Modbus通信联合PID仿真
  • 【LeetCode】110. 平衡二叉树
  • LeetCode 423 - 从英文中重建数字
  • 建设部信息中心网站提供模板网站制作多少钱
  • 徐州集团网站建设关键词排名霸屏代做