Type: concept
Confidence: 0.95
Created: 2026-04-15
Updated: 2026-04-15
Tags: 技术AI方法论AI工程

情境化检索

概述

情境化检索(Contextual Retrieval)是 Anthropic 提出的 RAG 增强方案:在将文档 Chunk 建立嵌入向量和 BM25 索引之前,用 LLM 为每个 Chunk 自动生成 50–100 token 的情境说明并前置,从而保留文档上下文信息,大幅提升检索精度。

关键内容

核心问题:切块破坏上下文

传统 RAG 将文档切成独立 Chunk 时,每个 Chunk 会失去来自原文档的上下文信息:

问题:What was ACME Corp's revenue growth in Q2 2023?
原始 Chunk:"The company's revenue grew by 3% over the previous quarter."
缺失:哪家公司?哪个季度?→ 检索系统无法判断相关性

解决方案:情境化前置

用 Claude 为每个 Chunk 生成简洁的情境说明,然后前置到 Chunk 开头:

原始 Chunk:
"The company's revenue grew by 3% over the previous quarter."

情境化 Chunk:
"This chunk is from an SEC filing on ACME corp's performance in Q2 2023;
 the previous quarter's revenue was $314 million.
 The company's revenue grew by 3% over the previous quarter."

生成情境的 Claude Prompt(用 Claude 3 Haiku 生成):

<document>{{WHOLE_DOCUMENT}}</document>
Here is the chunk we want to situate within the whole document
<chunk>{{CHUNK_CONTENT}}</chunk>
Please give a short succinct context to situate this chunk within the overall
document for the purposes of improving search retrieval of the chunk.
Answer only with the succinct context and nothing else.

两个子技术

Contextual Embeddings(情境化嵌入): - 对情境化后的 Chunk 生成语义嵌入向量 - 单独效果:检索 Top-20 失败率 -35%(5.7% → 3.7%)

Contextual BM25(情境化 BM25): - 对情境化后的 Chunk 建立 BM25 词汇索引 - 与 Contextual Embeddings 联合:失败率 -49%(5.7% → 2.9%)

与重排序的组合(最强方案)

在情境化检索后加入 Reranking 步骤: - 先召回 Top-150 Chunk - 用重排序模型(如 Cohere Reranker)对照查询重新评分 - 取 Top-20 注入模型 - 联合效果:失败率 -67%(5.7% → 1.9%)

成本与实现

为什么成本可控:配合 AnthropicPrompt Caching 功能——整个文档只需载入缓存一次,对该文档的每个 Chunk 做情境化时直接引用缓存,无需重复传输。估算成本约 $1.02 / 百万文档 token

实现注意事项: 1. Chunk 边界:切块大小、边界策略和重叠影响检索效果,需实验调优 2. 嵌入模型选择:实验中 Gemini Text 004 和 Voyage 表现最佳 3. 情境化 Prompt 定制:可针对特定领域定制(如加入领域词汇表),比通用 Prompt 效果更好 4. Chunk 数量:Top-20 优于 Top-10 和 Top-5,但存在上限——注意 注意力预算 的影响

实验范围

跨代码库、小说、ArXiv 论文、科学论文四类知识域,多种嵌入模型(Gemini、Voyage、OpenAI 等),多种检索策略的系统性评测,情境化在所有配置下均改善检索性能。

效果堆叠总结

方案 Top-20 失败率 改善
仅嵌入(基准) 5.7%
嵌入 + BM25(标准 RAG) 约 4.5% ~21%
情境化嵌入 3.7% -35%
情境化嵌入 + 情境化 BM25 2.9% -49%
+ 检索重排序</td> <td>Reranking 1.9%

来源

相关