PromptTemplate, ChatPromptTemplate, FewShotPromptTemplate, FewShotChatMessagePromptTemplate
这四个组件构成了 LangChain 提示词系统的核心,它们可以看作一个从“基础”到“高级”、从“纯文本”到“对话”的递进体系。理解它们的区别与联系,是构建高效 LLM 应用的关键。
📝 1.PromptTemplate:纯文本提示词的基础
PromptTemplate是最基础的模板,专为纯文本补全模型(text-in, text-out)设计,其输出是一个单一的格式化字符串。
- 核心参数:
template:包含{variable}占位符的模板字符串。input_variables:一个字符串列表,显式声明模板中需要被填充的变量名。
- 典型用法:
fromlangchain_core.promptsimportPromptTemplate template=PromptTemplate.from_template("Write a {adjective} poem about {topic}.")prompt_string=template.format(adjective="funny",topic="AI")# 输出: "Write a funny poem about AI." - 适用场景:简单的文本生成、分类、摘要等单轮任务,不涉及对话角色或复杂历史上下文。
💬 2.ChatPromptTemplate:对话模型的结构化模板
ChatPromptTemplate专为聊天模型设计,其输出是一个消息列表(List[BaseMessage])。它通过为每条消息指定角色(如system,human,ai),来构建结构化的对话输入。
- 核心参数:
messages:一个消息模板列表,通常以元组(role, template)的形式提供,角色包括'system','human','ai'等。- 也支持使用
MessagesPlaceholder来动态插入对话历史。
- 典型用法:
fromlangchain_core.promptsimportChatPromptTemplate template=ChatPromptTemplate.from_messages([("system","You are a helpful assistant named {name}."),("human","{user_input}"),])prompt_value=template.invoke({"name":"Bob","user_input":"What's your name?"})# prompt_value 是一个包含 SystemMessage 和 HumanMessage 的 ChatPromptValue 对象 - 适用场景:几乎所有与主流聊天模型(GPT, Claude等)的交互,特别是需要设定系统人设、构建多轮对话、RAG 或 Agent 的应用。
🎯 3.FewShotPromptTemplate:为纯文本模型注入示例
FewShotPromptTemplate在PromptTemplate的基础上,通过注入少量示例来引导模型学习新任务或输出格式。它最终生成的仍然是一个格式化后的长字符串,其中包含了所有示例。
- 核心参数:
examples:一个示例字典的列表,例如[{"input": "...", "output": "..."}, ...]。example_prompt:一个PromptTemplate,用于定义单个示例的格式化方式。prefix/suffix:分别放在示例列表前后的固定文本,用于提供任务说明和包裹用户的实际输入。input_variables:最终用户输入中需要被填充的变量名。example_selector:(可选)一个示例选择器对象。如果提供,它会根据用户输入动态地从示例库中选择最相关的示例,而不是使用固定的examples列表。
- 典型用法:
fromlangchain_core.promptsimportPromptTemplate,FewShotPromptTemplate examples=[{"input":"happy","output":"sad"},{"input":"tall","output":"short"},]example_prompt=PromptTemplate.from_template("Input: {input}\nOutput: {output}")few_shot_prompt=FewShotPromptTemplate(examples=examples,example_prompt=example_prompt,prefix="Give the antonym of each word:",suffix="Input: {input}\nOutput:",input_variables=["input"],)final_prompt=few_shot_prompt.format(input="big")# 输出会是一个包含上述示例和 "Input: big\nOutput:" 的完整字符串 - 适用场景:面向文本补全模型的少样本学习,如文本分类、格式转换、信息提取等任务。
🎯 4.FewShotChatMessagePromptTemplate:为对话模型注入示例
这是FewShotPromptTemplate的对话版本,专为聊天模型设计。它的输出是一个消息列表,其中巧妙地嵌入了以对话形式呈现的示例,从而在保持对话结构的同时引导模型。
- 核心参数:
examples:示例字典列表。example_prompt:一个ChatPromptTemplate,用于定义单个示例在对话中的呈现格式(例如,一个(human, ...)后跟一个(ai, ...))。example_selector:(可选)与FewShotPromptTemplate类似,用于动态选择示例。需要与examples二选一提供。
- 典型用法:
fromlangchain_core.promptsimportChatPromptTemplate,FewShotChatMessagePromptTemplate examples=[{"input":"2+2","output":"4"},{"input":"2+3","output":"5"},]example_prompt=ChatPromptTemplate.from_messages([('human','{input}'),('ai','{output}')])few_shot_prompt=FewShotChatMessagePromptTemplate(examples=examples,example_prompt=example_prompt,)# 通常被嵌入到一个更大的 ChatPromptTemplate 中使用final_prompt=ChatPromptTemplate.from_messages([('system','You are a math assistant.'),few_shot_prompt,# 示例对话会在此处展开('human','{input}'),]) - 适用场景:面向聊天模型的少样本学习,是构建需要示例引导的复杂对话应用、Agent 和 RAG 系统的标准方式。
💎 总结与对比
| 特性 | PromptTemplate | ChatPromptTemplate | FewShotPromptTemplate | FewShotChatMessagePromptTemplate |
|---|---|---|---|---|
| 核心功能 | 基础文本模板 | 结构化对话模板 | 为文本模型注入示例 | 为对话模型注入示例 |
| 输出类型 | 单个字符串 | 消息列表 | 包含示例的长字符串 | 包含示例对话的消息列表 |
| 对话角色支持 | ❌ 不支持 | ✅ 原生支持 | ❌ 不支持 | ✅ 原生支持 |
| 适用模型 | 纯文本补全模型 | 聊天模型 | 纯文本补全模型 | 聊天模型 |
| 动态示例选择 | ❌ | ❌ | ✅(通过example_selector) | ✅(通过example_selector) |
| 典型场景 | 简单文本生成、分类 | RAG、多轮对话、Agent | 文本模型的少样本任务 | 对话模型的少样本任务、Agent、RAG |
总的来说,选择哪个组件取决于两个关键问题:你的目标模型是纯文本模型还是聊天模型?以及你的任务是否需要用示例来引导模型?对于现代 LLM 应用,ChatPromptTemplate和FewShotChatMessagePromptTemplate的组合是最常用和推荐的方式。