技術指南

情境檢索

情境檢索是一種 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%。不同資料的結果可能會有所差異。

為什麼提示快取會使情境檢索成本降低?

每個區塊的呼叫都包含完整文件;快取這個共用前綴可避免重複支付全額費用。