Query Rewriting: HyDE, Multi-Query, and Decomposition

The query the user types is not the query your retriever wants. Rewriting bridges the gap before retrieval, so the index sees something closer to what the answer looks like.

Build 1.5 hr Python

Prerequisites: Phase 11 lessons 04 (embeddings), 06 (RAG); Phase 19 Track B foundations (lessons 20-29); Phase 19 lessons 64 and 65

#Learning Objectives

  • Implement Hypothetical Document Embeddings (HyDE): generate a fake answer, embed it, retrieve against that vector instead of the query vector.
  • Implement multi-query expansion: rewrite one query into N paraphrases, retrieve with each, merge the union by reciprocal rank fusion.
  • Implement query decomposition: split a complex question into sub-questions, retrieve per sub-question, merge.
  • Compare the three rewriters head to head on a fixture and explain when each strategy wins.
  • Wire a mock LLM that produces deterministic, on-fixture outputs so the rewriter loop runs offline.
Full Access lesson

Keep going. The rest of this lesson is one step away.

Unlock all 503 lessons, video, code, quizzes and every future update. Pay in your own currency with Paystack.

The text and code of this lesson are based on AI Engineering from Scratch by Rohit Ghumare, MIT licensed.