4.4.14 · HinglishAlignment, Prompting & RAG

Reranking and hybrid search

1,968 words9 min readRead in English

4.4.14 · AI-ML › Alignment, Prompting & RAG


1. Ye cheez chahiye hi kyun?

Ek naive RAG pipeline kuch is tarah kaam karta hai: embed query → nearest neighbours in vector DB → feed top-k to LLM. Do failure modes hain:

  1. Dense (vector) search exact terms miss karta hai. Embeddings meaning capture karte hain, isliye "ERR_4092" error code ya "Metformin" drug ki query ke liye semantically "nearby" lekin literally galat chunks retrieve ho sakte hain. Isme exact keyword match ka koi concept nahi hota.
  2. Sparse (keyword) search meaning miss karta hai. "car" ki search us doc ko match nahi karegi jo sirf "automobile" kehti hai.

80/20 insight: retrieval quality — prompt tweaking nahi — usually RAG accuracy ka sabse bada lever hota hai. Hybrid + rerank aksar kisi bhi prompt engineering se behtar perform karta hai.


2. Do retrievers — first principles se derive kiye

2.1 Sparse: BM25 (KYA aur KYUN)

Hum score karna chahte hain ki document query ke terms ke liye kitna relevant hai. Ye intuitions build karne hain:

  • Jo term mein zyada baar aata hai woh score badhaye (term frequency, TF) — lekin diminishing returns ke saath (50th "cat" 5th se zyada kuch nahi batata).
  • Jo term poore corpus mein rare hai woh zyada discriminating hota hai (inverse document frequency, IDF). "The" bekaar hai; "photosynthesis" gold hai.
  • Lambe documents ko term-count ka naajayaz fayda milta hai, isliye hum length se normalise karte hain.

In teeno ideas ko formula mein daalte hain:

Ye shape kyun?

  • Fraction saturate karta hai: jaise score ho jaata hai, diminishing returns deta hai. Yahi "50th cat" effect hai.
  • control karta hai kitni jaldi saturate hota hai; control karta hai kitni strongly length normalisation lagti hai ( ⇒ length ignore karo, ⇒ full normalisation).
  • , jahan = total docs, = wale docs — rare terms ko bada multiplier milta hai.

2.2 Dense: embeddings ki cosine similarity

Query aur doc ko vectors mein embed karo ek bi-encoder se (har ek independently encode hota hai), score karo: Kyunki documents offline ek baar embed hote hain, yeh fast hai (approximate nearest neighbour search) — lekin query aur doc kabhi ek doosre ko "dekhte" nahi, isliye subtle interactions kho jaate hain.


3. Do lists merge karna — Reciprocal Rank Fusion (RRF)

BM25 aur cosine ke scores incomparable scales par hain. Hum unhe directly add nahi kar sakte. RRF is problem ko sidestep karta hai sirf har document ki rank use karke har list mein.

kyun? #1 rank wala document contribute karta hai; #100 rank wala contribute karta hai. Toh top positions dominate karti hain, aur jo doc dono lists mein aata hai (chahe middling rank par) woh sirf ek mein aane wale doc se aage hota hai. Constant rank 1 aur rank 2 ke beech ka difference soften karta hai taaki winner-take-all na ho.

Figure — Reranking and hybrid search

4. Cross-encoder se reranking (precision step)

Winning architecture: broadly aur saste mein retrieve karo (bi-encoder + BM25 → top-100), phir precisely aur expensively rerank karo (cross-encoder → top-5). Tum expensive cost sirf 100 items par pay karte ho, poore corpus par nahi.


5. Common mistakes (steel-manned)


6. Feynman

Recall Ek 12-saal ke bachche ko explain karo

Socho ek badi library mein koi fact dhundh rahe ho. Ek helper (keyword search) jo exact words tune bole unhe dhundha hai. Doosra helper (meaning search) unhi books ko dhundhta hai jo usi idea ke baare mein hain, chahe alag words se likha ho. Tum dono se poochte ho taaki kuch achha miss na ho — yahi hybrid search hai. Woh tumhe 100 books ki ek messy pile de dete hain. Phir ek bahut careful reader (the reranker) tumhara question aur har book saath mein actually padhta hai aur best 5 ko upar rakhta hai. Tum sirf woh 5 answer-writer ko dete ho, taaki woh ek badi messy pile se confuse na ho.


7. Flashcards

Pure vector search kya problem solve nahi kar sakta jo hybrid search karta hai?
Exact/rare keyword aur code matches (jaise "ERR_4092") jo embeddings miss karte hain; yeh lexical (BM25) recall ko semantic recall mein add karta hai.
Hybrid search mein do component retrievers kya hain?
Sparse/lexical (BM25 keyword) aur dense/semantic (embedding cosine similarity).
BM25 ka term-frequency term saturate kyun karta hai?
Diminishing returns — ek term ke extra occurrences kam info add karte hain; fraction jab .
BM25 parameters aur kya control karte hain?
= term-frequency kitni jaldi saturate hoti hai; = document-length normalisation ki strength (=none, =full).
RRF formula kya hai?
, typically .
Raw scores add karne ki jagah rank-based RRF kyun use karte hain?
BM25 aur cosine incomparable scales par hain; RRF sirf ranks use karta hai isliye scale-free hai aur dono retrievers se mile docs ko reward karta hai.
Bi-encoder aur cross-encoder mein kya farak hai?
Bi-encoder query aur doc alag embed karta hai (fast, precomputable, coarse); cross-encoder unhe jointly cross-attention se process karta hai (slow, accurate, per-pair).
Cross-encoder poore corpus search karne ke liye kyun use nahi ho sakta?
Isko per document ek forward pass chahiye aur embeddings precompute nahi kar sakta, isliye per query millions of docs score karna infeasible hai — yeh ek small candidate set par reranker hai.
Hybrid search kaun sa metric improve karta hai, aur reranking kaun sa?
Hybrid → recall (net chaura karo); reranking → precision@top-k (best ko pehle order karo).
LLM ko reranking ki jagah bada top-k feed kyun nahi karte?
Lost-in-the-middle: dabe relevant chunks ignore ho jaate hain, plus zyada cost, latency, aur noise se hallucination badhta hai.
Typical two-stage retrieve-then-rerank sizing kya hoti hai?
~100 candidates saste mein retrieve karo (hybrid), cross-encoder se ~5 tak rerank karo LLM ke liye.
Kya reranking candidate set se missing relevant doc recover kar sakta hai?
Nahi — reranking sirf reorder karta hai; recall retrieval time par handle karna padta hai.

8. Connections

  • Vector Databases and ANN Search — dense candidate list supply karta hai.
  • BM25 and TF-IDF — sparse retriever ki scoring foundation.
  • Embeddings and Bi-encoders — dense vectors kaise produce hote hain.
  • Cross-encoders and Sentence Transformers — reranking model.
  • Retrieval-Augmented Generation (RAG) — woh pipeline jisme yeh feed hota hai.
  • Lost in the Middle - LLM context limits — kyun chota top-k matter karta hai.
  • Evaluation - Recall@k and MRR — retrieval quality kaise measure karte hain.

Concept Map

split into

embed into

misses meaning

misses exact terms

merged with

merged with

improves recall

re-scored by

improves precision at top

fed to

solved by

defines score for

Query

Sparse BM25 keyword

Dense vector search

Retrieval failure modes

Hybrid search

Candidate list top-100

Reranking cross-encoder

Top-k passages

LLM answer

BM25 formula TF-IDF length norm