LangChain 第三课:Chain 链精讲
LangChain 第三课Chain 链精讲通俗讲解 完整注释代码一、核心概念1. 什么是 Chain链就是把多个 LangChain 组件按顺序串成一条自动执行流水线不用分步手动调用写好流程后输入参数自动走完全部流程2. Chain 核心作用简化代码减少重复调用固定业务执行流程模型 提示词 逻辑一键运行可嵌套、可组合实现复杂任务3. 最常用 5 种主流链工作必用LLMChain 基础问答链最基础SequentialChain 顺序执行多步链SimpleSequentialChain 简易顺序链RetrievalQA 检索问答链RAG 专用ConversationChain 带记忆对话链二、1. LLMChain 基础问答链入门必学作用绑定提示词模板 大模型一键执行问答完整带注释代码python运行# 导入依赖 from langchain_openai import OpenAI from langchain.prompts import PromptTemplate # 导入基础链 from langchain.chains import LLMChain # 1. 初始化大模型 # temperature0.2 偏向严谨、少发散 llm OpenAI(temperature0.2) # 2. 定义提示词模板 template 请简短讲解{knowledge}这个知识点 prompt PromptTemplate( input_variables[knowledge], # 定义传入变量 templatetemplate ) # 3. 组装成基础链 # 把模型和提示词绑定在一起 llm_chain LLMChain(llmllm, promptprompt) # 4. 运行链传入参数执行 result llm_chain.run(knowledge向量数据库) # 打印结果 print(result)执行流程传入变量 → 填充提示词 → 调用大模型 → 返回答案三、2. SimpleSequentialChain 简易顺序链作用一步结果作为下一步输入连续执行多个任务适合翻译→总结、解释→精简、扩写→缩写业务例子第一步解释名词第二步把解释压缩成一句话带注释代码python运行from langchain_openai import OpenAI from langchain.prompts import PromptTemplate from langchain.chains import LLMChain, SimpleSequentialChain # 1. 初始化模型 llm OpenAI(temperature0.3) # 第一步链详细解释 template1 详细解释一下{topic} prompt1 PromptTemplate(input_variables[topic], templatetemplate1) chain1 LLMChain(llmllm, promptprompt1) # 第二步链精简成一句话 template2 把下面内容精简成一句话{content} prompt2 PromptTemplate(input_variables[content], templatetemplate2) chain2 LLMChain(llmllm, promptprompt2) # 组装顺序链 自动把上一步输出传给下一步 total_chain SimpleSequentialChain(chains[chain1, chain2]) # 只需要传入最开始参数 final_result total_chain.run(大模型RAG) print(final_result)特点只能单输入单输出不能传多个不同变量四、3. SequentialChain 标准顺序链企业常用作用支持多个输入、多个输出复杂多步骤业务首选业务场景输入技术名词 使用场景第一步写原理第二步写适用场景第三步写优缺点带注释代码python运行from langchain_openai import OpenAI from langchain.prompts import PromptTemplate from langchain.chains import LLMChain, SequentialChain llm OpenAI(temperature0.2) # 链1生成原理 prompt1 PromptTemplate( input_variables[tech], template简述{tech}工作原理 ) chain1 LLMChain(llmllm, promptprompt1, output_keyprinciple) # 链2生成优缺点 prompt2 PromptTemplate( input_variables[tech], template写出{tech}的优点和缺点 ) chain2 LLMChain(llmllm, promptprompt2, output_keyadvantage) # 组装多入多出顺序链 all_chain SequentialChain( chains[chain1, chain2], input_variables[tech], # 整体统一输入 output_variables[principle,advantage] # 需要拿到的所有输出 ) # 执行获取字典格式结果 res all_chain.invoke({tech:Agent智能体}) print(原理,res[principle]) print(优缺点,res[advantage])五、4. RetrievalQA 检索问答链RAG 核心链作用专门做文档知识库问答自动完成检索文档片段 → 拼接上下文 → 模型生成答案极简结构逻辑python运行# 伪代码逻辑 # 加载文档 - 分块 - 存入向量库 - 检索相似内容 - 问答链回答核心记忆做本地知识库问答直接用 RetrievalQA 链六、5. ConversationChain 对话记忆链作用自带记忆组件自动保存聊天上下文多轮对话连贯之前记忆层代码就是此链python运行from langchain_openai import OpenAI from langchain.chains import ConversationChain from langchain.memory import ConversationBufferMemory # 初始化记忆 memory ConversationBufferMemory() # 搭建对话链 conv_chain ConversationChain(llmOpenAI(temperature0.5), memorymemory) # 多轮对话自动记历史 print(conv_chain.predict(input我在学习LangChain)) print(conv_chain.predict(input帮我总结它的链功能))七、本节课必背知识点直接背Chain 本质组件流水线自动执行整套流程LLMChain基础绑定模型 提示词单任务首选SimpleSequentialChain简单串行单入单出SequentialChain复杂串行多入多出项目最常用RetrievalQARAG 知识库专属问答链ConversationChain带记忆多轮对话链链最大优势流程固化、代码简洁、方便统一维护八、通俗易懂大白话总结单个任务用LLMChain先后两步简单流程SimpleSequentialChain多步骤多结果正式业务SequentialChain本地文档问答RetrievalQA聊天连续对话ConversationChain