【LangChain系列七】提示词模板与少样本提示:把 Prompt 工程玩出花

📅 2026/7/6 14:54:12
【LangChain系列七】提示词模板与少样本提示:把 Prompt 工程玩出花
文章目录【LangChain系列七】提示词模板与少样本提示把 Prompt 工程玩出花1. 最简单的模板PromptTemplate2. 聊天场景ChatPromptTemplate3. 消息占位符MessagesPlaceholder4. LangChain Hub提示词仓库5. 少样本提示FewShotChatMessagePromptTemplate6. 几个实用技巧小结【LangChain系列七】提示词模板与少样本提示把 Prompt 工程玩出花写 LangChain 的文章到了第七篇该聊聊 Prompt 模板了。这块是 LangChain 里最基础也最实用的部分——毕竟大模型的质量一半靠模型本身一半靠你怎么问。1. 最简单的模板PromptTemplateLangChain 提供了PromptTemplate类用来创建文本提示模板。最常用的写法是from_templatefromlangchain_core.promptsimportPromptTemplate# 用 from_template 快速创建templatePromptTemplate.from_template(请用{language}写一首关于{topic}的诗)# invoke 传参调用resulttemplate.invoke({language:中文,topic:月亮})print(result)输出就是一个填好参数的StringPromptValue。简单直接没什么花活。你也可以用构造函数的方式手动指定input_variablestemplatePromptTemplate(template请用{language}写一首关于{topic}的诗,input_variables[language,topic])效果一样只是写法不同。个人更喜欢from_template少写点代码。2. 聊天场景ChatPromptTemplate实际开发中我们更多是在做多轮对话这时候就需要ChatPromptTemplate。它支持定义 system、user、ai 等不同角色的消息fromlangchain_core.promptsimportChatPromptTemplate promptChatPromptTemplate.from_messages([(system,你是一个专业的{role}请用简洁的语言回答问题),(user,{question})])resultprompt.invoke({role:Python讲师,question:什么是装饰器})print(result)from_messages接收一个元组列表每个元组的第一个元素是角色第二个是消息内容。支持的占位符有system— 系统指令user— 用户消息ai— 模型回复用于 few-shot 场景把模板和模型串起来用就是一条完整的链fromlangchain_openaiimportChatOpenAI modelChatOpenAI()chainprompt|model resultchain.invoke({role:Python讲师,question:什么是装饰器})print(result.content)这种prompt | model的链式写法是 LangChain 的核心模式后面会反复用到。3. 消息占位符MessagesPlaceholder有时候你想在模板里预留一个位置运行时动态插入一组消息。比如实现多轮对话时需要把历史消息塞进去。这时候用MessagesPlaceholderfromlangchain_core.promptsimportChatPromptTemplate,MessagesPlaceholder promptChatPromptTemplate.from_messages([(system,你是一个有帮助的助手),MessagesPlaceholder(history),(user,{input})])MessagesPlaceholder(history)就是一个占位符调用时往history里传消息列表fromlangchain_core.messagesimportHumanMessage,AIMessage history[HumanMessage(content你好),AIMessage(content你好有什么可以帮你的)]resultprompt.invoke({history:history,input:帮我写个Python脚本})这个在做聊天机器人时特别有用把历史对话记录塞进去模型就能记住上下文了。4. LangChain Hub提示词仓库写得好的 Prompt 想复用或者想看看别人怎么写的LangChain Hub 就是干这个的。它是一个提示词的在线仓库你可以上传、浏览、拉取模板。使用前需要配置LANGSMITH_API_KEY。fromlangchainimporthub# 从 Hub 拉取模板prompthub.pull(hwchase17/openai-tools-agent)# 拉取特定版本prompthub.pull(hwchase17/openai-tools-agent:latest)也可以上传自己的模板fromlangchainimporthub hub.push(your-namespace/my-prompt,prompt)实际开发中Hub 更多是用来参考别人怎么写 Prompt 的直接用的场景不算多。但了解一下没坏处万一哪天想复用别人的优质模板呢。5. 少样本提示FewShotChatMessagePromptTemplate少样本提示Few-shot是提升模型输出质量的利器——给模型几个输入→输出的例子它就能更好地理解你想要什么格式和风格。手动写 few-shot 的话你得自己组装一堆消息列表比较繁琐。LangChain 提供了FewShotChatMessagePromptTemplate来简化这件事fromlangchain_core.promptsimportChatPromptTemplate,FewShotChatMessagePromptTemplate# 1. 定义样例examples[{input:苹果,output:},{input:太阳,output:☀️},{input:星星,output:⭐},]# 2. 定义每个样例的消息模板example_promptChatPromptTemplate.from_messages([(human,{input}),(ai,{output})])# 3. 组装 few-shot 模板few_shot_promptFewShotChatMessagePromptTemplate(example_promptexample_prompt,examplesexamples)# 4. 最终提示词final_promptChatPromptTemplate.from_messages([(system,你是一个表情符号转换器把中文词语转换成对应的表情符号),few_shot_prompt,(user,{input})])# 调用resultfinal_prompt.invoke({input:开心})print(result)这样生成的消息列表里会自动把 examples 按照 example_prompt 的格式展开成一组对话消息插到你指定的位置。比起手动拼消息列表这种方式代码更清晰维护也方便——改 examples 数组就行。6. 几个实用技巧模板里可以混用普通文本和消息模板promptChatPromptTemplate.from_messages([(system,你是{role}),MessagesPlaceholder(history),(user,{input})])# 也可以用模板字符串promptChatPromptTemplate.from_template(你是一个{role}。请回答{question})部分填充partial有些参数你想提前固定运行时只传剩下的promptChatPromptTemplate.from_messages([(system,你是{role}),(user,{input})])# 预设 rolepartial_promptprompt.partial(rolePython专家)# 调用时只传 inputresultpartial_prompt.invoke({input:解释列表推导式})小结这篇文章覆盖了 LangChain 里提示词模板的核心用法PromptTemplate— 纯文本模板适合单轮简单场景ChatPromptTemplate— 聊天消息模板支持多角色MessagesPlaceholder— 动态插入消息列表做多轮对话必备FewShotChatMessagePromptTemplate— 少样本提示的标准化写法LangChain Hub— 提示词的在线仓库模板写好了后面串链、做 RAG、搞 Agent 都离不开它们。觉得有帮助的话点个赞收藏⭐支持一下吧