What is HyDE (Hypothetical Document Embeddings) and Multi-Query Expansion in RAG?
❓ Interview Prompt: Explain query transformation techniques: HyDE hypothetical answer generation and multi-query decomposition.
User queries are often short, vague, or structurally dissimilar to target document chunks. Query Transformation techniques rewrite incoming prompts before vector search:
1. HyDE (Hypothetical Document Embeddings):
Instructs an LLM to generate a hypothetical ideal answer to the user query. The hypothetical answer is embedded instead of the query! Because document-to-document similarity outperforms query-to-document similarity, retrieval precision improves dramatically.
2. Multi-Query Expansion:
Uses an LLM to generate 3 to 5 alternative paraphrased versions of the original query, executing parallel vector searches and merging results.
Code Implementation Example
async function hydeRetrieval(userQuery: string): Promise<DocumentChunk[]> {
// 1. Generate hypothetical answer document
const hypoPrompt = 'Write a detailed hypothetical technical answer to this question: "' + userQuery + '"';
const hypotheticalDoc = await llmClient.generate(hypoPrompt);
// 2. Embed hypothetical document instead of raw query!
const hypoVector = await embedText(hypotheticalDoc);
// 3. Search vector store using hypothetical document vector
return await vectorStore.query({ vector: hypoVector, topK: 5 });
}HyDE generates a hypothetical answer and embeds it, improving vector search by comparing document-to-document embeddings.