
1. LangChain提示词模板基础解析在构建大语言模型应用时提示词工程是核心环节。LangChain框架提供的PromptTemplate类正是为了解决动态提示词构建的痛点。传统硬编码提示词存在维护困难、复用性差的问题而模板化提示词通过变量插值实现了动态内容生成。1.1 基础模板构建方法创建提示词模板最直接的方式是使用PromptTemplate类。以下是典型示例from langchain_core.prompts import PromptTemplate # 基础模板示例 basic_template 请根据以下上下文回答问题 上下文{context} 问题{question} 回答 prompt PromptTemplate.from_template(basic_template)这个模板包含两个变量context和question。实际使用时通过format方法传入具体值filled_prompt prompt.format( contextLangChain是一个用于构建大语言模型应用的框架, questionLangChain的主要用途是什么 )1.2 模板变量进阶用法LangChain支持更复杂的变量控制变量类型校验可以为变量添加类型注解默认值设置为可选参数提供默认值变量描述添加说明文档提升可维护性from typing import Optional advanced_template PromptTemplate( input_variables[query, language], template请用{language}回答以下问题{query}, partial_variables{style: 专业的技术回答}, validate_templateTrue # 启用模板验证 )2. 复合模板构建技术实际应用中单一模板往往不能满足复杂需求。LangChain提供了多种模板组合方式。2.1 字符串拼接式组合最简单的组合方式是使用加号运算符from langchain_core.prompts import PromptTemplate base_prompt PromptTemplate.from_template(你是一位{role}专家) task_prompt PromptTemplate.from_template(请解释{concept}的概念) combined_prompt base_prompt \n\n task_prompt这种方式的优点是直观但当模板数量多时维护较困难。2.2 PipelinePrompt模板管道对于复杂场景推荐使用PipelinePromptTemplatefrom langchain_core.prompts import PipelinePromptTemplate # 定义最终模板框架 full_template {introduction} {example} {task} full_prompt PromptTemplate.from_template(full_template) # 定义子模板 introduction_template 你正在模拟{character}的说话风格。 introduction_prompt PromptTemplate.from_template(introduction_template) example_template 示例对话 Q: {sample_q} A: {sample_a} example_prompt PromptTemplate.from_template(example_template) task_template 现在请回答真实问题 Q: {query} A: task_prompt PromptTemplate.from_template(task_template) # 构建管道 input_prompts [ (introduction, introduction_prompt), (example, example_prompt), (task, task_prompt) ] pipeline_prompt PipelinePromptTemplate( final_promptfull_prompt, pipeline_promptsinput_prompts )这种结构的优势在于各子模板可独立修改模板间依赖关系清晰支持部分变量预填充3. 聊天提示词模板与普通提示词不同聊天场景需要维护对话历史。LangChain提供了专门的聊天模板。3.1 基础聊天模板from langchain_core.prompts import ChatPromptTemplate from langchain_core.messages import SystemMessage, HumanMessagePromptTemplate chat_template ChatPromptTemplate.from_messages([ SystemMessage(content你是一位专业的技术顾问), HumanMessagePromptTemplate.from_template({user_input}) ])3.2 动态对话历史管理实际对话需要维护上下文from langchain_core.prompts import MessagesPlaceholder dynamic_chat_template ChatPromptTemplate.from_messages([ SystemMessage(content你是一位有帮助的AI助手), MessagesPlaceholder(variable_namehistory), HumanMessagePromptTemplate.from_template({input}) ])使用MessagesPlaceholder可以在运行时插入历史消息列表。4. 模板中的模板嵌套技巧高级应用中我们需要在模板中嵌套其他模板实现更灵活的提示词构建。4.1 变量中的子模板from langchain_core.prompts import PromptTemplate # 子模板 detail_template PromptTemplate.from_template( 相关背景{background}\n具体要求{requirement} ) # 主模板 main_template PromptTemplate.from_template( 任务说明 {task_detail} 请按照上述要求完成工作。 ) # 组合使用 nested_prompt main_template.partial( task_detaildetail_template.format( background项目涉及LangChain框架, requirement实现动态提示词生成 ) )4.2 条件化模板选择通过函数实现动态模板选择from typing import Dict def get_template(scenario: str) - PromptTemplate: templates { simple: PromptTemplate.from_template(回答{query}), detailed: PromptTemplate.from_template( 问题分析{query} 思考过程{reasoning} 最终答案{answer} ) } return templates.get(scenario, templates[simple]) selected_template get_template(detailed)5. 实战构建RAG提示词系统让我们实现一个完整的检索增强生成(RAG)提示词系统。5.1 检索阶段提示词retrieval_template PromptTemplate.from_template( 你是一位专业的研究助理。请根据以下知识片段提取与问题相关的信息。 知识片段 {context} 问题 {question} 相关信息的摘要 )5.2 生成阶段提示词generation_template ChatPromptTemplate.from_messages([ SystemMessage(content你是一位技术专家正在回答用户问题), HumanMessagePromptTemplate.from_template( 根据以下上下文信息回答问题 上下文 {retrieved_context} 问题 {user_question} 请提供专业、准确的回答 ) ])5.3 完整流程集成from langchain_core.prompts import PipelinePromptTemplate rag_template 请按照以下步骤回答问题 1. 信息检索 {retrieval_result} 2. 综合回答 {generation_result} rag_prompt PipelinePromptTemplate( final_promptPromptTemplate.from_template(rag_template), pipeline_prompts[ (retrieval_result, retrieval_template), (generation_result, generation_template) ] )6. 模板管理最佳实践6.1 模板版本控制建议将模板存储在单独的文件中与代码分离prompts/ ├── retrieval/ │ ├── v1.txt │ └── v2.txt └── generation/ ├── basic.txt └── technical.txt6.2 模板性能监控记录不同模板的响应质量和耗时import time from typing import Dict, Any def track_prompt_performance( template: PromptTemplate, inputs: Dict[str, Any], model ) - Dict: start_time time.time() # 执行提示词 prompt template.format(**inputs) response model.invoke(prompt) duration time.time() - start_time return { template_version: template.metadata.get(version), duration: duration, response_length: len(response), quality_score: None # 可添加质量评估 }6.3 模板测试方案建立模板测试套件import unittest class TestPrompts(unittest.TestCase): def test_retrieval_template(self): template load_template(retrieval/v1.txt) test_input { context: 测试上下文, question: 测试问题 } result template.format(**test_input) self.assertIn(测试问题, result) self.assertNotIn({context}, result)7. 高级技巧与疑难解答7.1 处理模板冲突当多个模板定义相同变量时可以采用以下策略命名空间隔离user_template PromptTemplate.from_template(用户{content}) ai_template PromptTemplate.from_template(AI{content}) combined user_template.partial(content用户输入) \ ai_template.partial(contentAI回复)变量重命名user_template PromptTemplate.from_template({user_content}) ai_template PromptTemplate.from_template({ai_content})7.2 动态变量控制对于不确定的变量集合可以使用**kwargs展开from typing import Dict, Any def safe_format(template: PromptTemplate, **kwargs: Any) - str: # 只保留模板实际需要的变量 valid_vars { k: v for k, v in kwargs.items() if k in template.input_variables } return template.format(**valid_vars)7.3 模板缓存优化频繁创建的模板可以缓存from functools import lru_cache lru_cache(maxsize100) def get_cached_template(template_text: str) - PromptTemplate: return PromptTemplate.from_template(template_text)7.4 长文本处理策略当处理长文本时分块处理from langchain_text_splitters import CharacterTextSplitter splitter CharacterTextSplitter( chunk_size1000, chunk_overlap200 ) chunks splitter.split_text(long_text) chunk_templates [ PromptTemplate.from_template(f文本片段 {i}:\n{chunk}) for i, chunk in enumerate(chunks) ]摘要提取summary_template PromptTemplate.from_template( 请从以下文本中提取关键信息 {text} 关键点总结 )8. 模板设计模式8.1 角色设定模式def create_role_prompt(role: str, task: str) - PromptTemplate: return PromptTemplate.from_template(f 你是一位专业的{role}正在执行{task}任务。 请按照以下要求操作 {{instructions}} 具体内容 {{content}} )8.2 链式思考模式cot_template PromptTemplate.from_template( 问题{question} 请逐步思考 1. 理解问题{step1} 2. 分析关键点{step2} 3. 寻找解决方案{step3} 4. 验证方案{step4} 5. 最终答案{final_answer} )8.3 多视角分析模式multi_view_template PromptTemplate.from_template( 请从以下角度分析问题 技术角度 {technical_view} 业务角度 {business_view} 用户体验角度 {user_experience_view} 综合建议 {suggestion} )9. 性能优化技巧9.1 模板预编译对于高频使用的模板precompiled { greeting: PromptTemplate.from_template(你好{name}).format, query: PromptTemplate.from_template(搜索{keywords}).format } # 快速调用 greeting_msg precompiled[greeting](name张三)9.2 批量处理优化使用批量生成减少开销def batch_format(templates: List[PromptTemplate], inputs: List[dict]): return [ template.format(**input_dict) for template, input_dict in zip(templates, inputs) ]9.3 异步处理对于大量模板处理import asyncio async def async_format(template: PromptTemplate, inputs: dict): loop asyncio.get_event_loop() return await loop.run_in_executor( None, template.format, **inputs )10. 安全注意事项输入消毒import html def safe_format(template: PromptTemplate, **kwargs): sanitized { k: html.escape(str(v)) for k, v in kwargs.items() } return template.format(**sanitized)敏感词过滤from some_filter_library import ProfanityFilter filter ProfanityFilter() def clean_prompt(text: str) - str: return filter.clean(text)长度限制MAX_LENGTH 2000 def validate_prompt(prompt: str) - bool: return len(prompt) MAX_LENGTH11. 调试与测试11.1 模板验证def validate_template(template: PromptTemplate): try: # 测试必填变量 dummy_inputs { var: test_value for var in template.input_variables } template.format(**dummy_inputs) return True except KeyError as e: print(f缺少必要变量{e}) return False except Exception as e: print(f模板格式错误{e}) return False11.2 变量覆盖率检查def check_coverage(template: PromptTemplate, inputs: dict) - float: required set(template.input_variables) provided set(inputs.keys()) return len(required provided) / len(required)11.3 模板差异分析from difflib import unified_diff def compare_templates(template1: str, template2: str): lines1 template1.splitlines() lines2 template2.splitlines() return \n.join(unified_diff(lines1, lines2))12. 企业级应用建议12.1 模板注册中心建立组织内的模板共享机制class PromptRegistry: def __init__(self): self._templates {} def register(self, name: str, template: PromptTemplate): self._templates[name] template def get(self, name: str) - PromptTemplate: return self._templates.get(name) def list_all(self) - Dict[str, str]: return { name: template.template for name, template in self._templates.items() }12.2 模板版本迁移当模板需要更新时def migrate_template( old_template: PromptTemplate, new_template: PromptTemplate, converter: callable ) - PromptTemplate: 将旧模板的数据迁移到新模板 return new_template.partial( **converter(old_template.input_variables) )12.3 多语言支持from typing import Dict class I18nPrompt: def __init__(self, templates: Dict[str, PromptTemplate]): self.templates templates def get_for_locale(self, locale: str) - PromptTemplate: return self.templates.get(locale, self.templates[default])13. 未来演进方向可视化模板编辑器开发图形界面工具降低非技术人员使用门槛模板效果分析建立自动化评估体系量化不同模板的性能差异智能模板推荐基于历史数据推荐最适合当前场景的模板结构版本智能升级自动检测模板改进点生成优化建议在实际项目中我发现最有效的模板设计流程是原型设计→A/B测试→数据分析→迭代优化。每个模板都应该有明确的版本记录和变更说明这对团队协作特别重要。