4.4.11 · HinglishAlignment, Prompting & RAG

Retrieval-Augmented Generation (RAG) architecture

1,907 words9 min readRead in English

4.4.11 · AI-ML › Alignment, Prompting & RAG


RAG kyun exist karta hai? (wo problem jise ye solve karta hai)

Ek LLM ki knowledge uski training cutoff pe frozen hoti hai aur billions of weights mein compress hoti hai. Isse teen problems paida hoti hain:

  1. Staleness — training ke baad ki events ke baare mein ye kuch nahi jaanta.
  2. Hallucination — jab unsure hota hai, to fluent-but-wrong facts fabricate kar deta hai.
  3. No provenance — aap cite nahi kar sakte ki jawab kahan se aaya.

RAG kya hai? (definition)


Ye kaise kaam karta hai — poora pipeline

Figure — Retrieval-Augmented Generation (RAG) architecture

Phase A — Indexing (offline, ek baar hota hai)

  1. Documents ko passages mein Chunk karo (jaise ~200–500 tokens).
  2. Har chunk ko ek encoder se Embed karo ek vector mein.
  3. Vectors ko ek vector database (FAISS, Pinecone…) mein Store karo fast nearest-neighbour search ke liye.

Phase B — Retrieval (online, har query pe)

  1. Query ko embed karo: .
  2. Har chunk ko similarity se score karo, usually cosine:
  3. Top- highest-scoring chunks lo.

Phase C — Generation (online)

  1. Ek prompt banao: SYSTEM + [retrieved chunks] + QUESTION.
  2. LLM un chunks pe grounded hokar jawab generate karta hai (aur ideally unhe cite bhi karta hai).

Retrieval objective ko first principles se derive karna


Worked examples


Common mistakes (steel-manned)


The 80/20 — kya cheez actually kaam karti hai

  • Retrieval quality dominate karti hai. Bure chunks pe great LLM < sahi chunks pe average LLM.
  • Teen levers ~80% gains dete hain: good chunking, good embeddings/similarity, aur ==top- + re-ranking==.

Recall Feynman: ek 12-saal ke bachche ko explain karo

Soch ek open-book exam. Saab kuch yaad karne ki koshish karne ki jagah, pehle apni notebook mein sahi page pe flip karo, phir us page ko use karke jawab likho. RAG ek chatbot ke liye wohi karta hai: wo notes ke ek bade dher mein se sahi page dhundta hai (retrieval), usse sawaal ke paas tape karta hai, aur phir chatbot use padhke jawab deta hai. Is tarah wo cheezein banata nahi, aur agar tum dher mein ek naya page add karo, to use turant naya knowledge ho jaata hai — chatbot ko wapas school bhejne ki zaroorat nahi.


Flashcards

#flashcards/ai-ml

What problem does RAG primarily solve in LLMs?
Stale knowledge, hallucination, aur source provenance ka na hona — externally retrieved documents mein answers ground karke.
Write the RAG probabilistic decomposition.
— retriever times generator, latent passages pe summed.
Why use cosine similarity instead of raw dot product for retrieval?
Cosine vector magnitude hatata hai, isliye ranking topical direction pe depend karti hai, document length pe nahi — jab tak vectors pre-normalised na hon (tab dot product equivalent hai).
What are the three phases of a RAG pipeline?
Indexing (chunk+embed+store, offline), Retrieval (embed query, top-k nearest), Generation (LLM retrieved context pe conditioned hokar jawab deta hai).
Why is top-k a valid approximation of the full sum over passages?
Similarities pe softmax low-scoring chunks ke liye negligible bana deta hai, isliye sirf top kuch hi meaningfully contribute karte hain.
Does RAG fully eliminate hallucination?
Nahi — agar retrieval irrelevant/empty chunks return kare to model phir bhi confabulate kar sakta hai; retrieval quality alag se measure karni chahiye.
What is the 'lost-in-the-middle' effect and its RAG implication?
LLMs lambe contexts ke middle mein text ko poorly attend karte hain, isliye blindly k badhana hurt karta hai; k chhota rakho aur re-rank karo.
Why prefer RAG over fine-tuning for factual knowledge?
Facts swappable external data rehte hain; ek document update karne ke liye koi retraining nahi chahiye, unlike fine-tuning se weights mein bake kiye facts.
Softmax retrieval probability formula?
similarity scores pe.
What is the CERG mnemonic?
Chunk → Embed → Retrieve → Generate.

Connections

  • Vector Embeddings — text kaise banta hai; retrieval ki quality ceiling.
  • Cosine Similarity & Nearest Neighbour Search — ranking ka maths (FAISS/ANN).
  • Prompt Engineering — retrieved chunks kaise inject hote hain + grounding instructions.
  • LLM Hallucination — wo failure mode jise RAG mitigate karta hai.
  • Fine-tuning vs In-context Learning — parameters vs data trade-off.
  • Re-ranking & Cross-Encoders — initial retrieval ke baad precision boost.
  • Chunking Strategies — retrieval quality ke liye 80/20 lever.

Concept Map

has problems

causes

causes

causes

solves

keeps facts as

Phase A

Phase B

Phase C

scores by

derived from

top-k chunks feed

produces

Closed-book LLM

Frozen compressed knowledge

Staleness

Hallucination

No provenance

RAG pipeline

External corpus not weights

Chunk embed store vectors

Retriever p z given x

Generator p y given x z

Cosine similarity

Dot product minus magnitude bias

Grounded cited answer