Courses / GenAI & LLM / Module 6
Module 6: Intro to RAG (continues in RAG Systems)
6 h · theory 2 · hands-on 2 · project 1 · revision 1
You will be able to
- Explain the retrieve-then-generate pattern and its failure modes
- Build a minimal RAG answer with citations
- Tell a retrieval failure from a generation failure
- Know what the full RAG course adds: parsing, hybrid search, re-ranking, evaluation, access control
Capstone increment: A /ask endpoint that answers questions about extracted documents with citations and a 'not found' path.
Job-description skills: RAG · Lab tools: sentence-transformers, qdrant-client
1. Retrieve, then generate
Retrieval-augmented generation answers a question in two steps: find the passages most likely to contain the answer, then ask the model to answer using only those passages and to cite them. It lets a model answer about your private, current documents without retraining, and it makes answers checkable.
Offline you parse documents, split them into chunks, and index them. Online, every question is retrieved against that index, the top chunks go into the prompt, and the answer comes back with citations or an honest “not found”.
2. A minimal RAG answer with citations
Number the retrieved chunks in the prompt ([1], [2], …) with their source and page, and instruct the model to cite the numbers it used and to answer NOT FOUND when the sources don't contain the answer. Then check the output: every cited number must exist, and an answer without citations is suspect.
Add a “not found” path before the model is even called: if the best retrieval score is below a threshold, say so. A model handed weak context will often answer anyway from its training data, which is exactly the hallucination RAG was meant to prevent.
3. Two kinds of failure
When a RAG answer is wrong, first check whether the right chunk was retrieved. If it wasn't, it's a retrieval failure: chunking, the search method or k is the problem, and no prompt change will help. If it was, it's a generation failure: the model ignored, misread or over-extended the evidence.
Measure them separately. Retrieval is scored with recall@k and MRR against a golden set of questions with known relevant chunks; generation is scored for faithfulness (is every claim supported by the sources?) and answer correctness. Use the playground below to see how search method and k change recall.
| Rank (faded = not retrieved) | Relevant? | Document | BM25 → dense → blended score |
|---|---|---|---|
| 1 | Travel reimbursement Claim travel expenses with form HR-17 within 30 days. Attach receipts for flights, trains and hotels. | BM25 1.00 · dense 0.00 · 0.50 | |
| 2 | Salary and payslips Salary is credited on the last working day. Download payslips and Form 16 from the payroll portal. | BM25 0.44 · dense 0.00 · 0.22 | |
| 3 | Annual leave policy Employees get 24 days of annual leave. Apply for leave in the HR portal at least two weeks ahead. | BM25 0.00 · dense 0.00 · 0.00 | |
| 4 | Holiday calendar Public holidays for 2027 by office location. Optional holidays need manager approval. | BM25 0.00 · dense 0.00 · 0.00 | |
| 5 | Laptop and equipment Each employee receives a laptop. Report a lost or stolen device to IT security within 24 hours. | BM25 0.00 · dense 0.00 · 0.00 | |
| 6 | Password and MFA rules Use a password manager and turn on MFA. Never share one-time codes, even with IT. | BM25 0.00 · dense 0.00 · 0.00 | |
| 7 | Work from home Hybrid staff may work remotely up to three days a week. Home internet is reimbursed up to Rs 1,000 a month. | BM25 0.00 · dense 0.00 · 0.00 | |
| 8 | Parental leave Birth parents get 26 weeks of paid leave; other parents get 4 weeks. Tell HR 8 weeks before the leave starts. | BM25 0.00 · dense 0.00 · 0.00 |
Guided experiments
- “where is form HR-17”: slide to dense only. The travel document drops out and recall falls to 0. Why does BM25 find it instantly?
- “vacation days per year”: slide to BM25 only. It can only match the word “days”, so the holiday calendar is missed. Which word in the question does no document contain?
- “time off when we have a baby”: BM25 only retrieves nothing at all. Slide to dense only: both leave policies come back. What did the dense side “know” that BM25 didn’t?
- “my computer was stolen” at the middle setting: try k = 1, then k = 2. Recall@k and nDCG change while MRR stays at 1. What does each metric reward?
- Find one blend where every question gets recall@3 = 1. That’s why production systems use hybrid search.
- Type your own question and tick the relevant documents yourself. That is how a golden set is built.
4. What the RAG Systems course adds
This module gives you a working baseline and the vocabulary. The RAG Systems course turns it into a production system: layout-aware parsing of PDFs and tables, chunking strategies, hybrid search and re-ranking, query rewriting, evaluation with golden sets and RAGAS, access control so users only retrieve what they may see, freshness, and cost.
If your capstone needs more than a question-answering endpoint over a few hundred documents, take that course next.
Hands-on: coding window
The coding window and the full VS Code lab are available after you sign in.
Sign in to codeLabs
Lab 6.1: RAG over 50 documents
Chunk 50 policy documents (about 500 tokens per chunk with overlap), embed them into Qdrant (local mode), and answer 20 questions with a local model, citing chunk numbers.
Expected: Answers with citations; a printed retrieval list for each question; at least one honest NOT FOUND.
Lab 6.2: /ask endpoint (capstone increment)
Add POST /ask that answers questions about the extracted documents in the capstone, with citations to document and page, a score threshold for not-found, and a check that every citation exists.
Expected: Tests for an answerable question, an unanswerable one (returns not found), and an answer with an invalid citation (rejected).
Lab 6.3: Diagnose 10 bad answers
Collect 10 wrong answers from Lab 6.1. For each, decide retrieval or generation failure using the retrieved chunks, and propose one fix.
Expected: A table: question, failure type, evidence, fix. Most teams find retrieval failures dominate.
Go deeper
Videos
- What is Retrieval-Augmented Generation (RAG)? · IBM Technology · ~6 min. A short, clear explanation of why RAG exists, to share with non-engineers.
- Learn RAG From Scratch – Python AI Tutorial from a LangChain Engineer · freeCodeCamp.org · ~153 min. Builds indexing, retrieval and generation step by step, then covers query rewriting and routing.
Further reading
- Paper Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks · Lewis et al., NeurIPS 2020. The paper that named RAG: a retriever and a generator trained to work together.
- Article Contextual retrieval · Anthropic. Prepending chunk-specific context before embedding and BM25, with measured drops in retrieval failures.
- Paper Retrieval-Augmented Generation for Large Language Models: A Survey · Gao et al., 2023. A map of naive, advanced and modular RAG and the techniques in each stage.
GitHub repositories
- langchain-ai/rag-from-scratch: Notebooks 1-4 (indexing, retrieval, generation) alongside the video above.
- qdrant/qdrant-client: Local mode (QdrantClient(':memory:') or path=…) for development without running a server.
- vibrantlabsai/ragas: The faithfulness and context-recall metrics: what they measure and how.
Checkpoint quiz
Interview questions
- Walk me through a RAG pipeline end to end, offline and online.
- A user reports a wrong answer from your RAG bot. How do you debug it?
- How do you stop a RAG system from answering questions its documents don’t cover?
- How would you build a golden set for a new RAG project?
- When is RAG the wrong solution, and what would you use instead?
Ask the professor
Sign in to ask questions about this module.
Discussion
Community discussion appears here once giscus is configured (see README).