The company's revenue grew by 3% over the previous quarter. → This chunk is from an SEC filing on ACME corp's performance in Q2 2023; the previous quarter's revenue was $314 million.
想象你在看一本电话簿,每个号码旁边只写了'家里'、'公司'、'妈妈'。没有名字,你完全不知道这是谁的号码。Contextual Retrieval 就像是给每个号码贴上便利贴,写上'这是张三公司的电话',一下子就能找到了。
flowchart LR
subgraph 传统 RAG
D1["文档"] --> C1["分块"]
C1 --> E1["嵌入"]
E1 --> Q1["查询: ACME Q2 收入?"]
Q1 -->|匹配失败| X["❌ 3% 增长"]
end
subgraph Contextual Retrieval
D2["文档"] --> C2["分块"]
C2 --> P["Claude 添加上下文前缀"]
P --> E2["嵌入"]
E2 --> Q2["查询: ACME Q2 收入?"]
Q2 -->|匹配成功| OK["✅ ACME Q2: 3% 增长"]
end
Please give a short succinct context to situate this chunk within the overall document for the purposes of improving search retrieval of the chunk.
就像图书馆给每本书贴标签 — 不是让读者自己翻遍书架(向量搜索),而是由一位经验丰富的管理员(Claude)提前阅读每本书,在封面上写一句'这本书讲的是 ACME 公司 2023 年 Q2 财报'。成本低廉,但搜索效率大大提升。
sequenceDiagram
participant Doc as 文档
participant Cache as Prompt Cache
participant Claude as Claude Haiku
participant Chunk as Chunk 1..N
Doc->>Cache: 首次加载文档(完整成本)
loop 对每个 chunk
Chunk->>Cache: 读取缓存的文档(低成本)
Cache->>Claude: 文档缓存 + 当前 chunk
Claude-->>Chunk: 返回上下文前缀
end
Contextual Embeddings reduced the top-20-chunk retrieval failure rate by 35% (5.7% → 3.7%). Combining Contextual Embeddings and Contextual BM25 reduced it by 49% (5.7% → 2.9%).
就像用两种不同的筛子淘金 — 一个筛子按颜色找金子(语义嵌入),另一个按形状找金子(BM25 关键词)。单独使用都会漏掉一些金子,但两个筛子一起用,几乎不会遗漏。
flowchart TD
subgraph 基线
A["检索失败率: 5.7%"]
end
subgraph 单一技术
B["Contextual Embeddings
失败率: 3.7%
降低 35%"]
C["Contextual BM25
失败率: ~4.5%
降低 ~20%"]
end
subgraph 混合叠加
D["CE + CBM25
失败率: 2.9%
降低 49%"]
end
A --> B
A --> C
B --> D
C --> D
Reranked Contextual Embedding and Contextual BM25 reduced the top-20-chunk retrieval failure rate by 67% (5.7% → 1.9%).
就像找工作筛选简历 — 先用关键词搜索和 AI 匹配快速筛出 150 份候选简历(粗排),再由 HR 仔细阅读每份简历的相关性,最终选出 20 份最匹配的(精排)。两步筛远比一步准。
flowchart LR Q["用户查询"] --> R1["粗排: 混合检索
Embeddings + BM25"] R1 -->|Top-150| RR["Reranker
交叉编码器精排"] RR -->|Top-20| F["最终结果
失败率 1.9%"] style Q fill:#e1f5fe style F fill:#c8e6c9
Sometimes the simplest solution is the best. If your knowledge base is smaller than 200,000 tokens (about 500 pages of material), you can just include the entire knowledge base in the prompt.
就像查地图 — 如果目的地就在隔壁小区,直接走过去就行(全文塞入);如果在本市,用导航选路线(Contextual Retrieval);如果跨省旅行,就需要详细规划+中转(Contextual Retrieval + Reranking)。工具的复杂度应该匹配问题的规模。
flowchart TD
S{"知识库大小?"}
S -->|"< 200K tokens"| A["全文塞入 Prompt
准确率 100%"]
S -->|"200K - 10M tokens"| B["Contextual Retrieval
准确率 ~97%"]
S -->|"> 10M tokens"| C["CR + Reranking
准确率 ~98%"]
style A fill:#c8e6c9
style B fill:#fff9c4
style C fill:#ffccbc