← All projects

Project — Interactive Learning

Retrieval-Augmented Generation

A hands-on walkthrough of how RAG systems work — no ML background needed. Upload any document and watch a real pipeline run step by step, with plain-English explanations at every stage.

What is RAG?
Large language models know a lot — but they don't know your documents.
Retrieval-Augmented Generation bridges that gap:
find the relevant passages first, then ask the model to answer from them.
Instead of retraining the model on your data (expensive, slow, hard to update), RAG keeps your documents in a searchable vector store. At query time, the most relevant chunks are retrieved and pasted into the prompt — giving the model fresh, accurate context on every single request.
RAG Embeddings Vector Store FAISS Python
Phase 1 Ingestion Your document is broken into small pieces, each piece is converted into a number sequence (embedding), and everything is saved into a searchable index. This happens once, offline.
Phase 2 Retrieval When you ask a question, it's converted to the same number format and the index is searched for the most relevant pieces. No LLM involved yet — this is pure search.
Phase 3 Generation The retrieved pieces are handed to an LLM alongside your question. The model reads only those pieces and writes an answer — grounded in your document, not its training data.
Contents
Step 1 — Document Ingestion
Ingestion is the preparation phase — it only runs once per document. The goal is to make your document searchable by meaning, not just by keyword. We do this in three sub-steps: split the text into manageable pieces (chunks), convert each piece into a list of numbers that captures its meaning (embedding), then store all those number lists in a fast searchable index (vector store). After this, the document never needs to be processed again.

Upload any PDF or plain-text file under 500 KB. A short article or a few pages of notes works well.


Step 2 — Retrieval
Retrieval is the search phase — this is what happens the moment you ask a question. Your question is converted into the same number format as the chunks (an embedding), and the vector store finds whichever chunks are numerically closest to it. "Closest" means "most similar in meaning" — not matching exact words, but matching intent. Those top chunks become the context that gets handed to the LLM.
This demo uses semantic search — meaning-based matching via cosine similarity (a way of measuring the angle between two number lists; a smaller angle = more similar meaning).

Production systems often layer in BM25 (classic keyword matching, like a search engine) and combine both into hybrid search to get the best of both worlds. We keep it simple here so you can see the core idea clearly.
Ingest a document above first, then come back here to run a query.

Step 3 — Generation
Generation is where the LLM (Large Language Model) finally enters the picture. An LLM is a model that reads text and writes a response — think of it as a very capable writer that can only work with whatever text you hand it. In RAG, we hand it your question plus the retrieved chunks, forming a prompt — a single block of text the model reads top-to-bottom before writing its answer. Because the chunks come from your document, the answer is grounded in your content, not the model's general training.
Ingest a document above first, then ask a question here to see the full pipeline complete.
The bigger picture

What is RAG?

You've now seen the full pipeline run end-to-end. This section steps back and explains why RAG exists, what problem it actually solves, and why it has become the dominant approach for building AI systems that work with real-world documents.

The Problem RAG Solves

Large language models are trained on massive static datasets. Once trained, they have a fixed knowledge cutoff and know nothing about your private documents, internal databases, or anything published after training ended.

Fine-tuning can help, but it's expensive, slow, and doesn't update well. You'd have to retrain every time your data changes.

How RAG Works

RAG adds a retrieval step before generation. Your documents are split into chunks, converted into vector embeddings, and stored in a vector database. When a query arrives, the most semantically similar chunks are retrieved and injected into the LLM's prompt as context.

The model then generates an answer grounded in those chunks — not in its parametric memory. This makes answers accurate, updatable, and traceable to a source.

Why It Matters

RAG is the dominant architecture for enterprise AI assistants, document Q&A, knowledge bases, customer support bots, and code search tools. It's what makes AI practical when you need accurate, specific answers rather than fluent generalization.

Because answers cite retrieved chunks, you can audit them — something pure LLM generation can't provide.

What We Built Here

This demo runs a real RAG pipeline on AWS: Amazon Titan Embed Text v2 for embeddings, FAISS for in-memory vector search, and Claude Haiku 4.5 via Amazon Bedrock for generation — all connected through a serverless Lambda function.

Every step you saw is production-grade code, not a toy mock. The same patterns power billion-dollar enterprise systems.

Workflow — Beginner RAG (what this demo runs)
The diagram below maps out every step you just ran through, left to right. The left half (dark border) is ingestion — it runs once when you upload your document. The right half is query time — it runs fresh for every question you ask. Follow the arrows to see how a PDF becomes an answer.
Document
PDF / TXT / HTML
Source
Chunking
Fixed-size windows
~500–1000 chars
Indexing
Embedding
Titan Embed v2
1024-dim vectors
Indexing
Vector Store
FAISS IndexFlatIP
cosine similarity
Indexing
User Query
Natural language
question
Query
Retrieval
Top-k chunks
by cosine score
Query
Prompt
System + context
+ question
Query
LLM Answer
Claude Haiku 4.5
grounded response
Query
Workflow — Advanced RAG (production patterns)
Once you understand the basics, there's a lot of room to improve each step. This diagram shows what a production-grade RAG system looks like — the same five phases, but with smarter strategies at each one. You don't need to understand all of this now — use it as a map of what's possible and a reference for where each concept in the "What to explore next" section fits. Amber-bordered nodes are the stages this demo already covers.
Phase 1 — Indexing (offline)
Document Ingestion
PDF, HTML, Markdown, code, structured data
Chunking Strategy
Choose based on document type
Fixed-size Sentence Recursive Semantic Parent-child Late chunking
Metadata Enrichment
Title, date, source, section, page number, doc type
Dense Embedding
Bi-encoder model
e.g. Titan, OpenAI, BGE
Vector Index
FAISS, Pinecone, pgvector, Weaviate
&
Sparse Index
BM25 / TF-IDF keyword index (Elasticsearch, OpenSearch)
Document Store
Full text + metadata retained for retrieval and citation
━━━━━━━━━━
Phase 2 — Query Processing (online)
User Query
Raw natural language input
Query Transformation
Improve recall before retrieval
Query expansion HyDE Step-back prompting Multi-query Query rewriting
Query Embedding
Same bi-encoder as indexing — produces query vector
Phase 3 — Retrieval
Dense Retrieval
ANN search on vector index — semantic similarity
&
Sparse Retrieval
BM25 keyword match — exact term precision
Hybrid Fusion
RRF or weighted merge of both result sets
RRF Score fusion Ensemble
Phase 4 — Re-ranking
Candidate Pool
Top-k from hybrid retrieval (e.g. 20–50 chunks)
Cross-Encoder Re-ranker
Scores query + chunk jointly — far more accurate than bi-encoder but slower
Cohere Rerank BGE-reranker ColBERT LLM-as-judge
Top-k Refined
Smaller, higher-precision set (e.g. 3–5 chunks) sent to LLM
Phase 5 — Generation
Prompt Assembly
System prompt + ranked context + chat history + query
LLM Generation
Streaming response grounded in retrieved context
Claude GPT-4o Gemini Llama 3
Post-processing
Citation injection, faithfulness check, hallucination guard, safety filter
Answer + Sources
Grounded, auditable response with chunk citations
Key stages in this demo
Advanced / production additions
Offline = runs once at ingest time  ·  Online = runs per query
What to explore next