技术指南

上下文检索

上下文检索是一种 RAG 技术,它在嵌入每个块并为其建立索引以进行关键字搜索之前,在每个块前面添加一个简短的、文档感知的解释,因此块在单独阅读时仍然有意义。

听听这个指南AI叙述 · ~3 min

在本页4 分钟阅读
  1. 概述
  2. 深入探讨
  3. 现实世界的实施
  4. 上下文检索的未来
  5. 付诸实践
  6. 不断探索
  7. 常见问题

概述

这很重要,因为当从文档中分割出来时,块通常会丢失关键信息,例如它们所指的公司、产品或时间段,而这种丢失会导致检索器错过正确的段落。

深入探讨

Standard RAG splits documents into chunks, embeds each chunk, and retrieves the chunks most similar to a question. The problem is that splitting strips context. A chunk that says "the company's revenue grew by 3%" does not say which company or which period, so neither semantic search nor keyword search can reliably match it to a question that names them. Anthropic described contextual retrieval in September 2024. For each chunk, a language model is given the whole document and the chunk, and asked to write a brief context (typically around 50 to 100 tokens) that situates the chunk within the document. That context is prepended to the chunk. The combined text is then used in two indexes: an embedding index (contextual embeddings) and a BM25 keyword index (contextual BM25). At query time, results from both are combined, and optionally a reranker reorders the top candidates. In Anthropic's reported experiments across several datasets, contextual embeddings plus contextual BM25 reduced the top-20 retrieval failure rate by about 49 percent compared with standard embeddings, and adding reranking brought the reduction to about 67 percent. Results on your own data can differ, so it is worth measuring. The cost is at indexing time: one model call per chunk, each including the full document. Prompt caching makes this much cheaper, because the document is cached once and reused across all its chunks. Misconceptions to avoid: the added context is used for retrieval, and you can choose whether to show it to the generator. It does not replace good chunking, and for small knowledge bases that fit in the model's context window, sending everything directly may be simpler than any retrieval pipeline.

现实世界的实施

季度报告中的一大块内容只是“收入比上一季度增长了 3%”;上下文检索预先假定它来自特定公司的第二季度备案,因此命名该公司和季度的搜索可以找到它。

在员工手册中,“这在 90 天后适用”的一大块内容会获得一个前缀,解释它来自远程工作资格部分。

软件文档助手添加上下文,指出配置块属于 API 的版本 3,从而减少与版本 2 中的类似文本的混淆。

支持知识库在故障排除块中添加了它们所涵盖的产品型号的前缀,因此有关一种型号的问题将停止检索另一种型号的步骤。

上下文检索的未来

上下文检索反映了一种更广泛的趋势,即在索引时花费更多计算以使查询时间更加可靠。随着模型调用变得更便宜并且缓存成为标准,向块添加上下文可能会成为许多 RAG 管道中的默认步骤。较长的上下文窗口可以减少小集合中的检索需求,但不会减少大集合中的检索需求,因此此类技术应该与大型知识库保持相关性。预计会有更多与元数据过滤、知识图和后期交互检索的组合,团队根据测量的回忆而不是时尚进行选择。

付诸实践

为什么这很重要

  • 成本与预算. 多年来,架构决策决定着性能和运营成本。
  • 更清晰的判决. 技术教育帮助团队选择正确的堆栈,而不仅仅是最新的堆栈。
  • 质量控制. 更好的工程选择可以减少生产中的可靠性事故。

留意

  • 优化一项基准测试可以隐藏更广泛的系统弱点。
  • 基础设施和维护成本常常被低估。
  • 随着系统变得更加复杂,安全性和可观察性差距可能会扩大。

后续步骤

  1. 在实施之前定义延迟、质量和成本目标。
  2. 在实际负载和数据条件下进行基准测试。
  3. 仪器监控错误、漂移和用户影响。
  4. 在扩展之前准备回滚和事件响应路径。

不断探索

免费通讯

获取每日AI简报

每个工作日早晨提供三条经过验证的AI故事,使用简明英文撰写。永久免费,无广告。

每个工作日发送一封邮件。一键退订。我们永不出售或分享您的地址。

测试一下自己

参加上下文检索测验

每个答案即时反馈,课程通过后可获得可验证ID的可分享证书。

开始测验

支持免费AI教育。AI Understanding是一家501(c)(3)非营利组织——无广告,无付费墙,永不收费。 捐款

常见问题

什么是上下文检索?

上下文检索是一种 RAG 技术,它在嵌入每个块并为其建立索引以进行关键字搜索之前,在每个块前面添加一个简短的、文档感知的解释,因此块在单独阅读时仍然有意义。这很重要,因为当从文档中分割出来时,块通常会丢失关键信息,例如它们所指的公司、产品或时间段,而这种丢失会导致检索器错过正确的段落。

上下文检索解决什么问题?

拆分会删除周围的上下文,因此孤立的块可能不会提及问题所询问的实体或时期。

每个块的上下文是如何生成的?

该模型获得完整文档和块,并返回将块放入文档中的简短上下文。

Anthropic 的方法中哪两个索引接收上下文化块文本?

该方法使用上下文嵌入和上下文 BM25,然后组合它们的结果。

根据Anthropic报道的实验,上下文嵌入加上上下文BM25大约减少了多少top-20检索失败?

Anthropic 报告减少了约 49%,重新排名后上升至约 67%。其他数据的结果可能有所不同。

为什么即时缓存会使上下文检索更便宜?

每个块的调用都包含完整的文档;缓存共享前缀可以避免重复支付全价。