🎯 课程主题
ChatPromptTemplate 的四个高级特性:部分变量预填充、消息占位符、可复用模板库、模板组合。
📝 核心知识点
1. 部分变量预填充(partial)
- 概念说明:通过
partial()方法预先填充某些在所有调用中都相同的变量,创建模板的变体。 - 关键细节:
- 调用
partial()后必须用返回值接收,原 template 对象不会被修改。 - 适用于为不同用户或场景创建定制模板。
- 预填充后,剩余变量在
invoke时传入。
- 调用
from langchain_core.prompts import ChatPromptTemplate
template = ChatPromptTemplate.from_messages([
("system", "你是一个{role}"),
("human", "我的任务是{task}")
])
# 预填充固定变量
partial_template = template.partial(role="导游", audience="游客")
# 后续调用只需传剩余变量
result1 = partial_template.invoke({"task": "介绍北京的故宫"})
result2 = partial_template.invoke({"task": "介绍北京的颐和园"})
# 场景化示例:为不同部门创建专用模板
template = ChatPromptTemplate.from_messages([
("system", "你是{role},负责{task}")
])
it_template = template.partial(role="IT技术支持")
sales_template = template.partial(role="销售顾问")
result = it_template.invoke({"task": "解决服务器故障"})
result = sales_template.invoke({"task": "解答客户产品咨询"})
2. 消息占位符
- 概念说明:当不确定消息提示模板使用什么角色,或希望在格式化过程中插入消息列表时使用。
- 关键细节:
- 有两种实现方式:① 元组中的
"placeholder"②MessagesPlaceholder类。 - 占位符对应的是消息列表,而非单条消息。
- 典型应用:多轮对话系统中存储历史消息。
- 有两种实现方式:① 元组中的
方式一:使用 "placeholder" 元组
template = ChatPromptTemplate.from_messages([
("system", "我是一个AI助手"),
("placeholder", "{conversation}")
])
result = template.invoke({
"conversation": [
("human", "你好,请问明天的天气如何?"),
("ai", "明天天气晴朗"),
("human", "后天呢?")
]
})
方式二:使用 MessagesPlaceholder
from langchain_core.prompts import MessagesPlaceholder
template = ChatPromptTemplate.from_messages([
("system", "我是一个AI助手"),
MessagesPlaceholder(variable_name="conversation")
])
result = template.invoke({
"conversation": [
("human", "你好,请问明天的天气如何?"),
("ai", "明天天气晴朗"),
("human", "后天呢?")
]
})
多轮对话历史消息存储示例
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
template = ChatPromptTemplate.from_messages([
("system", "你是一个友好的AI助手"),
MessagesPlaceholder(variable_name="history"),
("human", "{question}")
])
result = template.invoke({
"history": [
("human", "1+2等于几?"),
("ai", "等于3")
],
"question": "这个结果再乘以4呢?"
})
3. 可复用模板库
- 概念说明:将提示词模板集中维护在独立的 Python 模块或文件中,实现模板的集中管理与复用。
- 关键细节:在实际项目中,建议按功能模块(翻译、总结、代码生成等)将模板分别存放在不同的
.py文件中,按需导入使用。
# templates.py
from langchain_core.prompts import ChatPromptTemplate
translation_template = ChatPromptTemplate.from_messages([
("system", "你是一个翻译专家"),
("human", "将以下内容翻译为英文:{text}")
])
# main.py
from templates import translation_template
result = translation_template.invoke({"text": "你好世界"})
4. 模板组合(LangChain 1.0+ 特性)
- 概念说明:LangChain 1.0 后支持两个
ChatPromptTemplate对象直接拼接。 - 关键细节:类似于字符串拼接,可用于动态组装提示词模板。
🏗️ 架构与工作流
原始模板(含全部变量)
│
├── partial() → 预填充模板A(固定变量已赋值) → invoke(剩余变量)
└── partial() → 预填充模板B(固定变量已赋值) → invoke(剩余变量)
消息占位符工作流:
用户问题 → HumanMessage → [消息列表] → 占位符替换 → 模型调用
历史消息 → MessagesPlaceholder ─┘
💻 代码实战
见各知识点中的代码示例。
⚠️ 常见问题与避坑指南
partial()不修改原对象:必须用返回值接收,否则原 template 仍有全部变量,会导致调用时报错"缺失变量"。MessagesPlaceholder注意导入:需从langchain_core.prompts导入,且variable_name要与invoke时的 key 一致。- 占位符对应的是列表:给占位符赋值时传入的是消息列表,而非单条消息。
💡 个人总结与延伸
这四个高级特性是构建复杂 LLM 应用的基础:partial 实现模板参数化定制,MessagesPlaceholder 解决多轮对话历史注入问题,模板库实现工程化管理。其中消息占位符在多轮对话场景中尤为重要,是后续 Agent 和对话系统的基础。