Retrieval-Augmented Generation
Combining dense vector retrieval with LLM generation to answer questions using ground truth enterprise knowledge.
What is Retrieval-Augmented Generation (RAG)?
Standalone Large Language Models rely on Parametric Memory: facts stored inside their static parameter weights during pretraining.
Parametric memory has major flaws:
- Knowledge Cutoff: Cannot answer questions about events after pretraining.
- Private Data Blindness: Cannot access internal company documents or private databases.
- Hallucinations: Generates plausible sounding but completely false facts when uncertain.
Retrieval-Augmented Generation (RAG - Lewis et al., 2020) solves this by connecting the LLM to Non-Parametric External Memory (a Vector Database).
USER QUERY ──► [ EMBEDDING MODEL ] ──► Vector Query q
│
▼
[ VECTOR DATABASE ] ◄── Search Top-K Relevant Document Chunks!
│
▼
CONSTRUCT PROMPT: "Answer question based ONLY on context: <Chunks> Question: <User Query>"
│
▼
[ LLM GENERATOR ] ──► Grounded Answer with Source Citations!
Production RAG Architecture (2 Phases)
┌─────────────────────────────────────────────────────────────┐
│ PHASE 1: OFFLINE DATA INGESTION PIPELINE │
│ Raw PDFs / Docs ──► Text Chunking ──► Embedding Model │
│ ──► Store Embeddings & Text Metadata in Vector Database! │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ PHASE 2: ONLINE RETRIEVAL & GENERATION PIPELINE │
│ User Query ──► Query Embedding ──► ANN Vector Search │
│ ──► Top-K Chunks ──► Reranker ──► Prompt Context ──► LLM │
└─────────────────────────────────────────────────────────────┘
Phase 1: Data Ingestion (Offline)
- Document Parsing: Extract text from raw PDFs, Markdown files, or SQL tables.
- Text Chunking: Split long documents into small overlapping passages (e.g. 512 tokens with 50-token overlap).
- Vector Embedding: Pass text chunks through an Embedding Model (e.g.
text-embedding-3-smallorbge-large-en). - Vector Storage: Store embedding vectors alongside raw text metadata in a Vector DB (Qdrant, Pinecone, Milvus).
Phase 2: Online Query & Generation (Real-Time)
- Query Embedding: Convert user query into a vector $\mathbf{q}$.
- ANN Vector Search: Execute Approximate Nearest Neighbor search to retrieve top $K$ relevant chunks (e.g. $K=5$).
- Prompt Construction: Format system prompt containing retrieved context passages.
- LLM Generation: LLM reads context passages and synthesizes a grounded answer with source citations.
Key Benefits of RAG
- Zero Hallucination Grounding: Forces the model to cite specific text chunks.
- Real-Time Data Freshness: Updating enterprise data requires simply updating the Vector DB without retraining LLM weights.
- Data Security & Privacy: Access control filters can be applied at the database level so users only retrieve documents they have permissions to see.
Say this out loud
RAG connects LLMs to external knowledge bases. It ingests documents by chunking, embedding, and indexing them in a vector database offline. During queries, RAG retrieves top-K relevant text passages using vector similarity and inserts them into prompt context, enabling grounded, hallucination-free generation with source citations.
Followups to expect
- What is Naive RAG vs Advanced RAG vs Modular RAG? Naive RAG uses simple vector retrieval. Advanced RAG adds pre-retrieval query rewriting and post-retrieval reranking. Modular RAG incorporates dynamic graph search, routing, and agent loops.
- What is RAG Triad Evaluation (Ragas)? Evaluating RAG quality across three metrics: Context Relevance (did retrieval find right chunks?), Groundedness (is LLM answer supported by context?), and Answer Relevance (does answer address user query?).
Check yourself
What primary limitation of standalone Large Language Models does Retrieval Augmented Generation (RAG) solve?