一、 LangChain 提示词模板
PromptTemplate:常用的String提示模板
聊天提示模板 ChatPromptTemplate: 常用的Chat提示模板,用于组合各种角色的消息模板,传入聊天模型。
消息模板包括:
- ChatMessagePromptTemplate
- HumanMessagePromptTemplate
- AlMessagePromptTemplate
- SystemMessagePromptTemplate
核心概念:消息角色
在聊天模型中,不同的消息类型(或"角色")告诉模型应该如何回应:
- System(系统):设定AI助手的背景、行为和人格
- Human(人类):代表用户的输入
- AI(AI):代表AI助手之前的回复
各模板详细解析
1. SystemMessagePromptTemplate
- 角色:系统指令
- 功能:设定对话的背景、规则和AI的行为准则
- 使用场景:在对话开始时定义AI的角色、能力限制、回答风格等
from langchain.prompts import SystemMessagePromptTemplate
system_template = SystemMessagePromptTemplate.from_template(
"你是一个专业的{subject}专家,用{language}回答问题。回答要简洁明了。"
)
# 格式化后生成 SystemMessage 对象
system_message = system_template.format(subject="Python编程", language="中文")
print(system_message.content)
# 输出:你是一个专业的Python编程专家,用中文回答问题。回答要简洁明了。
2. HumanMessagePromptTemplate
- 角色:用户输入
- 功能:代表用户向AI提出的问题或指令
- 使用场景:构建用户的查询内容
from langchain.prompts import HumanMessagePromptTemplate
human_template = HumanMessagePromptTemplate.from_template(
999



被折叠的 条评论
为什么被折叠?



