🎯 课程主题
ChatPromptTemplate 初始化时支持 6 种参数类型,涵盖从字符串到嵌套模板的全部使用场景。
📝 核心知识点
1. 字符串列表
- 概念说明:在
from_messages的列表中直接传入字符串。 - 关键细节:
- LangChain 会自动将字符串当作 用户消息(HumanMessage) 处理。
- 字符串中可以使用
{变量名}方式进行变量占位。
from langchain_core.prompts import ChatPromptTemplate
template = ChatPromptTemplate.from_messages([
"你好,我是{name}"
])
result = template.invoke({"name": "小明"})
2. 元组列表((role, content))
- 概念说明:使用二元组明确指定消息角色和内容。
- 关键细节:
- 格式为
(role, content),role 可以是"system"、"human"、"ai"等。 - 等价于上一种写法,但角色更明确。
- 格式为
template = ChatPromptTemplate.from_messages([
("system", "你是一个友好的AI助手"),
("human", "你好,我是{name}")
])
3. 字典列表({"role": ..., "content": ...})
- 概念说明:使用字典格式声明消息。
- 关键细节:结构与
invoke时的字典格式一致,适合程序化生成。
template = ChatPromptTemplate.from_messages([
{"role": "system", "content": "你是一个友好的AI助手"},
{"role": "human", "content": "你好,我是{name}"}
])
4. 消息对象列表
- 概念说明:直接传入
SystemMessage、HumanMessage等消息对象。 - 关键细节:
- 消息对象中不能声明变量:变量不会被解析,会原样输出。
- 若需变量,应使用元组列表、字典列表或
BaseMessagePromptTemplate。
from langchain_core.messages import SystemMessage, HumanMessage
template = ChatPromptTemplate.from_messages([
SystemMessage(content="你是一个友好的AI助手"),
HumanMessage(content="你好,我是小明")
])
# invoke 时传入空字典或不需要的变量
result = template.invoke({})
5. BaseMessagePromptTemplate 列表
- 概念说明:使用
SystemMessagePromptTemplate、HumanMessagePromptTemplate等子类。 - 关键细节:
- 通过
from_template方法创建,内部可声明变量。 - 是解决"消息对象不能放变量"问题的方案之一。
- 通过
from langchain_core.prompts import (
SystemMessagePromptTemplate,
HumanMessagePromptTemplate
)
system_tpl = SystemMessagePromptTemplate.from_template("你是一个{role}")
human_tpl = HumanMessagePromptTemplate.from_template("你好,我是{name}")
template = ChatPromptTemplate.from_messages([
system_tpl,
human_tpl
])
result = template.invoke({"role": "AI助手", "name": "小明"})
6. ChatPromptTemplate 嵌套(BaseChatPromptTemplate 列表)
- 概念说明:在
from_messages列表中再嵌套一个ChatPromptTemplate对象。 - 关键细节:支持模板的灵活组合与复用。
inner_template = ChatPromptTemplate.from_messages([
("system", "你是一个{role}")
])
outer_template = ChatPromptTemplate.from_messages([
inner_template,
("human", "你好,我是{name}")
])
result = outer_template.invoke({"role": "AI助手", "name": "小明"})
7. 综合使用示例
- 概念说明:在实际项目中,可将以上多种类型混合使用。
- 关键细节:不同参数类型可并存于同一个
from_messages列表中。
from langchain_core.messages import SystemMessage
from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate
human_tpl = HumanMessagePromptTemplate.from_template("我是{name}")
inner = ChatPromptTemplate.from_messages([
("system", "你是一个{role}")
])
template = ChatPromptTemplate.from_messages([
SystemMessage(content="系统启动"),
human_tpl,
inner
])
result = template.invoke({"name": "小明", "role": "翻译助手"})
🏗️ 架构与工作流
from_messages 与 __init__ 方法的参数类型完全相同。底层均接受 Sequence[MessageLikeRepresentation] 类型的参数,其中 MessageLikeRepresentation 是一个联合类型,涵盖了上述 6 种表示形式。
💻 代码实战
见上方各知识点的代码示例。
⚠️ 常见问题与避坑指南
- 消息对象不能声明变量:
HumanMessage(content="你好{name}")中的{name}不会被解析为变量,会原样输出。如需变量,改用元组、字典或MessagePromptTemplate。 - 不要混淆各种表示形式的写法:字典用
{"role": ..., "content": ...},元组用("role", "content")。
💡 个人总结与延伸
6 种参数类型提供了从简单到复杂的灵活度。日常开发中元组和字典列表最常用;当模板需要复用或嵌套时,使用 MessagePromptTemplate 和 ChatPromptTemplate 的组合是推荐做法。