HyDE (Hypythetical Document Embedding)


HyDE (Hypothetical Document Embedding) is a technique used in Retrieval-Augmented Generation (RAG) systems to improve document retrieval by generating a hypothetical document based on the user's query.
Instead of directly using the user query for retrieval, HyDE leverages an LLM to create a synthetic document that represents the potential answer, and then uses this document's embedding for similarity search within the knowledge base.
Letβs get some insights with daigram :
Ek example se samjhata hoon abhi clear ho jayega ye concept :
π Scene:
Imagine youβre building a RAG (Retrieval-Augmented Generation) system. A user asks a question like:
"Ashoka ne Buddhism kyun apnaya?"
Now normally, RAG goes to your document database, pulls in some passages that seem relevant, and then answers.
But what if your database doesnβt have an exact match?
Matlab β jo dhoondhna tha, woh mila hi nahi. π
Bas kuch vague vague results aa rahe hain.
π‘ Enters: HyDE β Hypothetical Document Embeddings
Instead of directly searching, you ask the AI:
"Bhai, agar aisi document hoti jisme iska jawab hota, toh woh kya bolti?"
So the AI imagines an answer β ek hypothetical document banata hai β based on its knowledge.
Phir us imagined content ko embed karke vector search karta hai β taaki jo actual documents hain database mein, unse achhe se match kar sake.
π¦ Simple Analogy:
Maan lo tu kisi padosi se poochhta hai:
"Dilli ke Lajpat Nagar mein best chole bhature kahan milte hain?"
Lekin padosi Dilli gaya hi nahi kabhi.
Toh woh kya karega?
Sochke bolega:
"Agar main gaya hota na, toh mujhe lagta ki bade purane area mein, local dukaan hogi jiska bada naam hoga, line lagi hoti logon ki..."
Ab woh dimaag laga ke ek imaginary dukaan ka idea deta hai.
Aur us idea se tu Google ya kisi dost se real dukaan dhoondhta hai.
Wahi HyDE karta hai β ek sochi samjhi (hypothetical) document banata hai, uski embedding karta hai, aur fir search mein use karta hai.
π§ Step-by-step :
User asks:
"Ashoka ne Buddhism kyun apnaya?"AI thinks:
"Agar mujhe is pe ek document likhni hoti, toh main kya likhta?"AI writes something like:
"After the Kalinga war, Ashoka felt deep remorse and adopted Buddhism to seek peace and spiritual growth..."Ye hypothetical paragraph ko vector banake, database mein search karta hai.
Matches actual historical texts related to Ashoka, Kalinga war, and Buddhism β jo shayad directly query se nahi milte.
π― Summary:
HyDE = "Mann hi mann mein soch le, agar aisa likha hota, toh kya hota?"
Aur fir ussi imagination se asli cheezein dhoondh le."
Isse search aur relevant content milne ke chances bahut better ho jaate hain, especially jab query direct match na kare.
Ab actual code dekhte hain :
from openai import OpenAI
import requests
from dotenv import load_dotenv
import json
import os
from langchain_google_genai import GoogleGenerativeAIEmbeddings
from langchain_qdrant import QdrantVectorStore
from pathlib import Path
from collections import Counter
from langchain_community.document_loaders import PyPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_qdrant import QdrantVectorStore
from dotenv import load_dotenv
from openai import OpenAI
import os
import ast
load_dotenv()
# Load environment variables from .env file
load_dotenv()
apikey = os.environ["OPENAI_API_KEY"]
# Initialize OpenAI client
client = OpenAI(api_key=apikey)
# 1. Load and split PDF
pdf_path = Path(__file__).parent / "node_js_sample.pdf"
loader = PyPDFLoader(pdf_path)
docs = loader.load()
# Split document into chunks
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
)
split_docs = text_splitter.split_documents(documents=docs)
# 2. Create an embedder
embedder = OpenAIEmbeddings(
model="text-embedding-3-large",
api_key=apikey
)
# Only run the below once to insert data into Qdrant
# vector_store = QdrantVectorStore.from_documents(
# documents=split_docs,
# embedding=embedder,
# url="http://localhost:6333",
# collection_name="learning_node_js",
# )
# vector_store.add_documents(documents=split_docs)
# Connect to existing Qdrant vector store
retriever = QdrantVectorStore.from_existing_collection(
url="http://localhost:6333",
collection_name="learning_node_js",
embedding=embedder,
)
print("π PDF Ingestion Complete!\n")
user_input = input("Please enter your question on node js : > ")
system_prompt=f"""
you are a helpful assistant that provides relevant answer of user query
now give answer to the question
user input : ${user_input}
"""
print("\nπ§ LLM Thinking \n")
llm_answer = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role":"system","content":system_prompt}]
)
print("\nπ§ LLM generated answer\n")
print(llm_answer.choices[0].message.content.replace("*","").replace("`",""))
print("\nπ§ Preparing final answer : \n")
hypothetical_answer=llm_answer.choices[0].message.content.replace("*","").replace("`","")
query_embedding = embedder.embed_query(hypothetical_answer)
# Step 4: Use embedding to do similarity search
docs = retriever.similarity_search_by_vector(query_embedding, k=5)
# Step 5 (Optional): Final answer from LLM using context + question
context_text = "\n\n".join([doc.page_content for doc in docs])
final_prompt = f"""
You are a Node.js expert. Answer the user's question using the context below.
Context:
{context_text}
User question: {user_input}
"""
final_response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": final_prompt}
]
)
print("\nβ
Final Answer:\n")
print(final_response.choices[0].message.content.replace("*","").replace("`",""))
OUTPUT :
So thatβs all about HyDE! π§ β¨
Umeed karta hoon ki aapko sab kuch achhe se samajh aaya hoga ππ
Milte hain next blog/post/video mein! ππ
Tab tak ke liye β
π Keep Learning π
π Keep Exploring π
π Keep Growing π±π‘
Take care and happy prompting! π€π¬π
#ChaiCode
#GENAI
Subscribe to my newsletter
Read articles from Jaskamal Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
