Home Knowledge Base HyDE: Hypothetical Document Embeddings

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:

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

AspectStandard QueryHyDE
VocabularyUser languageDocument language
Detail levelBrief questionExpanded context
Semantic spaceQuestion spaceAnswer space

The hypothetical document is in the same semantic space as real documents, improving similarity matching.

When to Use HyDE

ScenarioRecommendation
Technical documentationGood fit
Diverse vocabularyVery helpful
Short queriesBenefits most
High precision criticalWorth the latency

Limitations

Variants

hydehypothetical document

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.