概述
這很重要,因為使用者常會提出模糊、口語或多部份的問題,這些問題的用詞與文件不吻合,而更好的查詢能夠顯示出單純字面搜尋可能遺漏的相關段落。
深入探討
Retrieval quality depends heavily on the query. Users write short, ambiguous or conversational questions, while documents use their own vocabulary. Query transformation closes that gap before search happens. The simplest form is rewriting: an LLM turns the user's message into a clearer search query. This is essential in multi-turn chat, where follow-ups like "and in 2023?" mean nothing without history, so the system condenses the conversation into a standalone query. Decomposition splits a complex question into sub-questions that can be retrieved separately, useful for comparisons or multi-hop questions. Step-back prompting, described by Google DeepMind researchers in 2023, asks a more general question first to retrieve background principles. Multi-query retrieval generates several variants of the question, runs a search for each, and merges the results. LangChain popularized this with its MultiQueryRetriever. RAG-Fusion combines multiple queries with reciprocal rank fusion (RRF), a method described by Cormack, Clarke and Buettcher in 2009, which scores each document by summing 1/(k + rank) across result lists. Documents that appear high in several lists rise to the top without needing comparable raw scores. A related technique, HyDE (Hypothetical Document Embeddings, Gao and colleagues, 2022), has the model write a hypothetical answer and embeds that instead of the question, because an answer-shaped text often sits closer to real answer passages in embedding space. Misconceptions: more queries are not always better. Each variant adds latency and cost, and poorly generated variants can drift from the user's intent, pulling in irrelevant documents. Rewriting can also drop important constraints like dates or product names. Measure recall and answer quality on real questions before adopting these steps.
現實世界的實施
在聊天助理中,後續的「what about for part-time staff?」會根據先前的對話改寫成獨立查詢,例如「parental leave policy for part-time employees」。
一個比較兩家雲端服務供應商價格的問題會被分解為對每個供應商的個別搜尋,然後在回答前將結果合併。
支援機器人會產生四種「my app keeps logging me out」的說法,包含 session expiry 與 token refresh 等技術詞彙,並使用 reciprocal rank fusion 將結果融合。
研究助理使用「step‑back prompting」先搜尋窄題背後的通則,再檢索具體細節,給模型同時提供背景與具體資訊。
查詢改寫與多查詢檢索的未來
隨著能動式 RAG 的普及,查詢轉換越來越由模型自行決定要搜尋什麼、在結果顯得薄弱時進行迭代,而不是透過固定的改寫步驟。這使得背後的概念、分解、變體與融合變得更為重要,即使它們變得不那麼顯眼。嵌入模型和混合搜尋的改進雖然減少了一些詞彙不匹配的情況,卻仍無法消除對模糊或對話式問題進行澄清的需求。預計各團隊會有選擇性地使用這些技術,當查詢看起來複雜或首次檢索結果薄弱時觸發,以在品質與成本之間取得平衡。
付諸實踐
為什麼這很重要
- 成本與預算. 多年來,架構決策決定著效能和營運成本。
- 更明確的決策. 技術教育幫助團隊選擇正確的堆疊,而不僅僅是最新的堆疊。
- 品質管控. 更好的工程選擇可以減少生產中的可靠性事故。
留意
- 優化一項基準測試可以隱藏更廣泛的系統弱點。
- 基礎設施和維護成本常常被低估。
- 隨著系統變得更加複雜,安全性和可觀察性差距可能會擴大。
後續步驟
- 在實施之前定義延遲、品質和成本目標。
- 在實際負載和資料條件下進行基準測試。
- 儀器監控錯誤、漂移和使用者影響。
- 在擴展之前準備回滾和事件回應路徑。
不斷探索
常見問題
什麼是 Query Rewriting 與 Multi-Query Retrieval?
Query rewriting 與 multi-query retrieval 是 RAG 技術,會在搜尋前改寫使用者的問題,透過重新措辭、拆解成子問題,或產生多個變體並合併結果。這很重要,因為使用者常會提出模糊、對話式或多部份的問題,字句與文件內容不匹配,而更好的查詢能找出單純文字搜尋無法捕捉的相關段落。
為什麼在多輪對話中 query rewriting 尤其重要?
像「而在 2023 年呢?」這樣的追問需要對話歷史才能轉化為有用的獨立查詢。
哪種技術會將比較問題拆分為針對每個被比較項目的獨立搜尋?
分解會將複雜或多部份的問題拆成子問題,分別檢索。
回溯提示(step-back prompting)有什麼作用?
回溯提示,來自 Google DeepMind 研究人員,在具體細節之前先取得一般背景資訊。
在 reciprocal rank fusion 中,文件的分數如何計算?
RRF 會對每個包含該文件的列表,以 1/(k + 排名) 的方式加總,獎勵在多個列表中排名較高的文件。
為什麼 RRF 在合併向量搜尋與 BM25 結果時很方便?
因為 RRF 只使用排名位置,它可以結合原始分數不可比的列表。