Day 3: Query Optimization for RAG: HyDE, Multi-Query Expansion, and Sub-Query Decomposition
Production RAG Masterclass · Day 3 of 7 Most RAG post-mortems blame the index. In practice, the query that hit the index was a vague chat message that never resembled the documents you stored. Figure 1. Fix the query shape before you retune the vector store. Thesis Raw user queries are short, vague, and poorly formatted for semantic matching. Optimize the query — then retrieve. The query–document mismatch User: “why is billing failing?” Doc: “Invoice reconciliation jobs abort when Stripe webhook signature validation returns HTTP 400…” Those strings barely share tokens. Embeddings help. A deliberate bridge helps more. Strip conversational noise first Before expansion, normalize the prompt: Drop politeness filler (“please”, “can you”, “thanks”). Drop chat history that is not about the current ask. Keep entities, IDs, product names, and constraints. def clean_query(q: str) -> str: return llm(f"Rewrite as a concise search query:\n{q}") ...