Lesson 161 — Advanced

Document Q&A Platform

Yeh project L4 (RAG) ko ek deployable product mein badalta hai: user PDF poochta hai aur jawab sources ke saath aata hai. Is lesson mein architecture se lekar retrieval eval tak ka poora build plan hai.

⏱ 30 min• Advanced• L0–L13 tak ke modules

Project kya hai

WHAT

Ek service jahan user document upload kare aur uske andar se sawaal pooche. Andar: chunking, embeddings, vector search, phir LLM jawab source reference ke saath. Focus product hai, notebook nahi.

WHEN

Jahan documents bahut hain aur dhoondhna mehngega — contracts, product manuals, HR policy, research papers, compliance reports. Jab "kahan likha hai" ka sawaal roz aaye.

WHERE

FastAPI backend + ek simple web UI. GitHub par public repo, kahin bhi deploy (Vercel/Render/AWS) with a live demo URL jo interview mein khul sake.

Engineering ka asli kaam yahan scope control hai: pehle ek document type (jaise PDF manuals), ek language, ek deployment — phir format badhao. Har extra format (scanned pages, tables, mixed languages) apna naya failure mode laata hai.

Scope rule: v1 = text PDFs + English/Hinglish questions + top-5 retrieval + citations. Scans, tables aur multi-tenant auth v2 ke liye bachao.

Architecture — pipeline ke 7 stages

Pipeline ko alag-alag stages mein todo — har stage ka apna input/output aur apna test hota hai:

Golden rule: Stage ka order badlo mat — pehle eval set banao, warna har improvement tumhe bas "lagta behtar hai" hogi.

FastAPI skeleton

from fastapi import FastAPI, UploadFile
from pydantic import BaseModel

app = FastAPI()

class AskRequest(BaseModel):
    question: str
    top_k: int = 5

@app.post("/ingest")
async def ingest(file: UploadFile):
    doc = parse_pdf(await file.read())          # stage 1-2: parse + chunk
    store.upsert(embed_all(doc.chunks))         # stage 3-4: embed + store
    return {"doc_id": doc.id, "chunks": len(doc.chunks)}

@app.post("/ask")
async def ask(req: AskRequest):
    hits = store.search(embed_text(req.question), k=req.top_k)  # stage 5
    answer, cites = answer_with_citations(req.question, hits)   # stage 6
    return {"answer": answer, "sources": [h.cite() for h in hits]}
Blocking dhyan rakho: PDF parse aur embedding calls blocking hain — threadpool/asyncIO mein daalo, warna ek badi file poori API rok degi. Uploaded files ka temp cleanup bhoolo mat.

Retrieval eval — pehla deliverable

LLM answers har baar alag hote hain, isliye pehle retrieval evaluate karo (deterministic hai): kya relevant chunk upar aaya? Phir answer quality ka spot-check karo.

# Chhota sa golden set — har improvement ko isi se maano
eval_set = [
    {"q": "Refund kitne din mein milta hai?", "relevant_chunks": ["policy-7", "policy-8"]},
    {"q": "Escalation contact kaun hai?", "relevant_chunks": ["contact-2"]},
]

def hit_at_1(store, item):
    top = store.search(item["q"], k=1)
    return int(top and top[0].chunk_id in item["relevant_chunks"])

score = sum(hit_at_1(store, it) for it in eval_set) / len(eval_set)
print(f"hit@1 = {score:.2f}")   # har chunk-size, k aur hybrid toggle par yahi doharao
Judge combo: LLM-as-judge akele andha hota hai — programmatic checks (string/ID match) + judge (nuance) + kabhi kabhi human review, teenon ka mix rakho.

Ship checklist

Numbers honestly: README mein sirf woh numbers daalo jo tumhare eval set se nikle hain. "99% accuracy" bina eval ke — interview mein sabse pehla dhoka pakda jaayega.

Exercise

Question: Sawaal ke relevant passages chunne wala pipeline step — ek shabd?

Question: RAG ka full form? (space se, English)

Question: Top-k results mein se koi bhi relevant chunk mila toh use kya count karte hain? (ek shabd, English)

Common mistakes

Desi example: Librarian vs Document QA

Librarian seedha jawab nahi deta — pehle index banata hai, phir shelf dhoondhta hai, passage dikhata hai. Tumhara RAG pipeline wahi librarian hai, bas index vector mein hota hai:

text
sawaal -> index search -> passage uthao -> line-by-line padho
        -> jawab + page number   (bina index, andheri mein dhoondhna)
Interview angle — yahan se sawaal aate hain
  • Q: Ek document QA system design karo — pipeline ke stages batao. (ingest → chunk → embed → store → retrieve → answer → eval)
  • Q: Chunking strategy kaise choose karoge? (document type par: legal = smaller + overlap, manuals = section-wise; eval se verify)
  • Q: User ko jawab ke saath sources kaise dikhaoge? (chunk ids prompt mein, model se cited span wapas, UI mein highlight)
  • Q: System ka cost kaise kam karoge? (embed cache, hybrid se k chhota, small model for simple queries, prompt caching — L9.5)
Lesson complete?

Architecture, skeleton aur eval — teenon ka plan ready hai. Agla step: apne paas ke real PDFs se golden set banao, phir har improvement wahi measure karo.

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