🧩 RAG কী?
Retrieval Augmented Generation — আপনার private data (document, database) থেকে relevant তথ্য retrieve করে LLM-কে answer দিতে সাহায্য করে। LLM-এর knowledge + আপনার data।
🎯 কেন RAG?
- LLM আপনার company data জানে না
- Fine-tuning দামি ও জটিল
- Knowledge update সহজ (document reindex)
- Source citation দেয়া যায়
- Hallucination কমে
- Private data leak না (on-premise সম্ভব)
🏗️ RAG Architecture
┌──────────────────────────────────┐ │ Offline Pipeline │ │ Documents → Chunk → Embed → DB │ └──────────────────────────────────┘ ┌──────────────────────────────────┐ │ Online Query │ │ User question │ │ ↓ embed │ │ Vector search → Top-K chunks │ │ ↓ │ │ Prompt: context + question │ │ ↓ │ │ LLM → Answer with citation │ └──────────────────────────────────┘
📚 Embedding কী?
Text → high-dimensional vector (e.g., 1536 dims)। Semantically similar text-এর vector কাছাকাছি।
"dog" → [0.2, -0.5, 0.8, ..., 0.1] "puppy" → [0.3, -0.4, 0.7, ..., 0.2] // কাছে "car" → [-0.6, 0.3, 0.1, ..., 0.9] // দূরে
🛠️ Embedding Model
| Model | বৈশিষ্ট্য |
|---|---|
| OpenAI text-embedding-3-small | Cheap, good |
| OpenAI text-embedding-3-large | Best quality |
| Cohere embed-v3 | Multilingual |
| Voyage AI | Code, retrieval-tuned |
| BGE, E5 (open source) | Self-host free |
| Sentence-transformers | Classic, free |
💾 Vector Database
- Pinecone — managed, fast
- Weaviate — open source
- Qdrant — open, Rust-based
- Chroma — embedded, simple
- Milvus — large scale
- pgvector — PostgreSQL extension ⭐
- Redis — vector search
- Elasticsearch — hybrid search
✂️ Chunking Strategy ⭐
Document ছোট chunk-এ ভাঙা। Chunk size balance:
- Too small — context হারায়
- Too big — retrieval precision কমে
- Sweet spot: 300-800 token
Technique
- Fixed size — simplest (e.g., 500 token)
- Sentence-based — sentence boundary-তে break
- Paragraph — natural break
- Semantic — topic change detect
- Recursive — heading-based (markdown)
- Sliding window — overlap রাখুন (50-100 token)
🔎 Retrieval Strategy
- Top-K — K সবচেয়ে similar chunk (K=3-10)
- MMR — diverse results (redundancy কমায়)
- Hybrid search — semantic + keyword (BM25)
- Reranking — Cohere Rerank / cross-encoder
- Metadata filter — date, source, category
📝 RAG Prompt
"Answer the user's question based ONLY on the context below.
If the context doesn't contain the answer, say:
'I don't have this information in the provided documents.'
Always cite source: [Doc Title, page X].
Don't make up information.
Context:
---
{retrieved_chunks}
---
Question: {user_question}
Answer:"
🧪 RAG Evaluation Metric
- Retrieval: precision@K, recall@K, MRR
- Generation: faithfulness (grounded?), answer relevance
- End-to-end: user satisfaction, task completion
- Tool: RAGAS, TruLens, Arize Phoenix
🚀 Advanced RAG ⭐
HyDE (Hypothetical Doc Embedding)
Question-এর hypothetical answer generate → embed → search।
Query Rewriting
User question → LLM refine → better search query।
Multi-query
Same question-এর ৩-৫ variation generate, সব retrieve।
Parent-Child
Child chunk search, parent paragraph retrieve (more context)।
Agentic RAG
LLM সিদ্ধান্ত নেয় কবে, কোথায় search করবে (tool use)।
Graph RAG
Knowledge graph + vector — relationship-aware retrieval।
🛠️ Framework
- LangChain — Python/JS, popular
- LlamaIndex — RAG-focused
- Haystack — production-ready
- Semantic Kernel (Microsoft)
- OpenAI Assistants API — managed RAG
- NotebookLM — no-code
💼 Use Case
- Company wiki Q&A bot
- Customer support (product docs)
- Legal document assistant
- Research paper search
- Medical guideline lookup
- Code documentation chat
- HR policy bot
⚠️ RAG Pitfall
- Poor chunking → irrelevant retrieval
- Old/stale index
- Irrelevant context আসলে prompt confuse
- No reranking → top-K-এ noise
- Citation hallucinate (verify)
- Cost scale (embedding + storage)
📊 Cost Example
10K document, avg 5 pages: - Embedding one-time: ~$5 - Storage: ~$70/month (Pinecone) - Query: $0.001 per (K=5) - LLM answer: $0.01 per Total: ~$100/month for 10K query