M5-081M5: NLP & Large Language ModelsRAG End-to-End ArchitectureHard
⚡Flashcard Drill➔Mastery:

Query Transformation Techniques: HyDE, Multi-Query, and Step-Back Prompting

📐 Mathematical Definition
HyDE: q→d^=LLM(q)→retrieve by E(d^);multi-query: {q1,…,qm}\text{HyDE}:\ q\to\hat d=\text{LLM}(q)\to\text{retrieve by }E(\hat d);\qquad \text{multi-query}:\ \{q_1,\dots,q_m\}
⚡ One-Sentence Core Answer
Query transformation bridges the vocabulary and semantic gap between short colloquial user queries and detailed technical documents using hypothetical document generation, multi-query expansion, and abstract step-back reasoning.

📌 Key Takeaways

  • •
    Query-document asymmetry: user queries are short, colloquial, and sparse ('blue screen on boot'); reference documents are long, formal, and technical ('Kernel panic error 0x0000003B'); direct vector similarity fails
  • •
    HyDE (Hypothetical Document Embeddings): prompts an LLM to generate a hypothetical answer to the query; uses the hypothetical answer's embedding to search the vector space (matching document-to-document)
  • •
    Multi-Query & Step-Back: Multi-Query expands query into 3-5 paraphrased variants to capture diverse lexical angles; Step-Back prompting abstracts the query to retrieve foundational domain concepts

📐 Mathematical Derivations

Mathematical Mechanism: 1. The Asymmetry Bottleneck: User query qq has length ∣q∣≪∣d∣|q| \ll |d|. Vector embedding E(q)E(q) resides in the 'question manifold' of the latent space, while passage embeddings E(d)E(d) reside in the 'answer manifold'. The semantic distance ∥E(q)−E(d)∥2\|E(q) - E(d)\|_2 is artificially large even when dd answers qq. 2. HyDE (Hypothetical Document Embeddings) Formulation (Gao et al. 2022): 1. Prompt an instruction-tuned LLM with zero-shot generation: d~=LLM(Generate a hypothetical passage that answers: q)\tilde{d} = \text{LLM}(\text{Generate a hypothetical passage that answers: } q) 2. Compute the dense embedding of the generated hypothetical passage: vHyDE=E(d~)v_{\text{HyDE}} = E(\tilde{d}) 3. Retrieve documents nearest to vHyDEv_{\text{HyDE}} via vector search: R=arg⁡max⁡d∈Dcos⁡(vHyDE,E(d))\mathcal{R} = \arg\max_{d \in \mathcal{D}} \cos(v_{\text{HyDE}}, E(d)) Because d~\tilde{d} is written in the linguistic style, length, and technical vocabulary of a reference document, its embedding lives directly inside the document manifold, bypassing query-document asymmetry.

🏭 Production Trade-offs

