Hypothetical Document Embeddings (HyDE)
What You'll Learn
You'll learn how generating a fake "ideal answer" and embedding it can lead to better retrieval than embedding your raw question.
The Core Problem
When you embed a user's question like "What causes rust?", you're searching for documents that are semantically similar to a question. But your knowledge base contains answers, not questions. This semantic mismatch can hurt retrieval quality.
How HyDE Works
Step 1: Generate a Hypothetical Answer
Use your LLM to generate what a good answer might look like, even if it's imperfect or contains inaccuracies:
Query: "What causes rust?"
Hypothetical answer: "Rust forms when iron reacts with oxygen
and moisture in the air, creating iron oxide..."
Step 2: Embed the Hypothetical Document
Instead of embedding the question, embed this generated answer.
Step 3: Retrieve with the Hypothetical Embedding
Search your vector database using this embedding. Since real answers and your hypothetical answer share semantic space, you get better matches.
The Analogy
Imagine asking a librarian "Where's a book about rust?" versus describing what the book might say: "I need a book explaining how iron reacts with oxygen to form rust." The second approach gives the librarian better clues about which book you actually need.
When to Use HyDE
- Short, ambiguous queries that lack context
- Domain-specific searches where question-answer mismatch is high
- When you have LLM budget for an extra generation step before retrieval
Key Takeaway: HyDE bridges the semantic gap between questions and answers by generating a hypothetical answer document, embedding it, and using that embedding for retrieval—often finding more relevant results than embedding the raw query.