HyDE: Hypothetical Document Embeddings
What is HyDE? HyDE (Hypothetical Document Embeddings) is a retrieval technique that generates a hypothetical answer to the query, then uses that to find similar real documents.
The Problem HyDE Solves User queries and documents often have vocabulary mismatch:
- Query: "How to fix slow database?"
- Document: "PostgreSQL query optimization using indexing..."
Direct embedding similarity may not connect these well.
How HyDE Works
User Query
|
v
[LLM generates hypothetical answer]
|
v
Hypothetical Document
|
v
[Embed hypothetical document]
|
v
[Search for similar real documents]
|
v
Retrieved Documents
Implementation
def hyde_search(query: str, vector_store, llm) -> list:
# Generate hypothetical answer
hypothetical = llm.generate(f"""
Write a detailed answer to this question:
{query}
Write as if you are writing a document that would answer this.
""")
# Embed the hypothetical document
hypo_embedding = embed(hypothetical)
# Search with hypothetical embedding
results = vector_store.search(hypo_embedding, top_k=10)
return results
Why It Works
| Aspect | Standard Query | HyDE |
|---|---|---|
| Vocabulary | User language | Document language |
| Detail level | Brief question | Expanded context |
| Semantic space | Question space | Answer space |
The hypothetical document is in the same semantic space as real documents, improving similarity matching.
When to Use HyDE
| Scenario | Recommendation |
|---|---|
| Technical documentation | Good fit |
| Diverse vocabulary | Very helpful |
| Short queries | Benefits most |
| High precision critical | Worth the latency |
Limitations
- Adds LLM call latency
- Hypothetical may be wrong (can mislead retrieval)
- Works best with capable LLMs
- Not necessary if query matches document vocabulary well
Variants
- Multi-HyDE: Generate multiple hypothetical docs, combine results
- Query + HyDE: Use both original query and hypothetical embedding
- Domain-specific prompts: Tailor hypothetical generation to domain
hydehypothetical document
Explore 500+ Semiconductor & AI Topics
From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.