Overview
HyDE (Hypothetical Document Embeddings) is a novel RAG architecture that inverts the traditional retrieval process:- Instead of embedding the query, generate a hypothetical ideal answer to the query
- Embed the hypothetical answer (which is longer and more detailed than the query)
- Retrieve documents similar to the hypothetical answer
- Generate the final answer based on the retrieved real documents
How It Works
Pipeline Steps
- Query Analysis: Receive user’s question
- Hypothetical Document Generation: Use an LLM (gpt-3.5-turbo with temperature=0.7) to generate a detailed, hypothetical medical document that would answer the question
- Semantic Retrieval: Embed the hypothetical document and retrieve real documents similar to it
- Answer Generation: Use a more powerful LLM (gpt-4o) to generate the final answer from real retrieved documents
HyDE uses two LLM calls: one creative call (temperature=0.7) to generate the hypothetical document, and one precise call (temperature=0) to generate the final answer.
Why This Works
- Vocabulary matching: Hypothetical documents naturally use similar vocabulary to real documents
- Richer query representation: Full paragraph vs. short question provides more semantic signal
- Query expansion: Hypothetical document includes related concepts and terms
- Dense information: More tokens to embed means more semantic information
Key Features
- Two-stage LLM usage: Separate models for generation and answering
- Creative hypothesis generation: Higher temperature (0.7) for diverse, detailed hypothetical documents
- Semantic-only retrieval: Uses only the hypothetical document for retrieval, not original query
- Detailed metrics: Tracks costs and tokens for both LLM calls separately
Implementation Details
Model Configuration
Hypothetical Document Generation Prompt
Core Processing Function
Example Hypothetical Document
For the query “¿Qué es la preeclampsia?”, HyDE might generate:Usage with query_for_evaluation()
Return Structure
When to Use This Approach
Best For
- Vocabulary gap queries: When users ask questions with different terminology than documents
- Short, ambiguous queries: HyDE expands sparse queries into rich documents
- Conceptual questions: When semantic meaning is more important than exact keywords
- Cross-lingual scenarios: Hypothetical documents can bridge language/dialect differences
- Exploratory retrieval: When you want to find documents conceptually related to the answer
Advantages Over Other Methods
- Bridges vocabulary gap: Hypothetical document uses corpus vocabulary automatically
- Query expansion: Single query becomes rich, multi-faceted document
- Semantic richness: More tokens = more semantic information for retrieval
- Handles ambiguity: LLM interprets vague queries into concrete documents
Limitations
- Higher cost: Two LLM calls instead of one (~50-100% more expensive)
- Higher latency: Additional LLM call adds ~1-2 seconds
- Hallucination risk: Hypothetical document may include incorrect information
- Retrieval bias: Retrieves documents similar to what LLM thinks answer should be
- No keyword precision: Pure semantic search may miss exact term matches
Performance Characteristics
Speed
- HyDE generation: ~1-2 seconds (gpt-3.5-turbo)
- Retrieval: ~0.5-1 second (semantic search)
- Answer generation: ~1-2 seconds (gpt-4o)
- Total: ~4-6 seconds (slower than other methods)
Cost
- HyDE LLM call: ~$0.0002-0.0004 (gpt-3.5-turbo, ~200-300 output tokens)
- Embedding: ~$0.00001 (hypothetical document embedding)
- Answer LLM call: ~$0.002-0.005 (gpt-4o)
- Total: ~$0.003-0.006 per query (50-100% more expensive than simple semantic)
Quality
- Best for vocabulary gaps: Outperforms other methods when query/document vocabulary differs
- Good for short queries: Expansion improves retrieval for sparse queries
- Variable performance: Quality depends on hypothetical document quality
- Can improve or hurt recall: May retrieve conceptually related but not directly relevant docs
Prompt Engineering for HyDE
The quality of hypothetical documents directly impacts retrieval. Key prompt elements:Good HyDE Prompts
- Specify document type: “Write a medical guide section” vs. “Answer this question”
- Request detail: “Detailed and comprehensive” encourages rich generation
- Include structure hints: List specific elements to include
- Use domain language: “Clinical details”, “medical recommendations”
- Set tone: “Official medical guide” vs. “patient education material”
Example Variations
Comparison with Other Architectures
Advanced: Multi-Hypothesis HyDE
You can extend HyDE by generating multiple hypothetical documents:Source Files
- Implementation:
~/workspace/source/src/rag/hyde.py:172-225 - Hypothetical doc generation:
~/workspace/source/src/rag/hyde.py:106-130 - HyDE prompt:
~/workspace/source/src/rag/hyde.py:63-80 - Evaluation interface:
~/workspace/source/src/rag/hyde.py:228-302
