RAG patterns — RAG 模式
Jump to navigation
Jump to search
RAG 模式(英语:RAG Patterns)是一套用于构建检索增强生成(Retrieval-Augmented Generation, RAG)系统的架构和方法论。这些模式旨在通过将大型语言模型(LLM)与外部动态可访问的数据源集成,解决 LLM 的一些基本问题,如幻觉、知识过时和领域特异性不足[1]。RAG 的发展已从简单的线性流程演变为复杂的模块化和代理系统[2]。
主要 RAG 模式
随着技术的发展,出现了多种 RAG 模式,每种模式都旨在解决特定问题,并在质量、速度和成本之间进行权衡。
- Classic RAG(经典 RAG) — 这是一种基础方法,将用户查询向量化,在向量数据库中搜索相关片段(chunk);然后将找到的 chunk 连同问题一起输入 LLM 以生成答案[1]。
- Multi-Query RAG(多查询 RAG) — LLM 生成原始查询的多个改写或细化版本;对所有版本进行搜索,并将结果合并,从而提高召回率(recall)[3]。
- HyDE (Hypothetical Document Expansion) — 用于克服简短查询与长文档之间的“语义鸿沟”。LLM 首先生成一个“假设性”的文档答案,然后使用其嵌入进行搜索,这通常能提高检索质量[4]。
- Hybrid Retrieval(混合检索) — 结合了语义(向量)和词法(BM25)搜索。混合方案已成为生产系统的标准:向量搜索覆盖语义匹配,而 BM25 则能找到精确的术语、ID 或缩略词;最后通过融合(fusion)合并结果[5][6][7]。
- Re-ranking(重排序) — 一个两阶段过程:快速检索器返回一组候选结果(例如,前 100 个),然后由交叉编码器(cross-encoder)或其他重排序器重新计算相关性,并选出最佳结果(例如,前 5 个)提供给 LLM[8][9]。
- Query Routing(查询路由) — 在包含多个异构数据源(不同的索引、数据库、API)的系统中,查询通过路由器(基于 LLM 的选择器或分类器)被导向最佳数据源;通常包含回退(fallback)策略[10]。
- Agentic/Web RAG(代理式 RAG) — LLM 充当代理:它分解复杂问题,规划迭代步骤,并利用工具(向量搜索、网页搜索)进行反馈驱动的操作。典型的实现是 ReAct 范式[11];对于面向网页信息收集且要求强制引用的场景,可参考 WebGPT[12]。
相关及新兴范式
- GraphRAG(图 RAG) — 使用知识图谱作为数据源和上下文选择机制;搜索沿着实体间的连接结构和文本内容进行,从而提高了多跳(multi-hop)问题的可解释性和答案质量[13][14]。
- MM-RAG(多模态 RAG) — 处理文本和视觉信息源(扫描件、图表、表格)。例如,VisRAG 展示了在多模态文档上进行面向视觉语言模型(VLM)的检索和生成[15]。
- Packaging & Context Handling(上下文打包) — 将检索到的 chunk 集成到提示词中的方法,包括:Stuff、Map-Reduce、Refine、Tree-of-Chunks (RAPTOR)[16]。
模式对比表
| 模式 | 应用场景 | 对质量的影响 | 成本/延迟 | 风险与限制 |
|---|---|---|---|---|
| Classic RAG | PoC 和基于同构数据库的简单问答 | 基线水平;严重依赖嵌入质量[1] | 低 | 对措辞敏感;存在上下文不相关的风险 |
| Hybrid Retrieval | 大多数生产场景;尤其适用于包含大量代码、缩略词或 ID 的数据 | 提高召回率;能覆盖精确术语[5][6][7] | 低/中 | 需要调整融合权重;管理两个索引 |
| Re-ranking | 对精度要求极高的场景 | 显著提升 top-k 结果的精确率[8][9] | 中/高 | 增加额外的延迟和成本 |
| Multi-Query | 简短或涉及多个方面(multi-aspect)的查询 | 提高召回率[3] | 中 | 可能产生多余或带噪声的改写 |
| HyDE | 简短或模糊且存在较大“语义鸿沟”的查询 | 提升零样本(zero-shot)检索质量[4] | 中 | 效果依赖于“假设性”文本的质量 |
| Query Routing | 存在多个数据源(文档库、SQL、API、网页)的场景 | 通过选择正确的数据源来提高相关性[10] | 中 | 路由错误会导致检索失败 |
| Agentic/Web RAG | 复杂、探索性、多步骤的查询 | 解决超出线性流程能力范围的任务[11][12] | 高 | 实现复杂,可能陷入循环;需要设置安全护栏(guardrails) |
实践与架构
实施阶段
- Proof of Concept (PoC): 从 Classic RAG 开始,在一个有限但有代表性的数据集上验证嵌入质量和基本检索效果[1]。
- Minimum Viable Product (MVP): 实施 Hybrid Retrieval 和 Re-ranking,这是投入产出比最高的组合[5][8]。
- Production(生产环境): 添加查询转换(HyDE、Multi-Query),并在必要时引入 Query Routing;配置可观测性(记录检索、重排序和响应日志)并进行 A/B 测试[3][10]。
关键组件
- 分块(Chunking): 影响质量最关键的因素之一。简单的固定大小分块常会破坏语义单元。推荐使用结构化(基于标记)或递归(段落→句子→词)的分割器[17][18]。
- 嵌入与元数据: 为每个 chunk 存储 document_id、页面/章节、标题、日期等元数据;这对于过滤和正确引用来源至关重要。
- 混合检索与重排序: 使用 BM25+向量搜索并进行融合(或 RRF),然后对一小部分候选结果使用交叉编码器进行重排序[5][6][8]。
- 上下文打包: 对于大型语料库,可选择 Map-Reduce、Refine 或 Tree-of-Chunks 等策略[16][18]。
常见错误(反模式)
- 仅使用向量搜索而忽略 BM25 → 在处理代码/ID/缩略词时失败[5][7]。
- chunk 过大或过小 → 导致上下文丢失或嵌入“模糊化”[17]。
- 生产环境中缺少重排序 → LLM 接收到充满噪声的上下文[8]。
- 缺少可观测性和来源追踪 → 无法分析错误原因(参见 RAG 评估)。
质量评估与指标
评估在检索层面(离线)和端到端生成层面进行。
检索器指标
- Hit Rate, Recall@k, MRR — 衡量相关文档的覆盖率和排名位置。
- Context Precision & Recall — 衡量检索到的上下文在多大程度上不含“垃圾信息”并覆盖了所有必要内容(已在 RAGAS 中实现)[19]。
生成器指标(端到端)
- Faithfulness / Groundedness — 衡量答案与所提供上下文的一致性。
- Answer Relevancy(答案相关性) — 衡量答案与原始问题的一致性。
为了自动化评估,可使用开源框架,如:RAGAS、TruLens(RAG triad:上下文相关性、依据性和答案相关性)、DeepEval[20][21]。
参见
- Retrieval-Augmented Generation (RAG)
- 向量数据库
- 嵌入
- AI 代理
- GraphRAG
- MM-RAG
- LLM 评估与基准测试
参考文献
- Lewis, P., Perez, E., et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. NeurIPS. arXiv:2005.11401.
- Fan, W., Ding, Y., et al. (2024). A Survey on RAG Meeting LLMs: Towards Retrieval-Augmented Large Language Models. KDD. DOI:10.1145/3637528.3671470; arXiv:2405.06211.
- Gao, L., Ma, X., Lin, J., Callan, J. (2023). Precise Zero‑Shot Dense Retrieval without Relevance Labels (HyDE). ACL 2023. ACL Anthology; arXiv:2212.10496.
- Nogueira, R., Cho, K. (2019). Passage Re‑ranking with BERT. arXiv:1901.04085.
- Weaviate Docs. Hybrid search (BM25+Vector). [1].
- Qdrant Docs. Hybrid Queries. [2].
- Milvus Docs. Full‑Text Search / Hybrid Search. [3] / [4].
- LangChain Docs. MultiQueryRetriever. [5].
- Cohere Docs. Rerank — best practices. [6].
- LlamaIndex Docs. Routing (query routers/selectors). [7].
- Yao, S., et al. (2023). ReAct: Synergizing Reasoning and Acting in Language Models. ICLR. arXiv:2210.03629.
- Nakano, R., et al. (2021). WebGPT: Browser‑assisted question‑answering with human feedback. arXiv:2112.09332.
- Microsoft Research Blog. GraphRAG: Unlocking LLM discovery on narrative private data. (2024). [8].
- Microsoft Research. Project GraphRAG. (2024). [9].
- Yu, S., et al. (2024). VisRAG: Vision‑based Retrieval‑augmented Generation on Multi‑modality Documents. arXiv:2410.10594.
- Sarthi, P., et al. (2024). RAPTOR: Recursive Abstractive Processing for Tree‑Organized Retrieval. arXiv:2401.18059.
- Es, S., et al. (2024). RAGAs: Automated Evaluation of Retrieval Augmented Generation. EACL (Demo). [10].
- TruLens Docs. RAG Triad. [11].
- DeepEval (GitHub). The LLM Evaluation Framework. [12].
- LangChain Docs. RecursiveCharacterTextSplitter. [13].
- LlamaIndex Docs. HierarchicalNodeParser; Response Synthesis (Tree/Refine). [14]; [15].
注释
- ↑ 1.0 1.1 1.2 1.3 Lewis, P., Perez, E., et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. NeurIPS. arXiv:2005.11401.
- ↑ Fan, W., Ding, Y., et al. (2024). A Survey on RAG Meeting LLMs: Towards Retrieval-Augmented Large Language Models. KDD. DOI:10.1145/3637528.3671470; arXiv:2405.06211.
- ↑ 3.0 3.1 3.2 LangChain Docs. MultiQueryRetriever. https://python.langchain.com/docs/how_to/MultiQueryRetriever/
- ↑ 4.0 4.1 Gao, L., Ma, X., Lin, J., Callan, J. (2023). Precise Zero‑Shot Dense Retrieval without Relevance Labels. ACL 2023. arXiv:2212.10496; ACL Anthology: 2023.acl‑long.99.
- ↑ 5.0 5.1 5.2 5.3 5.4 Weaviate Docs. Hybrid search (BM25+Vector). https://docs.weaviate.io/weaviate/concepts/search/hybrid-search
- ↑ 6.0 6.1 6.2 Qdrant Docs. Hybrid Queries. https://qdrant.tech/documentation/concepts/hybrid-queries/
- ↑ 7.0 7.1 7.2 Milvus Docs. Full‑Text Search 和 Hybrid Search. https://milvus.io/docs/full-text-search.md; https://milvus.io/docs/hybrid_search_with_milvus.md
- ↑ 8.0 8.1 8.2 8.3 8.4 Nogueira, R., Cho, K. (2019). Passage Re‑ranking with BERT. arXiv:1901.04085.
- ↑ 9.0 9.1 Cohere Docs. Rerank — best practices. https://docs.cohere.com/docs/reranking-best-practices
- ↑ 10.0 10.1 10.2 LlamaIndex Docs. Routing (query routers/selectors). https://docs.llamaindex.ai/en/stable/module_guides/querying/router/
- ↑ 11.0 11.1 Yao, S., et al. (2023). ReAct: Synergizing Reasoning and Acting in Language Models. ICLR 2023. arXiv:2210.03629.
- ↑ 12.0 12.1 Nakano, R., et al. (2021). WebGPT: Browser‑assisted question‑answering with human feedback. arXiv:2112.09332.
- ↑ Microsoft Research Blog. GraphRAG: Unlocking LLM discovery on narrative private data. 2024. https://www.microsoft.com/en-us/research/blog/graphrag-unlocking-llm-discovery-on-narrative-private-data/
- ↑ Microsoft Research. Project GraphRAG. https://www.microsoft.com/en-us/research/project/graphrag/
- ↑ Yu, S., et al. (2024). VisRAG: Vision‑based Retrieval‑augmented Generation on Multi‑modality Documents. arXiv:2410.10594; OpenReview: zG459X3Xge.
- ↑ 16.0 16.1 Sarthi, P., et al. (2024). RAPTOR: Recursive Abstractive Processing for Tree‑Organized Retrieval. arXiv:2401.18059.
- ↑ 17.0 17.1 LangChain Docs. RecursiveCharacterTextSplitter. https://python.langchain.com/docs/how_to/recursive_text_splitter/
- ↑ 18.0 18.1 LlamaIndex Docs. HierarchicalNodeParser 和 Tree Summarization. https://docs.llamaindex.ai/en/stable/api/llama_index.core.node_parser.HierarchicalNodeParser.html; https://docs.llamaindex.ai/en/stable/examples/low_level/response_synthesis/
- ↑ Es, S., et al. (2024). RAGAs: Automated Evaluation of Retrieval Augmented Generation. EACL (Demo). https://aclanthology.org/2024.eacl-demo.16/
- ↑ TruLens Docs. RAG Triad. https://www.trulens.org/getting_started/core_concepts/rag_triad/
- ↑ DeepEval (GitHub). https://github.com/confident-ai/deepeval