
1. 本章目标学完本章后你应该能够理解 Prompt 在大模型应用中的作用使用普通字符串编写 Prompt使用PromptTemplate管理单段提示词使用ChatPromptTemplate管理聊天消息给 Prompt 注入变量编写适合业务场景的提示词完成商品文案、学习计划、客服回复三个案例2. 什么是 PromptPrompt 就是给模型的指令。例如请用一句话介绍 LangChain。这是一条很简单的 Prompt。在实际项目中Prompt 往往会更复杂你是一名电商运营专家。 请根据商品名称、商品卖点、目标用户生成一段适合详情页展示的商品文案。 要求 1. 语气自然 2. 不超过 150 字 3. 突出用户利益Prompt 的质量会直接影响模型输出质量。3. 为什么需要 Prompt 模板如果每次都手写完整 Prompt会有几个问题代码里字符串太长多个地方重复写类似提示词变量拼接容易出错后期不方便维护和修改例如prompt f请为商品 {product_name} 写一段文案卖点是 {selling_points}这种写法可以用但当提示词变长后就不好维护。LangChain 提供了 Prompt 模板可以把固定内容和变量分开。4. PromptTemplate 基本使用PromptTemplate适合普通文本提示词。示例from langchain_core.prompts import PromptTemplate prompt_template PromptTemplate.from_template( 请为商品 {product_name} 写一句广告语突出卖点{selling_points} ) prompt prompt_template.invoke( { product_name: 无线鼠标, selling_points: 静音、续航长、轻便, } ) print(prompt.text)模板中的变量{product_name} {selling_points}会被实际数据替换。5. ChatPromptTemplate 基本使用如果要使用 system / human 消息结构推荐使用ChatPromptTemplate。示例from langchain_core.prompts import ChatPromptTemplate prompt_template ChatPromptTemplate.from_messages( [ (system, 你是一名电商运营专家回答要简洁、有吸引力。), (human, 请为商品 {product_name} 写一段文案卖点是{selling_points}), ] )调用prompt prompt_template.invoke( { product_name: 无线鼠标, selling_points: 静音、续航长、轻便, } )ChatPromptTemplate会生成消息列表适合聊天模型。本课程后面会经常使用它。随堂代码from langchain_core.prompts import PromptTemplate prompt_templatePromptTemplate.from_template(请为商品 {product_name} 写一句广告语突出卖点{selling_points}) promptprompt_template.invoke( { product_name:logi鼠标, selling_points:特别安静灵敏速度快无延迟 } ) print(prompt.text)from langchain_core.prompts import ChatPromptTemplate chat_templateChatPromptTemplate.from_messages( [ (system,你是一名电商运营专家回答要简洁、有吸引力。), (human,请为商品 {product_name} 写一段文案卖点是{selling_points}) ] ) promptchat_template.invoke( { product_name:logi鼠标, selling_points:特别安静灵敏速度快无延迟 } ) print(prompt) # 在真正使用时需要将整个对象都传递给大模型而不是一个字符串 print(prompt.messages[1].content)核心本质差异PromptTemplate纯文本提示词模板只生成一段完整字符串。无角色概念只能把系统指令、用户提问揉在一段文字里靠文字描述区分角色模型容易混淆。ChatPromptTemplate 【建议使用】对话消息模板生成结构化消息列表[SystemMessage, HumanMessage, AIMessage]。适用现代对话大模型GPT-3.5/4、Claude、通义千问、文心一言等所有 Agent、ReAct、聊天智能体都用它。ChatPromptTemplate原生支持三种角色消息SystemMessage系统提示词智能体规则、ReAct 流程、约束HumanMessage用户输入AIMessage模型历史回复用于多轮记忆6. Prompt 编写建议第一阶段先掌握几个最实用的原则。6.1 明确角色例如你是一名电商运营专家。角色可以帮助模型确定回答风格。6.2 明确任务例如请根据商品名称和卖点生成一段详情页文案。任务越明确模型越容易输出想要的结果。6.3 明确约束例如要求 1. 不超过 150 字 2. 不要使用夸张宣传 3. 适合年轻上班族约束可以减少输出跑偏。6.4 给出输入字段例如商品名称{product_name} 商品卖点{selling_points} 目标用户{target_user}让模型清楚知道每个变量的含义。7. 案例一商品文案生成器本案例根据商品信息生成一段电商文案。创建01_product_copywriter.pyimport os from dotenv import load_dotenv from langchain.chat_models import init_chat_model from langchain_core.prompts import ChatPromptTemplate load_dotenv() modelinit_chat_model( modeldeepseek-v4-flash, model_provideropenai, base_urlos.getenv(DEEPSEEK_BASE_URL), api_keyos.getenv(DEEPSEEK_API_KEY), temperature0.5 ) prompt_str 请根据下面信息生成一段商品详情页文案。 商品名称{product_name} 商品卖点{selling_points} 目标用户{target_user} 要求 1. 不超过 150 字 2. 突出用户能获得的好处 3. 不要使用虚假夸张表达 chat_promt_templateChatPromptTemplate.from_messages([ (system,你是一名电商运营专家擅长写自然、有吸引力但不过度夸张的商品文案), (human,prompt_str) ]) promptchat_promt_template.invoke({ product_name:无线静音鼠标, selling_points:静音按键、蓝牙连接、续航 30 天、轻便便携, target_user:经常在办公室和图书馆使用电脑的人, }) responsemodel.invoke(prompt) print(response.content)运行python 01_product_copywriter.py这个案例体现了 Prompt 的基本结构角色输入要求输出任务8. 减少重复代码封装模型初始化前面几个案例都重复写了模型初始化代码。将其放入到一个 utils 工具包下可以创建model_factory.pyimport os from dotenv import load_dotenv from langchain.chat_models import init_chat_model def get_deepSeek_model(temperature:float0.7): load_dotenv() model init_chat_model( modeldeepseek-v4-flash, model_provideropenai, base_urlos.getenv(DEEPSEEK_BASE_URL), api_keyos.getenv(DEEPSEEK_API_KEY), temperaturetemperature ) return model然后案例中可以改成from model_factory import get_deepseek_model model get_deepseek_model(temperature0.5)这不是复杂封装只是减少重复代码。9. 案例二学习计划生成器本案例根据学习目标生成学习计划。创建02_study_plan.pyfrom langchain_core.prompts import ChatPromptTemplate from utils.model_factory import get_deepSeek_model modelget_deepSeek_model(0.5) prompt_str 请为我制定一个学习计划。 学习主题{topic} 学习时长{days} 天 每天可学习时间{hours_per_day} 小时 当前基础{level} 要求 1. 按天列出学习内容 2. 每天给出练习任务 3. 内容要适合当前基础 chat_promt_templateChatPromptTemplate.from_messages([ (system,你是一名编程课程规划老师擅长为初学者制定可执行的学习计划。), (human,prompt_str) ]) promptchat_promt_template.invoke({ topic:langchain, days:3, hours_per_day:1, level:会python基础 }) responsemodel.invoke(prompt) print(response.content)运行python 02_study_plan.py这个案例可以看到Prompt 可以接收多个变量变量可以是字符串也可以是数字模型输出可以被规则引导10. 案例三客服回复生成器本案例根据用户问题和订单状态生成客服回复。创建03_customer_reply.pyfrom langchain_core.prompts import ChatPromptTemplate from utils.model_factory import get_deepSeek_model model get_deepSeek_model(0.7) prompt_str 请根据用户问题和订单状态生成一段客服回复。 用户问题{user_question} 订单状态{order_status} 物流信息{shipping_info} 要求 1. 先安抚用户 2. 再说明当前情况 3. 最后给出下一步建议 4. 不超过 120 字 chat_promt_template ChatPromptTemplate.from_messages([ (system, 你是一名电商客服回复要礼貌、明确不推卸责任。), (human, prompt_str) ]) prompt chat_promt_template.invoke({ user_question: 我的快递三天没更新了是不是丢了, order_status: 已发货, shipping_info: 物流显示包裹已到达上海转运中心暂未更新下一站信息 }) response model.invoke(prompt) print(response.content)运行python 03_customer_reply.py这个案例接近真实业务用户问题来自前端订单状态来自数据库物流信息来自第三方接口Prompt 负责组织这些信息并生成回复11.MessagesPlaceholder作用MessagesPlaceholder用来在ChatPromptTemplate中插入一组消息列表。它通常用于插入聊天历史插入 Agent 中间步骤插入已经构造好的多条 message普通变量{question}只能填充一段文本而MessagesPlaceholder(history)可以一次插入多条(历史)消息。基本导入from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder基本语法MessagesPlaceholder(variable_name)常用写法MessagesPlaceholder(history)完整参数MessagesPlaceholder( variable_namehistory, optionalTrue, n_messages6, )参数说明参数类型说明variable_namestr占位符对应的输入变量名optionalbool是否允许不传该变量n_messagesint \| None最多保留最近多少条消息在 ChatPromptTemplate 中使用prompt ChatPromptTemplate.from_messages([ (system, You are a helpful assistant. ), MessagesPlaceholder(history), (human, {question}), ])这里面有一个照应关系当MessagesPlaceholder(history)时默认optionalFalse也就是说 prompt.invoke 时必须传入history 这个标签当然这个标签的名字两个地方保持一致即可那如果optionalTrue, 那么prompt.invoke 时可以不传递 history另外这个history必须是一个列表才行调用时传入prompt_value prompt.invoke({ history: [ (human, My name is Alice.), (ai, Nice to meet you, Alice.), ], question: What is my name?, })最终消息顺序相当于[ SystemMessage(contentYou are a helpful assistant.), HumanMessage(contentMy name is Alice.), AIMessage(contentNice to meet you, Alice.), HumanMessage(contentWhat is my name?), ]输入格式MessagesPlaceholder对应的变量必须是消息列表。可以使用 tuple 格式history [ (human, 你好), (ai, 你好有什么可以帮你), ]也可以使用 LangChain 消息对象from langchain_core.messages import HumanMessage, AIMessage history [ HumanMessage(content你好), AIMessage(content你好有什么可以帮你), ]错误写法prompt.invoke({ history: 你好, question: 刚才我说了什么, })history不能是普通字符串必须是 list。optionalTrue默认情况下如果没有传入history会报错。prompt ChatPromptTemplate.from_messages([ (system, You are a helpful assistant.), MessagesPlaceholder(history), (human, {question}), ]) prompt.invoke({ question: Hello!, })如果聊天历史可能为空建议设置prompt ChatPromptTemplate.from_messages([ (system, You are a helpful assistant.), MessagesPlaceholder(history, optionalTrue), (human, {question}), ])这样就可以不传historyprompt_value prompt.invoke({ question: Hello!, })n_messagesn_messages用来限制插入最近多少条消息。prompt ChatPromptTemplate.from_messages([ (system, You are a concise assistant.), MessagesPlaceholder(history, optionalTrue, n_messages4), (human, {question}), ])示例输入prompt_value prompt.invoke({ history: [ (human, My name is Alice.), (ai, Nice to meet you, Alice.), (human, I like Python.), (ai, Great choice.), (human, I also use LangChain.), (ai, LangChain is useful for LLM apps.), ], question: What do you know about me?, })这里history只会插入最近 4 条消息[ HumanMessage(contentI like Python.), AIMessage(contentGreat choice.), HumanMessage(contentI also use LangChain.), AIMessage(contentLangChain is useful for LLM apps.), ]然后再追加当前问题HumanMessage(contentWhat do you know about me?)placeholder 简写MessagesPlaceholder也可以使用 tuple 简写prompt ChatPromptTemplate.from_messages([ (system, You are a helpful assistant.), (placeholder, {history}), (human, {question}), ])通常等价于prompt ChatPromptTemplate.from_messages([ (system, You are a helpful assistant.), MessagesPlaceholder(history, optionalTrue), (human, {question}), ])如果需要设置n_messages建议使用显式写法MessagesPlaceholder(history, optionalTrue, n_messages4)from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder from utils.model_factory import get_deepSeek_model modelget_deepSeek_model(temperature0.7) chat_promptChatPromptTemplate.from_messages( [ (system,你是一个很棒的问答助手), MessagesPlaceholder(variable_namehistory,optionalTrue,n_messages4), (human,{question}) ] ) promptchat_prompt.invoke({ history:[ (human, My name is Alice.), (ai, Nice to meet you, Alice.), (human, I like Python.), (ai, Great choice.), (human, I also use LangChain.), (ai, LangChain is useful for LLM apps.), ], question:我叫什么名字 }) respmodel.invoke(prompt) print(resp.content)12. 本章重点本章最重要的是掌握Prompt 是给模型的任务说明Prompt 应该包含角色、任务、输入、约束PromptTemplate适合普通文本提示词ChatPromptTemplate适合聊天模型Prompt 模板可以通过变量复用业务场景中要把输入信息清晰地交给模型13. 常见问题12.1 Prompt 越长越好吗不是。Prompt 应该清晰但不应该堆太多无关要求。要求越多模型越可能顾此失彼。12.2 为什么模型没有完全按要求输出大模型不是传统程序不能保证百分百服从每一条指令。如果输出格式很重要后续要结合结构化输出和程序校验。这部分会在第 4 章讲。12.3 PromptTemplate 和 ChatPromptTemplate 怎么选简单建议普通文本任务可以用PromptTemplate聊天模型任务优先用ChatPromptTemplate本课程后面会更多使用ChatPromptTemplate。