Skip to content

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

This module is an introduction. RAG Systems covers it in full.

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”.

Offline: indexing (runs on a schedule)DocumentsPDF, HTML, ticketsParselayout, tables, OCRChunk+ page refs, metadataIndexBM25 + vectorsOnline: every questionQuestion“How do I claim…”Rewritestandalone queryRetrievetop k by scoreRe-rankcross-encoderGenerateanswer from contextCite / not foundverify groundingMeasure each stage separately: retrieval with recall@k / nDCG, answers with faithfulness
Most bad answers start upstream: a parsing, chunking or retrieval miss that generation can’t fix.

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.

Common misconception: “RAG stops hallucinations.” It reduces them when retrieval works. When retrieval misses, the model may fill the gap confidently. Citations and a not-found path are what make misses visible.

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.

Keyword vs meaning: retrieval playgroundPick a question or type your own, mark which documents are relevant, then slide between BM25 and dense retrieval.
recall@3 1.00MRR 1.00nDCG@3 1.001 relevant document marked
Rank (faded = not retrieved)Relevant?DocumentBM25 → dense → blended score
1Travel 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
2Salary 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
3Annual 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
4Holiday calendar
Public holidays for 2027 by office location. Optional holidays need manager approval.
BM25 0.00 · dense 0.00 · 0.00
5Laptop 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
6Password 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
7Work 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
8Parental 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
  1. “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?
  2. “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?
  3. “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?
  4. “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?
  5. Find one blend where every question gets recall@3 = 1. That’s why production systems use hybrid search.
  6. 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.

Tip: Build the golden set before tuning anything: 30-50 real questions with the chunks that answer them. Every later improvement is measured against it.

Hands-on: coding window

The coding window and the full VS Code lab are available after you sign in.

Sign in to code

Labs

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

Further reading

GitHub repositories

Checkpoint quiz

1. The right chunk wasn’t in the top 5 retrieved. Which fix can’t solve this?
2. Why add a retrieval-score threshold before calling the model?
3. Faithfulness measures…
4. recall@5 = 0.6 on your golden set means…
5. What makes a RAG answer checkable by a user?

Interview questions

  1. Walk me through a RAG pipeline end to end, offline and online.
  2. A user reports a wrong answer from your RAG bot. How do you debug it?
  3. How do you stop a RAG system from answering questions its documents don’t cover?
  4. How would you build a golden set for a new RAG project?
  5. 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).