Langchain学习——PromptTemplate

LangChain

详细测试样例和源码,可访问github

PromptTemplate - 基础字符串模板

💡 概念定义

PromptTemplate 是LangChain中最基础的提示模板类,用于创建具有变量占位符的字符串模板。它支持动态变量替换,是构建AI应用提示的核心组件。

官方文档: How to use prompts

🔧 核心方法

创建方法
from langchain_core.prompts import PromptTemplate

# 方法1:使用from_template(推荐)
prompt = PromptTemplate.from_template("Tell me a {adjective} joke about {topic}")

# 方法2:使用构造函数
prompt = PromptTemplate(
    input_variables=["adjective", "topic"],
    template="Tell me a {adjective} joke about {topic}"
)

输入:

  • template (str): 包含变量占位符的模板字符串,使用{variable_name}格式
  • input_variables (List[str], 可选): 显式指定输入变量列表,from_template会自动识别

输出: PromptTemplate实例

原理: 使用Python字符串格式化语法,自动识别模板中的变量占位符并创建输入变量列表

格式化方法
# 方法1:format方法
formatted_string = prompt.format(adjective="funny", topic="programming")

# 方法2:invoke方法(兼容LCEL)
formatted_string = prompt.invoke({
   
   "adjective": "funny", "topic": "programming"}).text

输入: 键值对字典,键为变量名,值为替换内容
输出: 格式化后的字符串
原理: 使用Python字符串格式化将变量占位符替换为实际值

📝 使用示例

from langchain_core.prompts import PromptTemplate

# 创建邮件模板
email_template = PromptTemplate.from_template(
    "写一封{tone}的邮件给{recipient},主题是{subject}。内容包括:{content}"
)

# 格式化邮件
email = email_template.format(
    tone="正式",
    recipient="客户",
    subject="产品更新通知",
    content="我们的新功能已经上线"
)

print(email)
# 输出: 写一封正式的邮件给客户,主题是产品更新通知。内容包括:我们的新功能已经上线

ChatPromptTemplate - 对话消息模板

💡 概念定义

ChatPromptTemplate 专门用于构建对话式AI的提示模板,支持多种消息类型(系统、用户、AI消息),是构建聊天机器人和对话应用的核心组件。

官方文档: How to use chat models

🔧 核心方法

创建方法
from langchain_core.prompts import ChatPromptTemplate

# 方法1:使用元组格式
chat_prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant specialized in {domain}"),
    ("user", "Please help me with: {request}"),
    ("assistant", "I'll help you with {request}. Let me think about this..."),
    ("user", "{follow_up}")
])

# 方法2:使用消息模板对象
from langchain_core.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate

system_template = SystemMessagePromptTemplate.from_template(
    "You are a {role} with expertise in {domain}"
)
human_template = HumanMessagePromptTemplate.from_template("Help me with: {task}")

chat_prompt = ChatPromptTemplate.from_messages([system_template, human_template])

输入:

  • 消息列表,每个消息可以是:
    • 元组格式: (role, content_template)
    • 消息模板对象: SystemMessagePromptTemplate, HumanMessagePromptTemplate, AIMessagePromptTemplate

输出: ChatPromptTemplate实例

原理: 将对话转换为结构化的消息序列,每个消息具有特定的角色和内容

格式化方法
# 生成消息列表
messages = chat_prompt.format_messages(
    domain="web development",
    request="optimize my React app",
    follow_up="What about performance monitoring?"
)

# 消息类型: [SystemMessage, HumanMessage, AIMessage, HumanMessage]

输入: 变量字典
输出: 消息对象列表(SystemMessage, HumanMessage, AIMessage等)
原理: 将每个消息模板格式化为对应的消息对象

📝 使用示例

from langchain_core.prompts import ChatPromptTemplate

# 创建智能客服模板
customer_service = ChatPromptTemplate.from_messages
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值