Courses / RAG Systems / Module 7
Module 7: Query understanding: rewriting, HyDE, multi-query, decomposition, routing
8 h · theory 2 · hands-on 3 · project 2 · revision 1
You will be able to
- Rewrite conversational follow-ups into standalone queries
- Use HyDE and multi-query where they help
- Decompose multi-part questions
- Route queries to the right index or tool
Capstone increment: Query rewriter and router in front of the retriever, each measured on the golden set.
Job-description skills: Query understanding · Lab tools: openai, pydantic
1. Users don’t write search queries
Real questions are follow-ups (“and for interns?”), vague (“leave rules”), multi-part (“compare the 2023 and 2025 travel policy”), phrased in different words from the documents, or not questions for your documents at all (“how many tickets did we close last week?”). Retrieving on the raw text fails on all of these.
Query understanding is a small pipeline in front of the retriever: rewrite, route, and sometimes expand or decompose. Each step costs a model call and latency, so each one has to earn its place on your golden set.
2. Rewrite follow-ups into standalone questions
In a conversation, the current turn often only makes sense with the previous ones. A condense-question step asks a fast model to rewrite the latest turn, given the recent history, into a standalone query that keeps every constraint (“How many sick leave days do interns get?”). Use structured output so the rewrite is a field you can log and evaluate.
Measure it on a labelled set of follow-ups: does retrieval on the rewrite find the right chunks? Watch for rewrites that drop a constraint (“for interns”) or add one the user never said.
3. HyDE and multi-query
HyDE (Gao and others, 2022) asks a model to write a hypothetical answer and embeds that instead of the question, because a fake answer looks more like real answers than a short question does. It helps when queries are short or phrased very differently from the documents; it can hurt when the model invents specifics that pull retrieval the wrong way.
Multi-query generates three to five rephrasings, retrieves for each and fuses the lists with RRF (often called RAG-Fusion). It improves recall on vague questions at the cost of several retrievals and one generation call. Step-back prompting asks a more general question first (“what is the leave policy?”) to fetch background before the specific one.
4. Decompose multi-part questions
“How do annual leave rules differ between the 2023 and 2025 policies?” needs two retrievals, one per policy year, then an answer that compares them. Decomposition asks the model for sub-questions, retrieves for each, and answers with all the contexts, citing each.
Independent sub-questions can run in parallel. When one answer is needed to ask the next (“who approves claims for the team that owns billing?”), the question is multi-hop, and the agentic RAG patterns of Module 13 fit better.
5. Route to the right place
Not every question belongs in the vector index. Questions about counts and trends belong in SQL over the ticket database; status questions belong in an API; small talk needs no retrieval at all; some questions should go to a different index or carry filters (year, document type, region) extracted from the text, sometimes called self-querying.
Start with rules you can test, then add an LLM classifier with a fixed output schema for what rules can’t decide, or an embedding-similarity router over example queries per route. Always keep a default route and log the decision with its reason. Measure routing accuracy on labelled queries, and track it per route.
Hands-on: coding window
The coding window and the full VS Code lab are available after you sign in.
Sign in to codeLabs
Lab 7.1: Condense-question rewriter
Build an LLM rewriter with structured output that turns the latest turn plus history into a standalone query. Label 20 follow-up questions and compare recall@5 with and without it.
Expected: Recall with and without rewriting, and three examples where the rewrite dropped or invented a constraint.
Lab 7.2: HyDE and multi-query
On the vague-question segment of the golden set, compare baseline, HyDE and multi-query (4 rephrasings with RRF). Record latency and cost per question for each.
Expected: A table of recall, latency and cost; a decision on which segment gets which method.
Lab 7.3: Router (capstone increment)
Add src/query/route.py routing to policies, tickets (SQL) or no retrieval, with year and doc-type filters, and a labelled set of 40 queries in tests/.
Expected: Routing accuracy of at least 90% in a pytest test that runs in CI.
Go deeper
Videos
- RAG from scratch: Part 5 (Query Translation -- Multi Query) · LangChain · ~6 min. Multi-query retrieval in six minutes, with code.
- RAG from scratch: Part 7 (Query Translation -- Decomposition) · LangChain · ~7 min. Decomposing complex questions into sub-questions, answered sequentially or in parallel.
- RAG from scratch: Part 9 (Query Translation -- HyDE) · LangChain · ~5 min. HyDE implemented and explained, including why a fake answer can retrieve better.
- RAG from scratch: Part 10 (Routing) · LangChain · ~7 min. Logical and semantic routing to different data sources.
Further reading
- Paper Precise Zero-Shot Dense Retrieval without Relevance Labels (HyDE) · Gao et al., 2022. Embed a hypothetical answer instead of the question; the paper behind HyDE.
- Paper Query Rewriting for Retrieval-Augmented Large Language Models · Ma et al., 2023. Rewrite-retrieve-read, with a small trained rewriter.
- Paper Least-to-Most Prompting Enables Complex Reasoning in Large Language Models · Zhou et al., 2022. Decompose a hard question into easier sub-questions answered in order.
GitHub repositories
- langchain-ai/rag-from-scratch: Notebooks 5 to 10: query translation (multi-query, RAG-Fusion, decomposition, step-back, HyDE) and routing.
- texttron/hyde: The original HyDE code; read how the hypothetical documents are generated and averaged.
- run-llama/llama_index: Router query engines and query transformations in the docs’ advanced retrieval section.
Checkpoint quiz
Interview questions
- How do you handle follow-up questions in a conversational RAG system?
- When would you use HyDE, multi-query or decomposition? What does each cost?
- Design a router that sends questions to documents, SQL or an API.
- How do you extract metadata filters from a natural-language question safely?
- Your rewriter improved recall overall but broke one segment. What do you do?
Ask the professor
Sign in to ask questions about this module.
Discussion
Community discussion appears here once giscus is configured (see README).