Deep Dive & Engineering Trade-offs: ① When HyDE Excels vs Fails: - HyDE excels on open-ended explanatory questions ('How does speculative decoding work?') where the model knows the general terminology even if it lacks specific private data. - HyDE fails catastrophically on factual lookup of unknown private entities ('What is Suiyao's phone number?'): the model hallucinates a plausible fictional number, and vector search retrieves documents matching the hallucinated fiction rather than the truth. ② Multi-Query Expansion & Fusion: Prompt the LLM to rewrite query qq into 3-5 alternative queries covering different synonyms and perspectives. Retrieve top-20 documents for each query independently, and combine all results via Reciprocal Rank Fusion (RRF). Robustly catches multi-perspective queries. ③ Step-Back Prompting: Abstracts specific queries to broad high-level principles (e.g., 'Why does my PyTorch CUDA out-of-memory error occur on line 42?' →\to 'What are the principles of PyTorch CUDA memory management?'). Retrieves high-level architectural documentation to guide specific debugging. ④ Latency Cost Multiplier: Every query rewriting or HyDE step requires a full LLM generation call before retrieval can begin, adding 200–500 ms200\text{--}500\text{ ms} to total TTFT. Deploy selectively using query intent classification. ⑤ Interview Strategy: Explain query-document asymmetry as the fundamental motivation, sketch the HyDE 3-step pipeline, and articulate the key limitation (hallucination on private factual entities).
⚠️ Common Interview Pitfalls
  • ✕
    Using HyDE on private, unknown entities where the LLM's hallucinated hypothetical document derails vector search
  • ✕
    Applying query expansion unconditionally to simple, clear queries (adds unnecessary latency and API cost)
  • ✕
    Using multi-query retrieval without a rank fusion algorithm like RRF to de-duplicate candidate chunks
🎯 Interviewer Follow-up Inquiries
  • ?
    Why does HyDE succeed on conceptual questions even when the generated hypothetical document contains factual inaccuracies?
  • ?
    How does Step-Back prompting improve retrieval for complex multi-step reasoning queries?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔
📚

Associated Knowledge Base Guides & Mindmaps

Explore the comprehensive technical article, exam cards, and global architecture tree.

← PreviousM5-080: Embedding Model Selection, Dimensions, and Domain Fine-Tuning📋Back to BankNext →M5-082: GraphRAG and Structured Knowledge Retrieval
中文速览与面试要点

解释 query 改写与 HyDE。

用 LLM 改写/扩展查询以弥合'查询-文档'的表达差异;HyDE 先生成假设答案再检索(用答案的嵌入更接近文档)。

核心要点

  • 数学机理: 核心问题:查询-文档不对称(query-document asymmetry) ——用户的查询通常 短 (几个词)、 口语化 、 信息不完整 ;而文档是 长 、 正式 、 信息完整 的。两者的'表达形式'差异使嵌入空间的相似度不可靠(短查询的嵌入与长文档的嵌入不在'同一风格')。 三类改写方法 : (1) 查询改写/扩展(query rewriting / expansion) ——用 LLM (a) 补全上下文 ('它'指什么?);(b) 扩展同义词与相关术语 ('心脏病' → '心血管疾病、心梗、冠心病');(c) 分解为子查询 (多跳问题拆成多个单跳查询);(d) 纠正错别字与语法 。 好处 ——提升召回(更多相关文档被命中)。 风险 ——过度扩展会引入噪声(召回不相关文档)。 (2) 多查询(multi-query / query generation) ——用 LLM 生成 m 个不同视角的改写 ,各自检索,再用 RRF 融合结果。 好处 ——覆盖不同表达(提升召回); 代价 ——m 倍检索成本。 (3) HyDE(Hypothetical Document Embeddings,Gao 等 2022) ——关键洞察: '用假设答案检索'比'用问题检索'更准 。做法:让 LLM 对查询 生成一个'假设的答案文档' (即使内容是编造的),然后用 这个假设文档的嵌入 去检索真实文档。 为什么有效 ——假设答案的 形式与真实文档相似 (都是'陈述性的长文本'),故其嵌入与真实文档'同分布',从而检索更准;这 弥合了 query-doc 的表达差异 (把'短查询'转换为'长文档形式')。 风险 ——若 LLM 生成的假设答案 方向错误 (幻觉出无关内容),则检索会偏离;故 HyDE 对'模型领域知识不足'的查询可能失效(此时不如直接检索)。 其他 ——(a) 查询分类/路由 (判断是否需要检索、走哪条检索路径);(b) 查询分解 (多跳问题拆解)。 实践组合 ——常'多查询 + 融合 + 重排',或'HyDE + 混合检索';需按查询类型选择(简单事实查询不需要改写)。
  • 深度剖析与工程权衡:① 'query-doc 不对称'是理解改写价值的钥匙 ——它解释了'为什么直接检索短查询效果差';HyDE 的巧妙之处正是'把查询转换成文档的形式'。② HyDE 的适用边界 ——它对'模型有相关知识'的查询有效(能生成方向正确的假设答案);对'模型完全不了解的领域'(如企业内部术语)无效(会生成错误方向的假设)。故 HyDE 不是万能的。③ 改写的成本 ——每次改写都需一次 LLM 调用(增加延迟与成本);故应按查询复杂度 按需改写 (简单查询不改、复杂/多跳查询改写)。④ '多查询 + RRF 融合'的实践 ——生成 3~5 个改写、各自检索、RRF 融合,通常比单查询召回更好;成本可控(3~5 倍检索,但检索比生成便宜)。⑤ 与'查询分解'的关系 ——多跳问题('A 的创始人的母校在哪')需先查 A 的创始人、再查其母校;这是'迭代检索'而非'单次改写'(见多跳检索)。⑥ 面试要点 ——被问'查询改写有什么用',应给出' 弥合 query-doc 不对称 '这一核心,并列举'改写扩展 / 多查询融合 / HyDE'三类方法与各自风险;能指出'HyDE 依赖模型的领域知识、对陌生领域可能失效'是深度理解的标志。
  • 改写:补全指代、扩展同义词、生成多个查询
  • HyDE:生成'假设文档',用它的嵌入检索(弥合 query-doc 不对称)
  • 多查询:生成多个改写,各自检索后融合

常见陷阱

  • 对所有查询都做改写(浪费成本)
  • 对模型不熟悉的领域用 HyDE(假设答案方向可能错)