Hypothetical document embeddings align queries with answer-like text for better dense retrieval.
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.
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:
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.