sharpbyte.dev
← RAG
RAG · topic 9 of 13

HyDE retrieval

Hypothetical document embeddings align queries with answer-like text for better dense retrieval.

Traditional RAG vs HyDE

Traditional RAG vs HyDE Another critical problem with the traditional RAG system is that questions are not semantically similar to their answers. As a result, several irrelevant contexts get retrieved during the retrieval step due to a higher cosine similarity than the documents actually containing the answer. HyDE solves this. The following visual depicts how it differs from traditional RAG and HyDE.

Problem statement: questions are often not semantically close to the passages that contain answers.
Problem statement: questions are often not semantically close to the passages that contain answers.

Let's understand this in more detail. As mentioned earlier, questions are not semantically similar to their answers, which leads to several irrelevant contexts during retrieval. HyDE handles this as follows:

Traditional embedding of the question vs HyDE’s hypothetical document as a retrieval proxy.
Traditional embedding of the question vs HyDE’s hypothetical document as a retrieval proxy.
HyDE flow: generate a synthetic answer-like passage, embed it, retrieve, then ground the real answer.
HyDE flow: generate a synthetic answer-like passage, embed it, retrieve, then ground the real answer.

Use an LLM to generate a hypothetical answer H for the query Q (this answer does not have to be entirely correct). Embed the answer using a contriever model to get E (Bi-encoders are famously used here). Use the embedding E to query the vector database and fetch relevant context (C). Pass the hypothetical answer H + retrieved-context C + query Q to the LLM to produce an answer. Done! Now, of course, the hypothetical generated will likely contain hallucinated details. But this does not severely affect the performance due to the contriever model - one which embeds. More specifically, this model is trained using contrastive learning and it also functions as a near-lossless compressor whose task is to filter out the hallucinated details of the fake document. This produces a vector embedding that is expected to be more similar to the embeddings of actual documents than the question is to the real documents: Several studies have shown that HyDE improves the retrieval performance compared to the traditional embedding model.

Steps: hypothetical answer H from Q, embed H, fetch supporting context C, then answer with Q+H+C.
Steps: hypothetical answer H from Q, embed H, fetch supporting context C, then answer with Q+H+C.
Why a contriever-style encoder can tolerate hallucinated hypotheticals yet still improve recall.
Why a contriever-style encoder can tolerate hallucinated hypotheticals yet still improve recall.

Key takeaways

  • Question–answer geometry mismatch hurts cosine retrieval; HyDE bridges with a synthetic passage embed.
  • Gains trade off against extra LLM calls and latency.