[{"data":1,"prerenderedAt":481},["ShallowReactive",2],{"article-agent\u002Flangchainrag":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"tags":11,"body":13,"_type":475,"_id":476,"_source":477,"_file":478,"_stem":479,"_extension":480},"\u002Farticles\u002Fagent\u002Flangchainrag","agent",false,"","LangChain 实现 RAG 检索增强问答系统：两种 Agent 方案详解","基于 LangChain 最新 API，完整实现 RAG 智能检索问答系统，涵盖大模型配置、文档处理、向量入库及工具调用型与上下文注入型两种 Agent 方案的构建与选型。","2026-03-25",[12],"Agent开发",{"type":14,"children":15,"toc":450},"root",[16,24,31,41,45,51,75,82,90,96,104,110,118,121,127,132,138,146,152,160,166,174,177,183,195,200,206,212,224,232,238,246,249,255,260,279,287,293,301,304,310,316,341,347,403,406,412,417,422,437],{"type":17,"tag":18,"props":19,"children":20},"element","p",{},[21],{"type":22,"value":23},"text","✨ 本文代码均为可直接运行的完整代码，基于LangChain最新版API编写，无过时语法，放心食用～",{"type":17,"tag":25,"props":26,"children":28},"h2",{"id":27},"一安装项目依赖包",[29],{"type":22,"value":30},"✅ 一、安装项目依赖包",{"type":17,"tag":32,"props":33,"children":35},"pre",{"code":34},"# 安装所有核心依赖，一行命令完成配置\npip install langchain langchain-text-splitters langchain-community bs4 python-dotenv\n",[36],{"type":17,"tag":37,"props":38,"children":39},"code",{"__ignoreMap":7},[40],{"type":22,"value":34},{"type":17,"tag":42,"props":43,"children":44},"hr",{},[],{"type":17,"tag":25,"props":46,"children":48},{"id":47},"二初始化三大核心组件核心配置",[49],{"type":22,"value":50},"✅ 二、初始化三大核心组件（核心配置）",{"type":17,"tag":18,"props":52,"children":53},{},[54,56,62,64,69,70],{"type":22,"value":55},"构建RAG Agent的核心三要素：",{"type":17,"tag":57,"props":58,"children":59},"strong",{},[60],{"type":22,"value":61},"对话大模型",{"type":22,"value":63},"、",{"type":17,"tag":57,"props":65,"children":66},{},[67],{"type":22,"value":68},"文本嵌入模型",{"type":22,"value":63},{"type":17,"tag":57,"props":71,"children":72},{},[73],{"type":22,"value":74},"向量存储器",{"type":17,"tag":76,"props":77,"children":79},"h3",{"id":78},"_1-配置大语言模型llm-对话生成核心",[80],{"type":22,"value":81},"1. 配置大语言模型（LLM）- 对话生成核心",{"type":17,"tag":32,"props":83,"children":85},{"code":84},"# 导入对话模型及环境变量配置依赖\nfrom langchain_openai import ChatOpenAI\nimport os\nfrom dotenv import load_dotenv\n\n# 加载.env文件中的环境变量（私密密钥管理，规范开发）\nload_dotenv()\n\n# 配置通义千问大模型（qwen-max）参数\nmodel_name = \"qwen-max\"\napi_key = os.getenv(\"DASHSCOPE_API_KEY\")\nbase_url = os.getenv(\"DASHSCOPE_BASE_URL\")\n\n# 初始化聊天模型：低随机性保证回答严谨，适配知识类问答场景\nmodel = ChatOpenAI(\n    model=model_name,\n    api_key=api_key,\n    base_url=base_url,\n    temperature=0.1,  # 温度值越低，回答越精准、确定性越强\n)\n",[86],{"type":17,"tag":37,"props":87,"children":88},{"__ignoreMap":7},[89],{"type":22,"value":84},{"type":17,"tag":76,"props":91,"children":93},{"id":92},"_2-配置文本嵌入模型-文本向量化核心",[94],{"type":22,"value":95},"2. 配置文本嵌入模型 - 文本向量化核心",{"type":17,"tag":32,"props":97,"children":99},{"code":98},"# 导入HuggingFace嵌入模型\nfrom langchain_huggingface import HuggingFaceEmbeddings\n\n# 初始化嵌入模型，行业主流轻量级高质量选型\n# 特点：文本表征能力强、适配中文+英文，首次运行自动缓存至本地，无需重复下载\nembeddings = HuggingFaceEmbeddings(model_name=\"sentence-transformers\u002Fall-mpnet-base-v2\")\n",[100],{"type":17,"tag":37,"props":101,"children":102},{"__ignoreMap":7},[103],{"type":22,"value":98},{"type":17,"tag":76,"props":105,"children":107},{"id":106},"_3-配置向量存储器-向量数据存储核心",[108],{"type":22,"value":109},"3. 配置向量存储器 - 向量数据存储核心",{"type":17,"tag":32,"props":111,"children":113},{"code":112},"# 导入内存级向量存储库\nfrom langchain_core.vectorstores import InMemoryVectorStore\n\n# 初始化内存向量库，轻量化无部署，无需持久化磁盘，适合快速开发\u002F演示场景\n# 绑定已初始化的嵌入模型，实现文本-向量的自动转换与匹配\nvector_store = InMemoryVectorStore(embeddings)\n",[114],{"type":17,"tag":37,"props":115,"children":116},{"__ignoreMap":7},[117],{"type":22,"value":112},{"type":17,"tag":42,"props":119,"children":120},{},[],{"type":17,"tag":25,"props":122,"children":124},{"id":123},"三文档处理全流程加载-分片-入库构建检索知识库",[125],{"type":22,"value":126},"✅ 三、文档处理全流程：加载 → 分片 → 入库（构建检索知识库）",{"type":17,"tag":18,"props":128,"children":129},{},[130],{"type":22,"value":131},"RAG核心前置步骤：将非结构化的网页文本，处理为结构化的向量知识库，共分3步完成，流程标准化且可复用",{"type":17,"tag":76,"props":133,"children":135},{"id":134},"_1-文档加载精准爬取网页核心内容",[136],{"type":22,"value":137},"1. 文档加载：精准爬取网页核心内容",{"type":17,"tag":32,"props":139,"children":141},{"code":140},"# 导入网页加载器及网页解析依赖\nimport bs4\nfrom langchain_community.document_loaders import WebBaseLoader\n\n# 网页解析规则：只提取指定class的核心内容（标题\u002F头部\u002F正文），过滤冗余HTML标签，提升数据质量\nbs4_strainer = bs4.SoupStrainer(class_=(\"post-title\", \"post-header\", \"post-content\"))\n\n# 初始化网页加载器，指定爬取路径+解析规则\nloader = WebBaseLoader(\n    web_paths=(\"https:\u002F\u002Flilianweng.github.io\u002Fposts\u002F2023-06-23-agent\u002F\",),\n    bs_kwargs={\"parse_only\": bs4_strainer},\n)\n\n# 加载网页文档至上下文\ndocs = loader.load()\n\n# 校验加载结果：确保成功加载1篇文档\nassert len(docs) == 1\n# 输出文档总字符数，直观查看文本体量\nprint(f\"✅ 文档加载完成，总字符数：{len(docs[0].page_content)}\")\n",[142],{"type":17,"tag":37,"props":143,"children":144},{"__ignoreMap":7},[145],{"type":22,"value":140},{"type":17,"tag":76,"props":147,"children":149},{"id":148},"_2-文档分片最优策略切割文本保留上下文关联",[150],{"type":22,"value":151},"2. 文档分片：最优策略切割文本，保留上下文关联",{"type":17,"tag":32,"props":153,"children":155},{"code":154},"# 导入递归字符分割器\nfrom langchain_text_splitters import RecursiveCharacterTextSplitter\n\n# 初始化文本分割器，采用行业最优的递归分割策略\ntext_splitter = RecursiveCharacterTextSplitter(\n    chunk_size=1000,        # 单个文本块最大字符数，适配嵌入模型输入长度限制\n    chunk_overlap=200,      # 相邻文本块重叠字符数，避免关键信息被截断，保留上下文关联性\n    add_start_index=True,   # 记录文本块在原始文档中的起始索引，便于溯源匹配内容\n)\n\n# 对加载的文档进行分片处理\nall_splits = text_splitter.split_documents(docs)\n\n# 输出分片结果\nprint(f\"✅ 文档分片完成，原始文档切分为 {len(all_splits)} 个子文档\")\n",[156],{"type":17,"tag":37,"props":157,"children":158},{"__ignoreMap":7},[159],{"type":22,"value":154},{"type":17,"tag":76,"props":161,"children":163},{"id":162},"_3-向量入库将分片文本存入向量库完成知识库构建",[164],{"type":22,"value":165},"3. 向量入库：将分片文本存入向量库，完成知识库构建",{"type":17,"tag":32,"props":167,"children":169},{"code":168},"# 将所有文本分片存入向量存储器，自动完成「文本→向量」转换+存储\ndocument_ids = vector_store.add_documents(documents=all_splits)\n\n# 预览前3个文本块的唯一标识ID\nprint(f\"✅ 向量入库完成，文本块ID预览：{document_ids[:3]}\")\n",[170],{"type":17,"tag":37,"props":171,"children":172},{"__ignoreMap":7},[173],{"type":22,"value":168},{"type":17,"tag":42,"props":175,"children":176},{},[],{"type":17,"tag":25,"props":178,"children":180},{"id":179},"四核心实现构建-rag-agent-检索问答智能体两种主流方案",[181],{"type":22,"value":182},"✅ 四、核心实现：构建 RAG Agent 检索问答智能体（两种主流方案）",{"type":17,"tag":18,"props":184,"children":185},{},[186,188,193],{"type":22,"value":187},"RAG的核心价值：让大模型结合",{"type":17,"tag":57,"props":189,"children":190},{},[191],{"type":22,"value":192},"外部知识库",{"type":22,"value":194},"回答问题，解决大模型「知识过时、事实性错误、领域知识不足」的痛点；",{"type":17,"tag":18,"props":196,"children":197},{},[198],{"type":22,"value":199},"以下提供两种工业界主流的RAG Agent实现方案，按需选择即可，均为可直接运行的最优写法",{"type":17,"tag":76,"props":201,"children":203},{"id":202},"方案一工具调用型-rag-agent推荐",[204],{"type":22,"value":205},"✅ 方案一：工具调用型 RAG Agent（推荐）",{"type":17,"tag":207,"props":208,"children":210},"h4",{"id":209},"核心逻辑",[211],{"type":22,"value":209},{"type":17,"tag":18,"props":213,"children":214},{},[215,217,222],{"type":22,"value":216},"自定义检索工具，让大模型",{"type":17,"tag":57,"props":218,"children":219},{},[220],{"type":22,"value":221},"自主决策是否调用检索工具",{"type":22,"value":223},"，并基于检索到的上下文生成答案，具备「工具调用思维」，适配复杂多轮问答\u002F多步骤查询场景，灵活性拉满。",{"type":17,"tag":32,"props":225,"children":227},{"code":226},"# 导入工具装饰器，封装自定义检索工具\nfrom langchain.tools import tool\n\n# 封装检索工具：返回检索到的上下文内容+原始文档对象，指定返回格式规范\n@tool(response_format=\"content_and_artifact\")\ndef retrieve_context(query: str):\n    \"\"\"核心检索工具：根据用户查询语句，从向量库中检索相关上下文信息，辅助回答问题\"\"\"\n    # 相似度检索：返回匹配度最高的2条文本内容（k值可按需调整）\n    retrieved_docs = vector_store.similarity_search(query, k=2)\n    # 格式化拼接检索结果：带上元数据+文本内容，提升大模型理解效率\n    serialized_context = \"\\n\\n\".join(\n        (f\"Source: {doc.metadata}\\nContent: {doc.page_content}\")\n        for doc in retrieved_docs\n    )\n    # 返回格式化文本 + 原始文档，兼顾可读性与溯源性\n    return serialized_context, retrieved_docs\n\n# 构建工具调用型智能体\nfrom langchain.agents import create_agent\n\n# 配置智能体核心参数：绑定大模型+检索工具+系统提示词\ntools = [retrieve_context]\n# 系统提示词：明确智能体能力边界与行为准则，精准引导大模型调用工具\nsystem_prompt = (\n    \"你是一个专业的问答助手，你可以调用检索工具获取外部知识库的上下文信息。\\n\"\n    \"请务必使用检索工具辅助回答用户的查询问题，确保答案的准确性和事实性。\"\n)\n# 初始化工具调用型RAG Agent\nagent = create_agent(model, tools, system_prompt=system_prompt)\n",[228],{"type":17,"tag":37,"props":229,"children":230},{"__ignoreMap":7},[231],{"type":22,"value":226},{"type":17,"tag":207,"props":233,"children":235},{"id":234},"测试复杂多步骤查询智能体自主调用工具",[236],{"type":22,"value":237},"✅ 测试：复杂多步骤查询（智能体自主调用工具）",{"type":17,"tag":32,"props":239,"children":241},{"code":240},"# 测试提问：多步骤复杂查询，考验智能体的工具调用能力与逻辑推理能力\nquery = (\n    \"What is the standard method for Task Decomposition?\\n\\n\"\n    \"Once you get the answer, look up common extensions of that method.\"\n)\n\n# 流式输出回答结果，实时查看智能体的思考+回答过程（体验更佳）\nprint(\"===== 工具调用型RAG Agent 回答结果 =====\")\nfor event in agent.stream(\n    {\"messages\": [{\"role\": \"user\", \"content\": query}]},\n    stream_mode=\"values\",\n):\n    event[\"messages\"][-1].pretty_print()\n",[242],{"type":17,"tag":37,"props":243,"children":244},{"__ignoreMap":7},[245],{"type":22,"value":240},{"type":17,"tag":42,"props":247,"children":248},{},[],{"type":17,"tag":76,"props":250,"children":252},{"id":251},"方案二上下文注入型-rag-agent极简高效",[253],{"type":22,"value":254},"✅ 方案二：上下文注入型 RAG Agent（极简高效）",{"type":17,"tag":207,"props":256,"children":258},{"id":257},"核心逻辑-1",[259],{"type":22,"value":209},{"type":17,"tag":18,"props":261,"children":262},{},[263,265,270,272,277],{"type":22,"value":264},"通过",{"type":17,"tag":57,"props":266,"children":267},{},[268],{"type":22,"value":269},"中间件(middleware)",{"type":22,"value":271}," 实现「全自动检索+上下文注入」，无需显式定义工具，智能体会在回答前",{"type":17,"tag":57,"props":273,"children":274},{},[275],{"type":22,"value":276},"自动检索",{"type":22,"value":278},"并将上下文注入到系统提示词中，全程无感调用，代码极简、运行高效，适配简单单轮问答场景。",{"type":17,"tag":32,"props":280,"children":282},{"code":281},"# 构建上下文注入型智能体\nfrom langchain.agents.middleware import dynamic_prompt, ModelRequest\n\n# 定义动态提示词中间件：自动检索+注入上下文，无感知完成检索逻辑\n@dynamic_prompt\ndef prompt_with_context(request: ModelRequest) -> str:\n    \"\"\"核心中间件：从请求中提取用户最新问题，检索相关上下文并注入系统提示词\"\"\"\n    # 提取用户最新的查询语句\n    last_query = request.state[\"messages\"][-1].text\n    # 自动执行相似度检索，获取相关上下文\n    retrieved_docs = vector_store.similarity_search(last_query)\n    # 格式化拼接检索到的文本内容\n    docs_content = \"\\n\\n\".join(doc.page_content for doc in retrieved_docs)\n    # 构造带上下文的系统提示词，让大模型基于外部知识回答\n    system_message = (\n        \"你是一个专业且乐于助人的问答助手，请严格基于下方提供的上下文信息回答用户问题：\\n\\n\"\n        f\"{docs_content}\"\n    )\n    return system_message\n\n# 初始化上下文注入型RAG Agent：无需传入工具，中间件自动完成检索逻辑\nagent = create_agent(model, tools=[], middleware=[prompt_with_context])\n",[283],{"type":17,"tag":37,"props":284,"children":285},{"__ignoreMap":7},[286],{"type":22,"value":281},{"type":17,"tag":207,"props":288,"children":290},{"id":289},"测试基础单轮查询极简高效",[291],{"type":22,"value":292},"✅ 测试：基础单轮查询（极简高效）",{"type":17,"tag":32,"props":294,"children":296},{"code":295},"# 测试提问：基础单轮查询，验证上下文注入效果\nquery = \"What is task decomposition?\"\n\n# 流式输出回答结果\nprint(\"\\n===== 上下文注入型RAG Agent 回答结果 =====\")\nfor step in agent.stream(\n    {\"messages\": [{\"role\": \"user\", \"content\": query}]},\n    stream_mode=\"values\",\n):\n    step[\"messages\"][-1].pretty_print()\n",[297],{"type":17,"tag":37,"props":298,"children":299},{"__ignoreMap":7},[300],{"type":22,"value":295},{"type":17,"tag":42,"props":302,"children":303},{},[],{"type":17,"tag":25,"props":305,"children":307},{"id":306},"核心知识点补充",[308],{"type":22,"value":309},"✨ 核心知识点补充",{"type":17,"tag":76,"props":311,"children":313},{"id":312},"两种agent方案对比-选型建议",[314],{"type":22,"value":315},"✅ 两种Agent方案对比 & 选型建议",{"type":17,"tag":317,"props":318,"children":319},"ol",{},[320,331],{"type":17,"tag":321,"props":322,"children":323},"li",{},[324,329],{"type":17,"tag":57,"props":325,"children":326},{},[327],{"type":22,"value":328},"工具调用型（方案一）",{"type":22,"value":330}," ：适合「复杂查询、多轮问答、多步骤推理」场景，大模型自主决策是否调用工具，灵活性强，是工业界主流方案；",{"type":17,"tag":321,"props":332,"children":333},{},[334,339],{"type":17,"tag":57,"props":335,"children":336},{},[337],{"type":22,"value":338},"上下文注入型（方案二）",{"type":22,"value":340}," ：适合「简单单轮问答、快速开发」场景，代码量少、无感知检索，开发效率高，但灵活性稍弱。",{"type":17,"tag":76,"props":342,"children":344},{"id":343},"核心优化点说明",[345],{"type":22,"value":346},"✅ 核心优化点说明",{"type":17,"tag":317,"props":348,"children":349},{},[350,368,379,392],{"type":17,"tag":321,"props":351,"children":352},{},[353,359,361,366],{"type":17,"tag":37,"props":354,"children":356},{"className":355},[],[357],{"type":22,"value":358},"temperature=0.1",{"type":22,"value":360},"：低温度值保证回答的",{"type":17,"tag":57,"props":362,"children":363},{},[364],{"type":22,"value":365},"精准性和确定性",{"type":22,"value":367},"，适配知识类问答场景，避免生成无关内容；",{"type":17,"tag":321,"props":369,"children":370},{},[371,377],{"type":17,"tag":37,"props":372,"children":374},{"className":373},[],[375],{"type":22,"value":376},"chunk_overlap=200",{"type":22,"value":378},"：文本块重叠设计，彻底解决「关键信息被截断」的问题，提升检索召回率；",{"type":17,"tag":321,"props":380,"children":381},{},[382,384,390],{"type":22,"value":383},"流式输出",{"type":17,"tag":37,"props":385,"children":387},{"className":386},[],[388],{"type":22,"value":389},"stream()",{"type":22,"value":391},"：实时返回回答内容，提升用户体验，避免长时间等待；",{"type":17,"tag":321,"props":393,"children":394},{},[395,401],{"type":17,"tag":37,"props":396,"children":398},{"className":397},[],[399],{"type":22,"value":400},"InMemoryVectorStore",{"type":22,"value":402},"：轻量化向量库，无需部署、无需持久化，适合快速开发和演示，生产环境可替换为Chroma\u002FPinecone等持久化向量库。",{"type":17,"tag":42,"props":404,"children":405},{},[],{"type":17,"tag":76,"props":407,"children":409},{"id":408},"总结",[410],{"type":22,"value":411},"✨ 总结",{"type":17,"tag":18,"props":413,"children":414},{},[415],{"type":22,"value":416},"本文完整实现了基于LangChain的RAG Agent智能检索问答系统，从「依赖安装→组件配置→文档处理→智能体构建→测试验证」全流程闭环，代码可直接复制运行，两种主流方案全覆盖，适配不同业务场景。RAG作为LLM落地的核心技术之一，该套代码可无缝迁移至PDF\u002F文档\u002F本地知识库等场景，实用性拉满。",{"type":17,"tag":76,"props":418,"children":420},{"id":419},"代码仓库",[421],{"type":22,"value":419},{"type":17,"tag":18,"props":423,"children":424},{},[425,427],{"type":22,"value":426},"gitee：",{"type":17,"tag":428,"props":429,"children":434},"a",{"href":430,"rel":431,"title":433},"https:\u002F\u002Flink.juejin.cn\u002F?target=https%3A%2F%2Fgitee.com%2Fo_insist%2Flangchain1.0_learn.git",[432],"nofollow","https:\u002F\u002Fgitee.com\u002Fo_insist\u002Flangchain1.0_learn.git",[435],{"type":22,"value":436},"gitee.com\u002Fo_insist\u002Fla…",{"type":17,"tag":18,"props":438,"children":439},{},[440,442],{"type":22,"value":441},"github：",{"type":17,"tag":428,"props":443,"children":447},{"href":444,"rel":445,"title":446},"https:\u002F\u002Flink.juejin.cn\u002F?target=https%3A%2F%2Fgithub.com%2Fo-insist%2Flangchain1.0_learn.git",[432],"https:\u002F\u002Fgithub.com\u002Fo-insist\u002Flangchain1.0_learn.git",[448],{"type":22,"value":449},"github.com\u002Fo-insist\u002Fla…",{"title":7,"searchDepth":451,"depth":451,"links":452},2,[453,454,460,465,469],{"id":27,"depth":451,"text":30},{"id":47,"depth":451,"text":50,"children":455},[456,458,459],{"id":78,"depth":457,"text":81},3,{"id":92,"depth":457,"text":95},{"id":106,"depth":457,"text":109},{"id":123,"depth":451,"text":126,"children":461},[462,463,464],{"id":134,"depth":457,"text":137},{"id":148,"depth":457,"text":151},{"id":162,"depth":457,"text":165},{"id":179,"depth":451,"text":182,"children":466},[467,468],{"id":202,"depth":457,"text":205},{"id":251,"depth":457,"text":254},{"id":306,"depth":451,"text":309,"children":470},[471,472,473,474],{"id":312,"depth":457,"text":315},{"id":343,"depth":457,"text":346},{"id":408,"depth":457,"text":411},{"id":419,"depth":457,"text":419},"markdown","content:articles:agent:langchain实现RAG问答系统.md","content","articles\u002Fagent\u002Flangchain实现RAG问答系统.md","articles\u002Fagent\u002Flangchain实现RAG问答系统","md",1779811689338]