LLMChain是 LangChain 早期经典链路写法在 LCEL 管道出现前是串联提示词 大模型的核心组件。下面分基础用法、传参、结合解析器、多示例、对比 LCEL 逐一演示。环境依赖bash运行pip install langchain langchain-openai公共导入与模型初始化python运行from langchain_openai import ChatOpenAI from langchain_core.prompts import ChatPromptTemplate, PromptTemplate from langchain.chains import LLMChain from langchain_core.output_parsers import StrOutputParser, JsonOutputParser # 初始化大模型 llm ChatOpenAI(modelgpt-3.5-turbo, temperature0)一、基础用法最简 LLMChain1. 字符串模板 LLMChainpython运行# 1. 定义提示词模板 prompt PromptTemplate( input_variables[question], # 声明输入变量 template请简要回答问题{question} ) # 2. 实例化 LLMChain chain LLMChain(llmllm, promptprompt) # 3. 执行调用传入参数字典 result chain.invoke({question: Python 是什么}) # 打印结果 print(完整返回, result) print(模型回答, result[text])返回说明chain.invoke()返回字典默认模型输出存在text键中。2. ChatPromptTemplate 对话模板推荐适配聊天模型写法更贴合对话场景python运行# 对话风格提示词 chat_prompt ChatPromptTemplate.from_messages([ (system, 你是一名技术助手回答简洁明了), (user, {query}) ]) # 构建链 chain LLMChain(llmllm, promptchat_prompt) # 执行 res chain.run(query什么是 LLMChain) # 简易 run 方法直接传参数 print(res)chain.run(变量值)简易调用适合单变量场景chain.invoke(字典)标准调用支持多变量。二、多输入变量场景模板包含多个占位符同时传入多个参数python运行# 多变量模板 prompt PromptTemplate( input_variables[name, skill], template姓名{name}擅长技能{skill}请用一句话介绍这个人 ) chain LLMChain(llmllm, promptprompt) # 传入多个变量 res chain.invoke({ name: 张三, skill: Python 开发、大模型应用 }) print(res[text])三、LLMChain 搭配输出解析器和解析器结合实现结构化输出JSON / 字符串示例 1搭配 StrOutputParserpython运行prompt PromptTemplate(template总结下文内容{content}, input_variables[content]) parser StrOutputParser() # 组合LLMChain 解析器 chain LLMChain(llmllm, promptprompt) | parser # 调用直接得到字符串不再是字典 result chain.invoke({content: LLMChain 是 LangChain 早期的链路组件用于串联提示词与大模型。}) print(result)示例 2搭配 JsonOutputParser 输出 JSONpython运行prompt PromptTemplate( input_variables[info], template 解析人物信息只返回标准JSON字段name, age 内容{info} ) json_parser JsonOutputParser() # 链路组合 chain LLMChain(llmllm, promptprompt) | json_parser # 调用直接得到字典 data chain.invoke({info: 李四26岁}) print(type(data), data)四、批量执行使用chain.batch()批量处理多组输入python运行prompt PromptTemplate(input_variables[q], template回答问题{q}) chain LLMChain(llmllm, promptprompt) # 批量输入列表 batch_inputs [ {q: 11等于几}, {q: 地球是什么形状} ] # 批量调用 batch_results chain.batch(batch_inputs) for idx, item in enumerate(batch_results): print(f问题{idx1}回答{item[text]})五、传统 LLMChain 嵌套复杂流程早期实现多步骤流程需要链式嵌套写法相比 LCEL 更繁琐python运行# 第一步提取关键词 prompt1 PromptTemplate(input_variables[text], template提取关键词{text}) chain1 LLMChain(llmllm, promptprompt1) # 第二步根据关键词写短句 prompt2 PromptTemplate(input_variables[kw], template根据关键词写一句话{kw}) chain2 LLMChain(llmllm, promptprompt2) # 手动串联取上一步结果传给下一步 text 人工智能赋能传统行业 res1 chain1.invoke({text: text}) kw res1[text] res2 chain2.invoke({kw: kw}) print(关键词, kw) print(最终句子, res2[text])缺点需要手动取值、传参链路越长代码越臃肿。六、LLMChain 与 LCEL 管道 写法对比同一功能两种实现直观对比1. LLMChain 写法python运行prompt ChatPromptTemplate.from_messages([(user, 回答{question})]) chain LLMChain(llmllm, promptprompt) res chain.invoke({question: 介绍大模型}) print(res[text])2. 等效 LCEL 管道写法官方主推python运行prompt ChatPromptTemplate.from_messages([(user, 回答{question})]) chain prompt | llm | StrOutputParser() res chain.invoke({question: 介绍大模型}) print(res)七、核心特点总结核心结构LLMChain 提示词模板 大模型是最基础的执行链返回值原生返回字典模型输出默认在text字段调用方式invoke(字典)标准调用run(变量)简易单参数调用batch(列表)批量调用优缺点优点入门简单历史项目大量使用兼容性好缺点多步骤流程需要手动传参、代码冗余原生流式、并行支持弱复杂链路维护成本高现状LangChain 官方现已推荐 LCEL 管道写法LLMChain偏向维护旧项目、学习理解历史架构