Courses / RAG Systems / Module 8
Module 8: Retrieval and answer evaluation: golden sets, judge calibration, evals in CI
12 h · theory 4 · hands-on 5 · project 2 · revision 1
You will be able to
- Separate retrieval metrics from answer metrics
- Measure faithfulness and answer relevance
- Calibrate an LLM judge against human labels
- Block regressions in CI
Capstone increment: Eval suite (Ragas + ranx + pytest) on every pull request, with scores posted to the PR.
Job-description skills: RAG evaluation · Lab tools: ragas, ranx, pytest
1. Two systems, two sets of metrics
A RAG answer can fail in four places: the fact isn’t in the index, it is there but wasn’t retrieved, it was retrieved but the model ignored it, or the model added claims the context doesn’t support. Retrieval metrics tell you about the first two; answer metrics tell you about the last two. One blended “quality score” tells you nothing about which to fix.
Build the layers in order: retrieval metrics on a golden set first (cheap and deterministic), then answer metrics scored by a calibrated judge, then end-to-end checks on questions the documents can’t answer.
2. A golden set that grows
Each golden-set row holds a question, the chunks or documents that answer it, a reference answer and tags: question type, language, source document family, difficulty. Fifty well-chosen questions beat five hundred generated ones nobody checked. Generate candidates with an LLM if you like, but have a person verify each.
Grow it from production: every failure a user reports and every “thumbs down” you confirm becomes a new row. Version the set with the corpus version it was built against; when documents change, some answers do too.
3. Answer metrics: faithfulness, relevance, correctness
Faithfulness asks whether every claim in the answer is supported by the retrieved context; Ragas splits the answer into claims and asks a judge model about each. Answer relevance asks whether the answer addresses the question. Answer correctness compares against the reference answer. Citation precision (Module 9) asks whether each cited chunk supports the sentence that cites it.
These need an LLM judge, and judges have biases: they prefer longer answers, the first option shown, and text in their own style. Treat their scores as measurements that need calibration, not as ground truth.
Deep dive
Deep dive: scoring faithfulness claim by claim
Faithfulness asks whether every claim in the answer is supported by the retrieved context, not whether the answer is true in the world. RAGAS-style faithfulness splits the answer into atomic claims with an LLM, asks a judge whether each claim can be inferred from the context, and reports supported claims ÷ total claims.
Context recall goes the other way: split the reference answer into claims and check how many of them the retrieved context supports. Low faithfulness with high context recall means the generator added things; low context recall means retrieval never found the facts, and no prompt change will fix that.
Worked example: “How long do I have to return a laptop?”
context: “Laptops can be returned within 14 days of delivery. Opened software is not refundable.” answer: “You have 14 days from delivery. Refunds reach your card in 5–7 days.” claim 1 the return window is 14 days supported claim 2 the 14 days count from delivery supported claim 3 refunds reach the card in 5–7 days not in the context faithfulness = 2 / 3 = 0.67 (claim 3 may be true, but the system can’t show it)
4. Calibrate the judge against people
Label 50 to 100 answers yourself as pass or fail with written criteria. Run the judge on the same answers and measure agreement with Cohen’s kappa, which corrects for agreement by chance. Specific binary criteria (“every number in the answer appears in the context”) agree with people far better than a vague 1-to-10 scale.
Use the tool below to see how a vague rubric and a specific rubric compare against human labels, and why “it passes everything” can look accurate while being useless.
| Human: pass | Human: fail | |
|---|---|---|
| Judge: pass | 8 | 6 bad answers passed |
| Judge: fail | 3 good answers failed | 3 |
Agreement 55% · Cohen’s κ 0.06 · catches 33% of the failures (κ corrects for agreement by chance: below 0.4 is weak, above 0.7 is good)
- #1 Correct refund steps, cites policy · human pass · judge 9/10 → pass
- #2 Confident, but invents a 60-day window · human fail · judge 8/10 → pass ✗
- #3 Correct, a little long · human pass · judge 7/10 → pass
- #4 Answers a different question · human fail · judge 7/10 → pass ✗
- #5 Correct, terse · human pass · judge 6/10 → fail ✗
- #6 Correct with the right form · human pass · judge 9/10 → pass
- #7 Polite, fluent, wrong amount · human fail · judge 9/10 → pass ✗
- #8 Correct, asks a useful follow-up · human pass · judge 8/10 → pass
- #9 Refuses a valid request · human fail · judge 5/10 → fail
- #10 Correct, says when it can’t know · human pass · judge 7/10 → pass
- #11 Leaks another customer’s order id · human fail · judge 8/10 → pass ✗
- #12 Correct · human pass · judge 9/10 → pass
- #13 Correct but clumsy wording · human pass · judge 5/10 → fail ✗
- #14 Missing the deadline the user asked for · human fail · judge 6/10 → fail
- #15 Correct, cites two sources · human pass · judge 8/10 → pass
- #16 Right answer, wrong currency · human fail · judge 9/10 → pass ✗
- #17 Correct, informal tone · human pass · judge 6/10 → fail ✗
- #18 Empty answer after a tool error · human fail · judge 4/10 → fail
- #19 Correct · human pass · judge 8/10 → pass
- #20 Hallucinated tracking link · human fail · judge 7/10 → pass ✗
Guided experiments
- With rubric v1, try every pass mark from 5 to 9. What is the best κ you can get? Which kinds of bad answer does v1 keep passing?
- Switch to rubric v2. Find the pass mark with the best κ. What changed in the rubric that made the judge useful?
- Agreement can look fine while κ is low. Set v1 with pass mark 1 (everything passes). What is the agreement, and why is κ zero?
- You have budget to label 50 more answers by hand. Which answers would you pick to label, and why?
5. Evals in CI
Run retrieval metrics on every pull request: they are deterministic, take seconds with ranx and cost nothing. Run the LLM-judged answer metrics on a fixed sample on every pull request with cached judgements for unchanged answers, and on the full set nightly. Post the scores and the difference from main as a PR comment, and block the merge if a metric drops below its threshold.
Small golden sets are noisy: a 60-question set can swing several points from random changes. Use a confidence interval on the difference (bootstrap is simple) so the gate fails on real regressions, not noise.
Hands-on: coding window
The coding window and the full VS Code lab are available after you sign in.
Sign in to codeLabs
Lab 8.1: Grow the golden set
Extend the capstone golden set to 60 questions with tags for type, language and document family, including 10 unanswerable questions. Have a second person check 20 of them.
Expected: golden/v2.jsonl with tags and a short note on disagreements found.
Lab 8.2: Calibrate a judge
Score 40 capstone answers for faithfulness with Ragas, label the same 40 yourself, and compute Cohen’s kappa. Rewrite the judge criteria once and re-measure.
Expected: Kappa before and after the rewrite, with the criteria you settled on.
Lab 8.3: Evals in CI (capstone increment)
Add a CI job that runs retrieval metrics with ranx on every PR, runs Ragas on a 20-question sample with cached judgements, posts scores versus main as a comment and fails below thresholds.
Expected: A PR that lowers recall shows a failing check with the numbers in a comment.
Go deeper
Videos
- Systematically improving RAG applications · Hamel Husain · ~69 min. Jason Liu’s talk on systematic RAG improvement: segment your failures and fix the biggest bucket first.
- AI Eval Concepts Explained in 15 Minutes · Hamel Husain · ~14 min. The core eval concepts in fifteen minutes, from someone who runs evals for many teams.
- Key Metrics and Evaluation Methods for RAG · What's AI by Louis-François Bouchard · ~11 min. A survey of RAG metrics and evaluation methods, useful as a checklist.
Further reading
- Paper Ragas: Automated Evaluation of Retrieval Augmented Generation · Es et al., 2023. How faithfulness, answer relevance and context relevance are computed, claim by claim.
- Paper ARES: An Automated Evaluation Framework for Retrieval-Augmented Generation Systems · Saad-Falcon et al., 2023. Trained judges plus a small human-labelled set to put confidence intervals on RAG scores.
- Paper FActScore: Fine-grained Atomic Evaluation of Factual Precision · Min et al., 2023. Splitting answers into atomic facts and checking each: the idea behind claim-level faithfulness.
- Article Your AI Product Needs Evals · Hamel Husain. A practitioner's case for building evals first, with the levels of testing that work in production.
- Article Systematically Improving Your RAG · Jason Liu. Synthetic questions, retrieval metrics first, and clustering failures by topic.
GitHub repositories
- vibrantlabsai/ragas: The faithfulness and context precision metric implementations; read how claims are extracted and judged.
- AmenRa/ranx: Run and Qrels objects, compare() with statistical tests: the retrieval half of your CI gate.
- confident-ai/deepeval: pytest-style LLM test cases and its RAG metrics, an alternative to Ragas in CI.
- promptfoo/promptfoo: Declarative eval configs and assertions, with a GitHub Action that comments on PRs.
Checkpoint quiz
Interview questions
- How do you evaluate a RAG system before you have users? After launch?
- Explain faithfulness. How is it computed, and how can it mislead you?
- How do you know your LLM judge is trustworthy?
- Design an eval gate for pull requests that is fast, cheap and not flaky.
- Your offline metrics improved but user satisfaction dropped. What could explain it?
Ask the professor
Sign in to ask questions about this module.
Discussion
Community discussion appears here once giscus is configured (see README).