#69 LLM engineering and evals 6.1 min
Detect LLM Hallucinations: 5 Checks Scored on 16 Answers
One check caught 1 of 8 made up answers. A strict model judge flagged 6 of 8 good ones.
Fool the figures check
Copy the code folder first. The checks need Ollama with all-minilm running, because checks.py embeds the handbook when it is imported. In a Python prompt inside the folder run: import checks. Then call checks.figures_ok on 'Bikes can be returned within 90 days if unused.' and on 'Bikes can be returned within 14 days if unused.' Predict both results first. The 90 days answer should give False and the 14 days answer should give True, even though 14 days is the rule for helmets and not for bikes. Explain in one sentence why the check passes it.
A hint
Look at what figures_ok compares: the set of figures in the answer against the set of figures anywhere in handbook.md.
Move the word overlap limit
In the same Python prompt, run: from answers import ANSWERS. Then count how many of the made-up answers (the ones whose second value is False) have checks.overlap(answer) below 0.8, and how many of the good answers do. Compare with the video, which found 3 caught and 2 good ones flagged at 0.8. Now repeat with 0.6 and with 0.95. At 0.6 you should find 2 caught and none flagged, and at 0.95 you should find 3 caught and 4 flagged. Say which limit you would pick and why picking it from these same sixteen answers is a weakness.
A hint
Use a loop with an if on follows and on checks.overlap(answer) < limit. The video warns against picking a limit from the same answers you test on.
Write an answer that slips through
Write one new made-up answer about the handbook that you think figures_ok and overlap_ok will both pass, even though the handbook does not say it. Predict the results, then test it. For example, 'Points can be swapped for cash after 12 months.' gives True from figures_ok and an overlap of 1.00, although the handbook says points cannot be swapped for cash. Try two more of your own: one that changes a rule without changing any figures, and one that adds a new figure.
A hint
The overlap check counts only words longer than 3 letters. A sentence built from handbook words in a new order can still score 1.00.
Predict the judge on your answers
This task uses llama3.2 and takes a minute or two, so first write down a prediction. Pick three answers from your own tests above. Predict whether judge_ok (the strict prompt) and judge_loose_ok will return True for each. Then run them in Python with checks.judge_ok(answer) and checks.judge_loose_ok(answer) and compare. Keep a note of any answer where the strict judge said False for a good answer, as the video found for 6 of 8 good ones.
A hint
A reply only counts as a pass if it starts with YES. Temperature is 0, so rerunning the same answer should give the same reply.
These tasks are for you to try on your own machine. Nothing is checked or saved.
What this does not show
Sixteen one-sentence answers that I wrote about one made-up handbook, one run, and one small local model (llama3.2) as the judge with two prompts; the 0.8 and 0.75 limits are my choice, and no claim splitting or larger judge was tested.
Up next in LLM engineering and evals
More in LLM engineering and evals
- #05 Structured Outputs in Python: Valid JSON Is Not Correct JSON
- #06 Test Your LLM App with pytest: An Eval Harness in 40 Lines
- #17 Cut LLM Cost and Latency: Caching and Shorter Answers
- #56 Prompt Versioning: Compare Changes Case by Case
- #57 LLM as Judge: I Swapped the Order and It Changed
- #58 JSON Mode vs Schema vs Function Calling: Measured (Ollama)
Related videos
- #57 LLM as Judge: I Swapped the Order and It Changed LLM engineering and evals
- #52 RAG Citations: Source Removed, Exact Quote, Wrong Source RAG and vector search
- #63 Few-Shot Examples: How Many Is Enough? A Measured Test LLM engineering and evals
- #64 Chain of Thought: When Step by Step Helps, and Its Cost LLM engineering and evals
