写点什么

从 0 到 1 打造问答机器人:RAG 全流程代码详解与优化基准

作者:聚客AI学院
  • 2025-07-10
    湖南
  • 本文字数:2515 字

    阅读完需:约 8 分钟

从0到1打造问答机器人:RAG全流程代码详解与优化基准

本文较长,建议点赞收藏,以免遗失。更多 AI 大模型应用开发学习视频及资料,尽在聚客AI学院

一、LangChain 搜索工具实战:集成 DuckDuckGo 实现实时信息查询

核心场景:解决大模型知识滞后问题,通过搜索引擎获取实时信息

1.1 基础集成方案

from langchain_community.tools import DuckDuckGoSearchRun# 初始化搜索工具search = DuckDuckGoSearchRun()result = search.invoke("OpenAI 2025年最新模型发布计划")print(result)  # 返回简洁文本摘要
复制代码

1.2 高级配置(含元数据过滤)

from langchain_community.utilities import DuckDuckGoSearchAPIWrapper# 定制化搜索参数wrapper = DuckDuckGoSearchAPIWrapper(    region="zh-cn",          # 中文结果    max_results=3,           # 限制结果数    time="y"                 # 最近一年信息)search = DuckDuckGoSearchResults(api_wrapper=wrapper)result = search.invoke("量子计算机最新突破")print(result[0]['title'], result[0]['link'])  # 输出标题和链接
复制代码

1.3 代理解决访问限制

# 通过代理API提升稳定性:cite[1]proxy_wrapper = DuckDuckGoSearchAPIWrapper(    api_endpoint="http://your-proxy-domain.com",  # 自建代理服务    max_results=5)
复制代码


避坑指南:公共 API 存在频率限制,建议使用代理或自建网关服务

二、Langchain 本地搜索:SearxNG+Agent 实战

核心价值:开源、去中心化的搜索引擎,保护隐私且可定制搜索源

2.1 自建 SearxNG 服务(关键配置)

# settings.yml 启用JSON输出:cite[8]search:    formats:        - html        - json  # 必须启用API格式
复制代码

2.2 LangChain 集成方案

from langchain_community.utilities import SearxSearchWrapper# 连接自建实例searx = SearxSearchWrapper(searx_host="http://localhost:8888")results = searx.run("Llama3微调教程", engines=["github"])# 作为Agent工具使用:cite[2]tools = load_tools(["searx-search"], searx_host="http://localhost:8888")agent = initialize_agent(tools, llm, agent="structured-chat-react")
复制代码

2.3 多引擎分流策略

# 创建专用工具链:cite[8]github_tool = SearxSearchResults(    name="Github_Search",     wrapper=wrapper,     kwargs={"engines": ["github"]})arxiv_tool = SearxSearchResults(    name="Arxiv_Search",    wrapper=wrapper,    kwargs={"engines": ["arxiv"]})
复制代码

三、RAG 数据工程起点:文档加载与结构化准备

核心挑战:PDF/Word/HTML 等格式的差异化解构

3.1 多格式文档加载

from langchain_community.document_loaders import (    PyPDFLoader,     Docx2txtLoader,    UnstructuredHTMLLoader)# PDF解析(保留布局)pdf_loader = PyPDFLoader("report.pdf")pdf_pages = pdf_loader.load_and_split()# Word解析(过滤样式噪声)docx_loader = Docx2txtLoader("manual.docx")text = docx_loader.load()[0].page_content# HTML解析(动态渲染)html_loader = UnstructuredHTMLLoader(    "page.html",     bs_kwargs={"features": "lxml"})
复制代码

3.2 高级表格提取技巧

import pdfplumber# 提取PDF表格:cite[3]with pdfplumber.open("financial.pdf") as pdf:    page = pdf.pages[0]    table = page.extract_table()    for row in table:        print(row[0], row[1])  # 输出单元格数据
复制代码

四、RAG 数据工程核心:文本切分



前沿技术:Meta-Chunking 动态分块策略

4.1 传统分块方法痛点

  • 固定长度切割破坏句子完整性

  • 语义边界识别不准(如代词跨块指代)

4.2 动态分块解决方案

from langchain_experimental.text_splitter import SemanticChunkerfrom langchain_community.embeddings import HuggingFaceEmbeddings# 基于语义相似度的动态分块splitter = SemanticChunker(    HuggingFaceEmbeddings(model_name="BAAI/bge-base-zh"),    breakpoint_threshold=0.5  # 相似度低于阈值时切分)chunks = splitter.split_text(long_document)
复制代码

4.3 Late Chunking 技术(解决代词问题)

原理:先整文档向量化 → 再按需分块 → 避免上下文丢失 效果:代词召回率提升 25%(如“它”正确指向“柏林”)

五、RAG 向量数据库实战:初始化+写入与查询

架构图


5.1 ChromaDB 快速入门

import chromadbfrom chromadb.utils.embedding_functions import OpenAIEmbeddingFunction# 初始化客户端client = chromadb.PersistentClient(path="./vector_db")collection = client.create_collection(    name="tech_docs",    embedding_function=OpenAIEmbeddingFunction())# 写入数据collection.add(    documents=["量子计算原理...", "区块链技术..."],    metadatas=[{"source": "doc1"}, {"source": "doc2"}],    ids=["id1", "id2"])# 相似查询results = collection.query(    query_texts=["量子比特的物理实现"],    n_results=2)
复制代码

5.2 生产级优化方案

  1. 混合索引:HNSW + 量化压缩(减少 40%内存占用)

  2. 元数据过滤:where={"date": {"$gte": "2024-01-01"}}

  3. 多向量支持:为同一文档存储摘要/关键词/正文向量

六、RAG 全流程实战:从文档到问答闭环

完整架构图:


6.1 代码实现(LangChain 链式集成)

from langchain_core.runnables import RunnablePassthroughfrom langchain_core.output_parsers import StrOutputParser# 构建混合检索链retriever = vector_db.as_retriever(search_kwargs={"k": 3})qa_chain = (    {        "context": lambda x: format_results(            retriever.invoke(x["query"]),        "question": RunnablePassthrough()    }    | prompt    | ChatOpenAI(model="gpt-4o")    | StrOutputParser())# 动态路由:根据问题类型选择来源def route_query(input):    if "最新" in input["query"] or "2025" in input["query"]:        return search_tool.invoke(input["query"])    else:        return retriever.invoke(input["query"])
复制代码

6.2 性能优化关键指标



完整代码库可参考我为大家整理的飞书文档:LangChain-Chatchat


更多 AI 大模型应用开发学习视频内容和资料,尽在聚客AI学院

发布于: 刚刚阅读数: 2
用户头像

让更多人拥抱AI,成就自我 2020-08-03 加入

更多AI大模型应用开发学习视频内容和资料,尽在聚客AI学院。

评论

发布
暂无评论
从0到1打造问答机器人:RAG全流程代码详解与优化基准_人工智能_聚客AI学院_InfoQ写作社区