LangChain Core Prompts 提示词模板速查
资料来源:https://reference.langchain.com/python/langchain-core/prompts/
本文只讲最常用的场景:什么时候用哪种模板,以及最小可运行写法。提示词模板的核心作用,就是把固定指令和动态变量分开,避免在代码里到处拼字符串。
1. PromptTemplate:单段文本提示词
适合场景:给普通 LLM 或简单链路传入一整段文本,例如翻译、改写、摘要、分类。
通俗理解:就是带 {变量} 的字符串模板。
python
from langchain_core.prompts import PromptTemplate
# 定义一段可复用的文本提示词,topic 会在运行时填入。
prompt = PromptTemplate.from_template(
"请用三句话解释 {topic},要求通俗、不要使用术语。"
)
result = prompt.invoke({"topic": "向量数据库"})
print(result.text)
什么时候选它:你的模型输入就是一段完整文本,不需要区分 system、human、ai 角色。
2. ChatPromptTemplate:聊天模型提示词
适合场景:调用 Chat Model,例如 ChatOpenAI、init_chat_model 返回的聊天模型,需要区分系统设定、用户问题、历史回答。
通俗理解:把提示词拆成多条消息,每条消息有角色。
python
from langchain_core.prompts import ChatPromptTemplate
# system 负责设定助手身份,human 负责接收用户输入。
prompt = ChatPromptTemplate.from_messages(
[
("system", "你是一个耐心的 Python 老师,只用初学者能懂的话回答。"),
("human", "请解释 {concept},并给一个生活类比。"),
]
)
messages = prompt.invoke({"concept": "装饰器"})
print(messages)
什么时候选它:绝大多数现代聊天模型都优先用它,因为角色清楚,后续也方便接入历史消息、工具调用和 Agent。
3. MessagesPlaceholder:插入一段消息列表
适合场景:把聊天历史、Agent 中间过程、外部保存的消息记录插入 prompt。
通俗理解:模板里先留一个"消息插槽",运行时把多条历史消息塞进去。
python
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
prompt = ChatPromptTemplate.from_messages(
[
("system", "你是一个客服助手,请结合历史对话回答。"),
MessagesPlaceholder("history"),
("human", "{question}"),
]
)
messages = prompt.invoke(
{
"history": [
("human", "我买的订单 123 发货了吗?"),
("ai", "我需要查询订单状态。"),
],
"question": "那现在有结果了吗?",
}
)
print(messages)
什么时候选它:只要有"历史对话"或"一组已有消息"要动态插入,就用它。可以设置 optional=True,没有历史时不报错。
4. FewShotPromptTemplate:文本 few-shot 示例
适合场景:给模型看几个输入输出样例,让它模仿格式,例如分类、抽取、固定文风改写。
通俗理解:先放几个"示范题",最后再问真正的问题。
python
from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate
# 每个示例如何展示。
example_prompt = PromptTemplate.from_template(
"输入:{text}\n标签:{label}"
)
examples = [
{"text": "这个耳机音质很好", "label": "正面"},
{"text": "物流太慢了", "label": "负面"},
]
prompt = FewShotPromptTemplate(
examples=examples,
example_prompt=example_prompt,
prefix="请判断评论情感,只输出标签。",
suffix="输入:{text}\n标签:",
input_variables=["text"],
)
print(prompt.invoke({"text": "包装精致,续航也不错"}).text)
什么时候选它:你希望模型严格模仿"文本格式",但不需要聊天角色。
5. FewShotChatMessagePromptTemplate:聊天版 few-shot 示例
适合场景:聊天模型也要看样例,而且样例本身要区分 human/ai。
通俗理解:给 Chat Model 几轮"用户这么问,AI 这么答"的示范。
python
from langchain_core.prompts import (
ChatPromptTemplate,
FewShotChatMessagePromptTemplate,
)
# 每个样例由一条 human 消息和一条 ai 消息组成。
example_prompt = ChatPromptTemplate.from_messages(
[
("human", "{question}"),
("ai", "{answer}"),
]
)
few_shot = FewShotChatMessagePromptTemplate(
example_prompt=example_prompt,
examples=[
{"question": "苹果属于什么类别?", "answer": "水果"},
{"question": "胡萝卜属于什么类别?", "answer": "蔬菜"},
],
)
prompt = ChatPromptTemplate.from_messages(
[
("system", "请按样例只输出类别。"),
few_shot,
("human", "{question}"),
]
)
print(prompt.invoke({"question": "香蕉属于什么类别?"}))
什么时候选它:你正在用聊天模型,并且想让模型模仿多轮问答样例。
6. SystemMessagePromptTemplate / HumanMessagePromptTemplate / AIMessagePromptTemplate
适合场景:你想显式创建某一种角色消息模板,而不是用 ("system", "...") 这种元组简写。
通俗理解:它们是聊天模板里的"单条消息零件"。
python
from langchain_core.prompts import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
)
system = SystemMessagePromptTemplate.from_template(
"你是 {domain} 领域的助手。"
)
human = HumanMessagePromptTemplate.from_template(
"请回答:{question}"
)
prompt = ChatPromptTemplate.from_messages([system, human])
print(prompt.invoke({"domain": "LangChain", "question": "PromptTemplate 是什么?"}))
什么时候选它:普通代码里用元组简写更方便;当你要复用、组合、单独管理某类消息模板时,再用这些类。
7. ImagePromptTemplate:图片输入模板
适合场景:多模态模型需要图片 URL、图片路径或图片变量,例如"看图描述""根据图片提取信息"。
通俗理解:不是给纯文本模型用的,而是给支持图片的聊天模型准备图片内容。
python
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages(
[
(
"human",
[
{"type": "text", "text": "请描述这张图片中的主要内容。"},
{"type": "image_url", "image_url": {"url": "{image_url}"}},
],
)
]
)
print(prompt.invoke({"image_url": "https://example.com/demo.png"}))
什么时候选它:模型必须支持视觉输入;纯文本任务不要用。
8. DictPromptTemplate:输出字典结构的模板
适合场景:下游 Runnable 需要一个字典,而不是单个字符串或消息列表。
通俗理解:一次格式化出多个字段,交给后面的链路分别使用。
python
from langchain_core.prompts import DictPromptTemplate
prompt = DictPromptTemplate(
template={
"instruction": "请总结 {topic}。",
"language": "{language}",
},
template_format="f-string",
)
print(prompt.invoke({"topic": "LangChain prompts", "language": "中文"}))
什么时候选它:普通提示词很少用;当链路下一步明确需要多个命名字段时再考虑。
选型口诀
| 需求 | 优先选择 |
|---|---|
| 一段文本模板 | PromptTemplate |
| 聊天模型,有 system/human/ai | ChatPromptTemplate |
| 插入历史消息 | MessagesPlaceholder |
| 文本补几个示例 | FewShotPromptTemplate |
| 聊天消息补几个示例 | FewShotChatMessagePromptTemplate |
| 单独复用某个角色消息 | SystemMessagePromptTemplate 等 |
| 多模态图片输入 | ImagePromptTemplate / chat 多模态内容 |
| 一次产出多个字段 | DictPromptTemplate |
日常开发建议:先用 ChatPromptTemplate;只有在纯文本链路才用 PromptTemplate;few-shot、历史消息、多模态都按需加。