Modern AI Engineering

Lesson 10.8 · 23 min · video lesson

HyDE: Generating Hypothetical Documents to Improve RAG

What if the best way to search for an answer is to first make one up, even a slightly wrong one, and then search for documents that look like it?

In short: HyDE (Hypothetical Document Embeddings) improves RAG retrieval by asking an LLM to write a fake answer to the question, embedding that fake answer instead of (or together with) the question, and searching for real documents near it. Answers look like other answers, so the fake one lands closer to the real passage than a short question does. It costs an extra LLM call per query and can mislead search when the LLM has no idea about the topic.

What you will learn

  • What is RAG in simple words
  • What is the search problem in RAG
  • Why searching with the question is weak
  • What is HyDE
  • Why searching with a fake answer works better
  • How HyDE works step by step
  • A worked example of HyDE
  • A simple code example of HyDE
  • Advantages of HyDE
  • Disadvantages of HyDE
  • When to use HyDE
  • Summary

In this lesson

  1. RAG in simple words, and the search problem inside it
  2. Why searching with the question is weak
  3. What is HyDE, and why a fake answer works better
  4. How HyDE works step by step
  5. A worked example in code
  6. Advantages and disadvantages of HyDE
  7. Common mistakes and how to spot them
  8. Practice: try it yourself
  9. When to use HyDE, and summary

Key terms in this lesson

HyDE
Hypothetical Document Embeddings: searching with the embedding of an LLM-written fake answer.
Hypothetical document
An answer-shaped passage generated by an LLM only to guide retrieval.
Zero-shot retrieval
Retrieval in a domain without any labelled query-document training pairs.
Query-document asymmetry
The difference in length, wording and style between questions and the passages that answer them.
Query rewriting
Transforming the user's query before search, e.g. HyDE, multi-query or decomposition.

This lesson is part of the Generative AI Engineering and Complete AI Engineer tracks. See plans and pricing to open the full lesson, its video, labs and quiz, or try the free lesson first (free account needed).

← 10.7 Document Chunking Strategies for RAG · 10.9 Embedding Caches: Avoiding Redundant Embedding Calls →