Course contentsShow
← AI Engineering
Lesson 385 of 1,88610. RAG - Improving RetrievalFree lesson

Hypothetical Document Embeddings (HyDE)

Generate hypothetical answers and use their embeddings to improve retrieval of relevant documents.

Hypothetical Document Embeddings (HyDE)

What You'll Learn

You'll discover how generating hypothetical answers first can dramatically improve the relevance of documents your RAG system retrieves.

The Core Idea

When a user asks a question, instead of embedding the question directly and searching for similar documents, HyDE takes a clever detour: it uses an LLM to generate a hypothetical answer—even if that answer might be wrong or hallucinated. Then it embeds that hypothetical answer and searches for documents similar to it.

Why does this work? Questions and answers live in different semantic spaces. The question "What causes rain?" uses different vocabulary than documents explaining precipitation. But a hypothetical answer like "Rain forms when water vapor condenses into droplets..." uses terminology much closer to actual meteorology documents.

The HyDE Process

  1. User asks a question: "How do I optimize database queries?"
  2. Generate hypothetical answer: Prompt an LLM to answer as if it knew (even if it doesn't have the real info)
  3. Embed the hypothetical answer: Convert this generated text into a vector
  4. Search with that embedding: Find documents similar to the hypothetical answer, not the original question
  5. Use retrieved docs: Pass the actual retrieved documents to the LLM for final generation

The Analogy

Imagine asking a librarian "Where are books about medieval castles?" versus describing what you expect to find: "I'm looking for books discussing stone fortifications, moats, battlements, and feudal architecture." The second approach—closer to what HyDE does—gives the librarian better vocabulary to match against actual book content.

Key Takeaway: HyDE bridges the semantic gap between questions and answers by generating hypothetical documents that "speak the language" of your knowledge base, leading to more relevant retrieval results.