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:

python 复制代码
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

python 复制代码
# 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

python 复制代码
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:

python 复制代码
# 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

python 复制代码
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

python 复制代码
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.
相关推荐
ltl6 小时前
内核追踪:ftrace、kprobe、uprobe、tracepoint 生产实战
linux
·薯条大王6 小时前
经济实惠玩云服务器|一台云服务器多人共用,子账号配置教程
java·linux·运维·服务器·汇编·c++·python
ZJH__GO9 小时前
网络编程v4pro--实现聊天室文件传输功能
java·服务器·网络·计算机网络
小小、码农9 小时前
Linux进程控制:从fork到exec,手写一个简易Shell
linux·运维·服务器
程序员黑豆9 小时前
Windows 系统 Java 环境变量配置全攻略:解决“不是内部或外部命令”
java·前端·ai编程
命运之光9 小时前
【C语言完整代码】就诊信息管理系统
java·c语言·开发语言
凤山老林9 小时前
Spring Boot @Async 线上实战:从默认配置到生产级线程池治理
java·spring boot·后端
不会就选b9 小时前
Linux之进程间通信(二)---模拟进程池
linux·运维·服务器
marvelyu10 小时前
每天10分钟学会OceanBase系列(Day 20):跨机房容灾实战——构建多数据中心高可用架构
java·大数据·数据库
AI人工智能+电脑小能手11 小时前
大白话说Java设计模式-04-工厂方法模式(业务实战篇)
java·设计模式·工厂方法模式·架构设计·代码解耦