微信扫码
添加专属顾问
我要投稿
LangChain提示词模板帮你告别混乱的字符串拼接,实现Prompt工程化管理,提升LLM应用开发效率。核心内容: 1. LangChain提示词模板的设计思想与核心价值 2. PromptTemplate和ChatPromptTemplate的基础用法解析 3. 通过FewShotPromptTemplate实现标准化、模块化提示词构建
01
—
LangChain 的提示词模板
02
—
PromptTemplate 包装器
from langchain import PromptTemplatetemplate = """你是一个旅游专家,请根据用户提出的描述:{msg},给出建议。"""# 实例化模板的第一种方式prompt = PromptTemplate(template=template, input_variables=["msg"])# 实例化模板的第二种方式# prompt = PromptTemplate.from_template(template)# 将用户的输入通过 format 方法嵌入提示词模板,并且做格式化处理final_prompt = prompt.format(msg="xxx")print(final_prompt)
from langchain.prompts import (ChatPromptTemplate,PromptTemplate,SystemMessagePromptTemplate,AIMessagePromptTemplate,HumanMessagePromptTemplate)template = """你是一个旅游专家。"""system_message_prompt = SystemMessagePromptTemplate.from_template(template)human_template = "请根据用户提出的描述:{msg},给出建议"human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt,human_message_prompt])final_prompt = chat_prompt.format_prompt(msg="xxx")print(final_prompt.to_string())
from langchain.prompts import FewShotPromptTemplatetemplate = """词语:{input},反义词是:{output}"""example_prompt = PromptTemplate(template=template, input_variables=["input", "output"])examples = [{"input": "高", "output": "矮"},{"input": "胖", "output": "瘦"},{"input": "黑", "output": "白"},]few_shot_prompt = FewShotPromptTemplate(examples=examples,example_prompt=example_prompt,prefix="来玩个游戏,我说正义词,你回答它的反义词",suffix="现在轮到你了,正义词:{input},反义词",input_variables=["input"])final_prompt = few_shot_prompt.format(input="高")print(final_prompt)
from langchain.prompts import PromptTemplate# 1. 角色定义模板:定义助手的基本身份和风格role_template = """你是一名{company}的{style}技术支持专家。你必须根据提供的产品知识库和对话历史来回答问题。如果知识库中没有相关信息,请如实告知,切勿编造。"""role_prompt = PromptTemplate.from_template(role_template)# 2. 知识库模板:从外部系统查询并注入相关知识kb_template = """【相关产品知识】{knowledge_base}"""kb_prompt = PromptTemplate.from_template(kb_template)# 3. 历史对话模板:管理多轮对话的上下文history_template = """【对话历史】{dialog_history}"""history_prompt = PromptTemplate.from_template(history_template)# 4. 当前问题模板:格式化用户的最新输入question_template = """【用户当前问题】{human_input}"""question_prompt = PromptTemplate.from_template(question_template)# 定义最终提示的“总装图纸”final_template = """{role_instruction}{knowledge_context}{conversation_context}请基于以上信息,专业、清晰地回答用户问题:{final_question}"""final_prompt = PromptTemplate.from_template(final_template)# 创建 PipelinePromptTemplate,建立子模板到最终模板变量的映射关系from langchain.prompts import PipelinePromptTemplatepipeline_prompt = PipelinePromptTemplate(final_prompt=final_prompt, # 指定最终组装的模板pipeline_prompts=[# 格式: (在final_prompt中的变量名, 对应的子模板)("role_instruction", role_prompt),("knowledge_context", kb_prompt),("conversation_context", history_prompt),("final_question", question_prompt)])# 准备输入变量(通常来自应用的不同模块)input_data = {"company": "天颖科技","style": "耐心且专业","knowledge_base": "产品‘天颖AI’支持OCR等功能。最新版本为v2。","dialog_history": "用户:天颖AI支持什么功能?\n助手:支持图生图功能。","human_input": "那它的最新版本是多少?"}# 格式化管道提示,引擎会自动按顺序调用所有子模板并组装full_prompt_text = pipeline_prompt.format(**input_data)print(full_prompt_text)
03
—
技巧与避坑
from_template 方法input_variables 的错误。# 推荐prompt = PromptTemplate.from_template(“翻译成{language}:{text}“)# 繁琐且易错prompt = PromptTemplate(input_variables=[“language“, “text“], template=“...”)
template = """角色:{role_description} # 例如:'一位严格的历史老师'任务:根据以下大纲,生成一份{output_length}的课程讲义。大纲:{outline}"""



53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2025-12-24
别再堆 Prompt 了:用 LangChain 1.0 搭建“深度思考 Agent”
2025-12-21
文档审核Agent2.0系统落地方案:LangChain1.1+MinerU
2025-12-21
LangChain、Dify、n8n、Coze框架对比
2025-12-20
涌现观点|LangChain 2025 报告发布:57%的企业在用Agent,但32%的人被"质量"卡住了
2025-12-18
2025 LangChain智能体工程年度报告发布!AI智能体从画饼到吃饼
2025-12-17
智能体LangChain v1.0生态解读与迁移建议
2025-12-08
让AI智能体拥有像人类的持久记忆:基于LangGraph的长短期记忆管理实践指南
2025-12-04
Agentic RAG这样用LangChain解决复杂问题
2025-11-03
2025-10-23
2025-10-19
2025-11-06
2025-10-31
2025-11-05
2025-10-23
2025-11-01
2025-10-15
2025-10-29
2025-11-03
2025-10-29
2025-07-14
2025-07-13
2025-07-05
2025-06-26
2025-06-13
2025-05-21