Lesson 62 — Intermediate

Capstone — Deploy an Advanced RAG Application

Aakhri capstone: tumhara hybrid search engine ab ek full RAG service banega — fusion + rerank + grounded generation + citations + abstain + eval + API. Yehi wo project hai jo resume pe aur interview mein tumhara flagship banega — L4 ke saare layers ek saath.

⏱ 10 min• Intermediate• RAG with Frameworks

Kya banayenge

Kya hai?

Ek deployable RAG service: FastAPI endpoints, advanced pipeline (query rewrite → fusion → rerank → grounded gen → cite/abstain), eval harness jo har change pe chalta hai, aur structured logs.

Kab kaam aata hai?

L4 ke end pe — jab sab layers ek saath ho aur ab integration + evaluation ki zarurat ho. Yahi project M4.7 ke hybrid engine ko production-shaped banata hai.

Kahan milta hai?

Ek repo: pipeline/ module, app.py (API), eval/ set, README. Deploy kisi bhi container-friendly host pe — config se model names, hardcoded nahi.

Scope: ek corpus, ek endpoint, ek eval set. Lekin quality guarantees real hon: har answer traceable (citations), out-of-scope pe abstain, aur har regression automatically pakri jaaye. Yehi "advanced" ka matlab hai — features nahi, guarantees.

Pehla deliverable eval hai: Pipeline se pehle eval set likho (gold queries + abstain queries + latency budget). Bina iske koi bhi "improvement" prove hi nahi kar paoge — isi liye ye pehle aata hai.

Step 1 — Service skeleton

from fastapi import FastAPI
app = FastAPI()

@app.post("/ask")
def ask(q: str, k: int = 5):
    out = pipeline.answer(q, k=k)     # saara logic pipeline module mein
    return {"answer": out.text, "citations": out.citations,
            "abstained": out.abstained, "latency_ms": out.ms}

@app.get("/health")
def health():
    return {"index_size": index.count(), "model": MODEL_NAME}
Observability pehle din se: RAG mein quality silently drop hoti hai (index stale, model badal gaya). Har request ka log = tumhara flight recorder. Deploy se pehle log schema fix karo, baad mein add karna mushkil hai.

Step 2 — Advanced pipeline

def answer(query, k=5):
    variants = rewrite(query)                       # M4.11
    ranked = rrf([dense_rank(v, 20) for v in variants]
                 + [bm25_rank(v, 20) for v in variants])  # M4.6
    top = rerank(query, ranked, top=k)              # M4.10
    if not top or top[0]["score"] < MIN_SCORE:
        return Out(ABSTAIN, [], True)               # M4.12
    text, cits = generate(query, top)               # M4.9 + citations
    return Out(text, cits, False)
Flags matter: Config-driven layer control se tumhe ek cheez milti hai: same eval set pe rerank on vs off, fusion on vs off — real numbers. Yehi production RAG engineering hai, fancy names nahi.

Step 3 — Eval, regression aur ship

EVAL = [
    ("refund policy damaged item", {"must_contain": "7 din", "gold": "doc-12#0"}),
    ("INV-4821 status",                     {"gold": "doc-7#2"}),
    ("kya kal cricket score batao",         {"abstain": True}),   # out of scope
]

def run_eval():
    hits = abst_ok = ans_ok = 0
    for q, exp in EVAL:
        out = pipeline.answer(q)
        if "gold" in exp:
            hits += exp["gold"] in [c["id"] for c in out.citations]
        if exp.get("abstain"):
            abst_ok += out.abstained
        if "must_contain" in exp:
            ans_ok += exp["must_contain"] in out.text
    print("retrieval", hits, "| abstain", abst_ok, "| answer", ans_ok)
Done kaise define karein: Eval set green (retrieval + abstain + answer sab pass), latency budget andar, citations clickable, logs aa rahe hain — tabhi ship. Iske baad har improvement isi harness se prove hoga, na ki andaza se.

Exercise

Question: Advanced pipeline mein pehle kya hota hai — generation ya retrieval?

Question: Out-of-scope sawaal pe pipeline kya return karta hai? (ek word English)

Question: Deployment se pehle har change ko kis cheez pe test karte hain?

Question: Query aur document ko saath mein padhne wala reranker kis tarah ka model hai?

Common mistakes

Desi example: Hotel front desk ka upgrade

Pehle ek receptionist sab karta tha (baseline RAG). Ab team hai: do guards (dense + sparse), supervisor (rerank), register with page numbers (citations), aur "nahi pata" bolna allowed (abstain). Sab ka naam-pata logged — manager ko pata hai kya ho raha hai:

text
guest (query)
  -> 2 guards poochhte hain (fusion: dense + sparse)
  -> supervisor filter (rerank top-k)
  -> register se page number (citations)
  -> "nahi pata" bhi ek jawab (abstain)
everything logged -> manager ka eval set = kal ka quality check
Interview angle — yahan se sawaal aate hain
  • Q: Advanced RAG pipeline ke layers? — Rewrite → fusion (dense+sparse) → rerank → threshold → grounded generate → cite/abstain.
  • Q: RAG service ka eval kaise karte ho? — Gold queries (retrieval+answer), out-of-scope (abstain), latency budget — teeno checkpoints.
  • Q: Production mein RAG quality kaise maintain? — Versioned eval set (regression), structured logs, config-driven thresholds, index update job.
  • Q: Citations kaise implement karoge? — Numbered context, [n] contract, parse + chunk id map, format check + support check.
Lesson complete?

L4 complete — tumhara advanced RAG service ready hai: hybrid retrieval, rerank, grounded generation, citations, abstain aur eval sab ek saath. Aage L5 mein agents ki duniya — wahan RAG tumhara tool banega, ek chapter nahi.

Popular courses: Data Science Course in Lucknow · AI Course in Lucknow · Python Course in Lucknow · Power BI Course · Data Analytics Course