Evaluate Your LLM App with a Small Test Set
You change the chunk size of the handbook assistant, ask three questions, and the answers look fine. Did the change help, hurt or do nothing? Three questions cannot tell you. In this lesson you evaluate your LLM app with a small test set: 20 questions with an answer key, a script that marks every answer, and saved runs you compare after each change.
Three good answers are not a measurement
An eval, short for evaluation, is a fixed set of test questions marked the same way every time, as How to Evaluate AI Answers explained in AI Fundamentals. Here you build one for the RAG app with citations from lesson 10.
A quick look misleads: a change that fixes one question can quietly break another you did not ask, and a fluent answer feels right either way. A score over the same 20 questions replaces that impression with numbers you can compare.
Write 20 questions in a JSONL file
JSONL, or JSON Lines, is a text file with one complete JSON object per line, and Python reads each line with json.loads. In testset.jsonl, each object holds a question, the facts a good answer must contain and the handbook file they come from:
{"id": "q05", "question": "How many days of paid annual leave do full-time staff get?", "expected_facts": ["18 days|eighteen days"], "expected_source": "03-leave.md", "should_refuse": false}
{"id": "q17", "question": "Who won the 2011 cricket World Cup?", "expected_facts": [], "expected_source": null, "should_refuse": true}
A fact lists the wordings it accepts, separated by |. Sixteen questions are answered by the handbook, at least one from each of its six files. Four are not, such as cricket or wedding leave. For those the right answer is a refusal, so an app that answers everything confidently loses marks.
Write the expected facts from the handbook before you see any answer, or you will be tempted to accept what the model said. Then keep the set fixed: two runs compare only if they ask the same questions.
Score retrieval and the answer separately
run_eval.py asks the app each question, exactly as ask_with_sources.py does, and scores two things. The first is retrieval: a hit means at least one of the top-k chunks comes from the expected file, and the retrieval hit rate is the share of the 16 answerable questions with a hit. A miss means the model never saw the fact.
The second is the answer. A keyword check passes an answer that contains every expected fact, after lowering the case and dropping the [1] markers; numbers must match whole. A question to refuse passes when the app shows no answer. The answer pass rate counts passes out of all 20. scoring.py runs the check on four fixed answers without a model, so its output never varies:
$ python3 scoring.py
1. Full-time staff get 18 days of paid annual leave [1].
keyword check: pass a person: right
2. Overtime is paid at time and a half [2].
keyword check: fail, missing one and a half a person: right, in a wording not listed
3. You can carry 15 days into the next year [1].
keyword check: fail, missing 5, march a person: wrong: 5 days, used by March
4. It is not 18 days of annual leave [1].
keyword check: pass a person: wrong, yet the keywords are there
Answer 2 is right but fails on an unlisted wording, which is why the overtime question, q03, also accepts "time and a half". Answer 4 is wrong but passes: a keyword check reads words, not meaning. In the table, retrieval and the answer get separate columns, so q06 found its file and still failed.
To judge meaning, add --judge. An LLM judge is one more model call that grades each answer against the expected facts with a rubric, a short written rule: pass only if every expected fact is stated, in any wording, and nothing contradicts them. A judge can be wrong too, especially the app's own 3B model, so the script lists where the two checks disagree; read those answers yourself.
Run it after every change, and keep the runs
The folder course-labs/building-llm-apps/11-evals holds the test set, the scoring scripts, and its own copies of the handbook and the lesson 10 app. With Ollama running on port 11434 and both models pulled, run:
cd course-labs/building-llm-apps/11-evals
python3 -m venv .venv && . .venv/bin/activate
pip install -r requirements.txt
python3 index.py data/handbook
python3 run_eval.py --top-k 4
The index step should report 34 chunks, as in lesson 10. The eval then asks all 20 questions, which takes a few minutes on a laptop CPU, and prints a row as each one finishes. Trimmed, with illustrative failures, score and file name:
id expected source retrieved answer failure detail
q06 03-leave.md yes fail ignored context missing: two weeks
q16 06-leaving-the-company.md no fail retrieval miss retrieved: 04-pay-and-expenses.md, 02-working-hours.md, 01-welcome.md
q17 (should refuse) - pass refused before the model: best score 0.452
q20 (should refuse) - fail bad refusal answered: Full-time staff get 18 days of paid annual leave [1].
Retrieval hit rate: 15 of 16 (94%), expected file in the top 4 chunks
Answer pass rate: 17 of 20 (85%), 16 to answer and 4 to refuse
Failures: 1 retrieval miss, 1 ignored context, 1 bad refusal
Saved this run to runs/20261203-150405-k4.json
It worked if you see 20 rows, both rates and the saved file, which holds the settings and every answer. cannot connect to Ollama means the container is stopped, and nothing is saved; index.json not found means you skipped the index step.
Each saved run makes the loop below possible: change one setting, rerun, and read the printed comparison with the newest earlier run, both rates and every question that flipped. Keep a change only if it breaks nothing that used to pass.
On 20 questions one flip moves the pass rate by 5 points, so a one-question gain is a hint, not proof: read that question. To compare chunk sizes, build one index per size, such as python3 index.py data/handbook --size 400 --out index-400.json, then run python3 run_eval.py --index index-400.json. In headings mode, --size only cuts sections longer than the size, so expect small moves on this handbook.
Read both rates: bigger chunks make a file-level hit easier, but give the model more text to search for the fact.
Read the failures, not just the average
An 85% pass rate says something failed, not what to fix, so each failure gets one of three labels, built on the retrieval and generation split of lesson 9:
| Label | What happened | What to try |
|---|---|---|
| retrieval miss | The expected file was not in the top-k | Larger top-k, another chunk size, reworded question |
| ignored context | The file was retrieved, but a fact is missing | Read the answer; sharper rule, fewer chunks, larger model |
| bad refusal | Refused an answerable question, or answered one to refuse | --min-score, the refusal rule |
Then read the failing answers: ask_with_sources.py with the question and --show-prompt prints the chunks and the prompt. Sometimes the test set is wrong, not the app, for example a correct answer in an unlisted wording. Fix it openly, and remember that runs before and after the fix no longer compare.
Check yourself
After a prompt change, the pass rate is still 17 of 20. Did nothing change?
Not necessarily: one question may now fail and another pass. The comparison lists flipped questions; read the newly failing one before you keep the change.
q16 fails as a retrieval miss. Would a stricter system prompt fix it?
No. The courier chunk never reached the model, so no prompt can use it; fix retrieval instead: a larger top-k, another chunk size or other wording.
Why does the test set include four questions the handbook does not answer?
Without them, an app that answers everything scores as well as one that refuses correctly. They measure the refusal, and a confident made-up answer is the failure users rarely catch.