03 提示词模板与 Chains

发布时间:2026/10/3 6:12:10
03 提示词模板与 Chains tags:/n - langchain提示词ChainsLCEL面试aliases:/n - Prompt Template链式调用03 提示词模板与 Chains一、提示词模板通过模板将变量插入 Prompt创建灵活的提示词。ChatPromptTemplate 示例 ChatPromptTemplate 演示 运行方式python 03_提示词模板.py fromlangchain_core.promptsimportChatPromptTemplatefromlangchain_openaiimportChatOpenAIimportosfromdotenvimportload_dotenv load_dotenv()# 构造提示词模板chat_prompt_templateChatPromptTemplate.from_messages([(system,你是一个专业的评论员),(human,请评价{product}的优缺点包括{aspect1}和{aspect2}。),])# 填充变量chat_message_listchat_prompt_template.invoke({product:iPhone 15,aspect1:性能,aspect2:外观,})# 调用模型llmChatOpenAI(modelqwen-plus,base_url...,api_key...)respllm.invoke(chat_message_list)print(resp.content)二、Runnable 接口LangChain 所有组件都实现了Runnable 接口统一调用方式。方法说明invoke()同步单次调用ainvoke()异步调用stream()流式调用batch()批次调用三、Chains链3.1 LCELLangChain 表达式语言使用管道符|将多个 Runnable 组件串联 LCEL 管道式组合演示 运行方式python 03_LCEL基础.py fromlangchain_core.promptsimportPromptTemplatefromlangchain_openaiimportChatOpenAIfromlangchain_core.output_parsersimportStrOutputParser llmChatOpenAI(modelqwen-plus,base_url...,api_key...)prompt_templatePromptTemplate(template讲一个关于{topic}的笑话,input_variables[topic],)parserStrOutputParser()# LCEL 管道式组合chainprompt_template|llm|parser# 一次调用自动串联执行respchain.invoke({topic:人工智能})print(resp)3.2 RunnableParallel并行执行 RunnableParallel 演示同一句话翻译成不同语言 运行方式python 03_RunnableParallel.py fromlangchain_core.promptsimportPromptTemplatefromlangchain_openaiimportChatOpenAIfromlangchain_core.runnablesimportRunnableParallelfromlangchain_core.output_parsersimportStrOutputParser llmChatOpenAI(modelqwen-plus,base_url...,api_key...)# 两个并行的翻译链english_chain(PromptTemplate.from_template(翻译成英文: {topic})|llm|StrOutputParser())korean_chain(PromptTemplate.from_template(翻译成韩文: {topic})|llm|StrOutputParser())# 并行执行map_chainRunnableParallel(englishenglish_chain,koreankorean_chain)respmap_chain.invoke({topic:人工智能是一种智能技术})print(resp)# {english: ..., korean: ...}3.3 LCEL 并行语法糖 LCEL 并行语法糖两个模型赏析同一首诗 运行方式python 03_LCEL并行.py llmChatOpenAI(modelqwen-plus,base_url...,api_key...)paragraph_1_chain(PromptTemplate.from_template(赏析这首诗{poem})|llm|StrOutputParser())paragraph_2_chain(PromptTemplate.from_template(从哲学角度分析这首诗{poem})|llm|StrOutputParser())summary_chain(PromptTemplate.from_template(对比这两种赏析{paragraph_1} 和 {paragraph_2})|llm|StrOutputParser())# LCEL 并行语法map_chain{paragraph_1:paragraph_1_chain,paragraph_2:paragraph_2_chain,}|summary_chain poem菩提本无树明镜亦非台本来无一物何处惹尘埃。respmap_chain.invoke({poem:poem})print(resp)相关笔记[[01-LangChain概述]] · [[02-Model-IO与模型调用]] · [[04-RAG检索增强]] · [[05-Agents智能体]]

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询