Lesson 61 — Intermediate
RAG with Frameworks
Ab tumne RAG khud likha hai — ab framework ka code padhne mein maza aayega. Is lesson mein hum LlamaIndex/LangChain style abstractions (indexes, retrievers, query engines, rerankers), rerank aur query transforms framework se, aur framework vs hand-built ka farq dekhenge.
Framework kya deta hai
Kya hai?
RAG frameworks (LlamaIndex, LangChain) ingestion, indexing, retrieval, reranking aur generation ke ready components dete hain — connectors, splitters, retrievers aur query engines pre-wired.
Kab kaam aata hai?
Production timeline tight ho, bahut se data sources/connectors chahiye, ya team ke liye ek standard pipeline chahiye. Aur tab jab tumhe underlying mechanics samajh ho (jo ab hai).
Kahan milta hai?
Framework ke high-level classes mein: document readers, index objects, retriever interfaces, query engines, aur source nodes (citations).
Framework ka asli faayda connectors aur integration hai — naya data source (Notion, DB, web) jodna ek class change. Logic (fusion, thresholds) tumhara apna — framework sirf plumbing deta hai.
Hand-built pipeline ka framework rebuild
# LlamaIndex style (naam representative; API framework versions ke saath badalti hai)
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
docs = SimpleDirectoryReader("data").load_data() # M4.1 ka loader
index = VectorStoreIndex.from_documents(docs) # chunk + embed + store
engine = index.as_query_engine(similarity_top_k=5) # retrieve + prompt + LLM
print(engine.query("Refund policy kaise hoti hai?"))
# LangChain style: retriever + chain
# retriever = store.as_retriever(search_kwargs={"k": 5})
# chain = prompt | retriever | llm
# answer = chain.invoke("Refund policy kaise hoti hai?")
- Loader → splitter → index: tumhara extract-chunk-embed, framework ke ingestion ke andar.
- Query engine: retrieve + assemble + generate ka ek call — tumhara rag(query).
- Source nodes: answer ke saath retrieved chunks wapas aate hain — tumhare hits jaise.
- Import paths framework versions ke saath badalte rehte hain — docs dekh ke likho, rat ke nahi.
Indexes, retrievers, rerankers, transforms
# Framework components = tumhare hand-built steps
qengine = index.as_query_engine(
similarity_top_k=20, # wide recall (M4.5)
node_postprocessors=[rerank(top_k=4)], # M4.10 ka rerank stage
)
# query transforms bhi components hain:
# query_transform = multi_query(3) # M4.11 ka rewriting
# fusion = rrf_fusion(...) # M4.6 ka fusion
# sab ek hi pipeline mein plug-in
- Retriever interface: dense, sparse, hybrid — sab ek interface ke peeche; switch karna aasaan.
- Node postprocessors: rerank, metadata filter, dedupe — retrieve ke baad wale steps yahan hote hain.
- Query transforms: rewrite, multi-query, HyDE — retrieve se pehle wala step.
- Citation: source nodes se answer ke claims map hote hain — tumhara cited_ids wala parser framework ke andar bhi chahiye.
Framework vs hand-built
- Speed: framework se pehla working demo ghanton mein; hand-built mein din (par seekhna bhi milta hai).
- Control: fusion logic, custom scoring, thresholds — hand-built mein sab tumhare haath mein; framework ke abstraction ke andar kabhi kabhi force karna padta hai.
- Debugging: framework ke andar kya hua kabhi kabhi leak nahi hota; hand-built mein har step print kar sakte ho.
- Complexity: framework ka dependency tree, version upgrades, breaking changes — ek aur maintenance surface.
- Rule: learning/one-off → hand-built pehle; production timeline + connectors → framework, apne logic ke saath.
Exercise
Question: Documents ko embed karke store karne wala framework object kya kehlata hai? (index / reranker / loader)
Question: Retrieve + prompt + LLM ko ek call mein karne wala framework component kya kehlata hai?
Question: Retrieve ke baad wale steps (rerank, filter) framework mein kya kehlata hain?
Question: Retrieved chunks answer ke saath wapas jo aate hain unhe framework mein kya kehte hain?
Common mistakes
- Framework ke peeche mechanics bhool jaana: Black box pe bharosa — jab kuch bigde toh debug hi nahi hoga. Fix: hand-built pipeline tha hi isliye; framework ke nodes ko print karke map karo.
- Rat ke API likhna: Import paths versions ke saath badalte hain — purana example chalega hi nahi. Fix: concept yaad rakho (retriever, postprocessor), exact API docs se lo.
- Apna logic framework mein force karna: Custom fusion framework ke abstraction se takra raha hai — hours jaate hain. Fix: apna logic framework ke bahar rakho, framework se sirf components lo.
Tumne cycle pe sikhna tha (hand-built) — gear, brake, sab pata hai. Ab automatic car (framework) chalao: pedal dabao, chal gaya. Par clutch-gear ka pata ho toh traffic mein bhi confuse nahi hote — aur car ruk jaaye toh cycle pe wapas aa sakte ho:
cycle (hand-built): retrieve + assemble + rag sab tumhara
car (framework) : loader + index + query engine ready
tumhara edge: clutch-gear samajh = mechanics pata hai
failover: car kharab? -> cycle pipeline abhi bhi chalti hai- Q: Framework ke main abstractions? — Loaders, splitters, indexes, retrievers, node postprocessors, query engines, source nodes.
- Q: Rerank framework mein kaise lagta hai? — Node postprocessor ke roop mein, retrieve ke baad, top_k config ke saath.
- Q: Framework vs hand-built ka trade-off? — Speed/connectors vs control/debuggability; tumhara logic framework-independent rakho.
- Q: Framework se debugging kaise? — Source nodes aur logged retriever outputs se; abstraction ke peeche wahi tumhara pipeline hai.
Frameworks clear — ab framework ka code samajh aata hai aur tumhara hand-built pipeline uska fallback bhi hai. Aakhri module: capstone — advanced RAG application deploy karna.