Build and Measure a RAG System
Who it is for
What you will achieve
Prerequisites
Curriculum
- Step 1: Why RAG Exists: The Problem It Solves, Compared With Pasting Everything and Fine-Tuning
A smart model has never read your company's files, and they change every month. Three ways to give it the facts: paste everything, fine-tune, or retrieve only the right pieces. Follow a refund-policy question through all three and see why RAG wins when documents change.
- Step 2: Real Embedding Models: an Open Model or a Provider API, Then Search, Duplicates and Recommendations
Swap a toy embedder for a model that learned. Two roads (all-MiniLM-L6-v2 on your machine, or OpenAI's API with your key), three sizes (384, 1,536 and 3,072 numbers), one rule, and three use cases built on the same rank function, with real scores.
- Step 3: Chunking Strategies for RAG: Fixed Size, Sentences or Headings, Measured on One Handbook
Where you cut decides what search can find. Cut a 13-section employee handbook three ways in plain Python and count the damage: fixed size gives 12 chunks with 12 cut and 11 mixed, sentences give 0 cut but 9 mixed, and headings give 13 clean chunks that know their source.
- Step 4: Vector Databases Hands-On: The Same Search in Chroma and pgvector
Store 12 help articles as vectors in two databases and ask both the same questions. Chroma is a folder on disk with where filters; pgvector is a table in the Postgres you already run. Same model, same answers, to the fourth decimal: 0.4259, 0.4494 and 0.8257.
- Step 5: The RAG Pipeline Explained: 7 Stages From Documents to a Cited Answer
Ingest, chunk, embed, store, retrieve, rerank and generate, in one Python file. Follow one question through a 26-chunk employee handbook to an answer that cites its source.
- Step 6: RAG Prompt Assembly and Citations: Number the Blocks, Fit a Token Budget, Check Every Claim
Hand the retrieved pieces to the model so every answer can be checked. Number each block, fit 471 tokens into a 330-token budget by dropping the weakest blocks, and run a checker that passes a good reply and finds three problems in a bad one: a wrong number, a block that doesn't exist and a sentence with no citation.
- Step 7: Hybrid Search: BM25 Plus Dense Embeddings, Merged With Reciprocal Rank Fusion
One customer types an error code, another describes the problem. BM25 nails the code and misses the meaning; dense search does the opposite. Run both over 12 help articles, see why adding scores fails, and merge the ranks with Reciprocal Rank Fusion so all three test searches come out right.
- Step 8: Reranking With a Cross-Encoder: When the Second Pass Helps, and When It Doesn't
A cross-encoder reads the question and each candidate together and re-orders them. On one question it lifts the right article from fourth to first. On 12 golden questions, first places go from 10 to 9. Here's why, what changes with a weaker first pass, and how many candidates are worth the milliseconds.
- Step 9: Retrieval Evals: Score the Retriever With Recall@K, MRR and nDCG Before Blaming the Model
When a RAG answer is wrong, check the retriever first. Score two retrievers on 24 graded queries with Recall@5, MRR and nDCG@5: overall there's no clear winner (0.757 vs 0.819, 0.854 vs 0.826, 0.769 vs 0.782), but the paraphrase slice drops to about 0.5 for both, and one query scores zero everywhere.