Sub topic 2
Reference-Based Metrics and Their Limits
Assume you know basic Python, can call an LLM through an API, and understand averages and proportions. This chapter uses those skills to compare application outputs with expected results.
Reference-Based Metrics
A reference-based metric compares an output with one or more reference answers.
- Exact match passes only when normalized strings are identical. It works well for fixed answers such as
Paris, but rejects valid alternatives such asThe capital is Paris. - Overlap measures shared words or tokens. Precision asks how much of the output appears in the reference. Recall asks how much of the reference appears in the output. These metrics tolerate small wording changes, but can reward fluent nonsense or penalize correct paraphrases.
def exact_match(output: str, expected: str) -> bool:
return output.strip().lower() == expected.strip().lower()
def token_recall(output: str, expected: str) -> float:
actual = set(output.lower().split())
reference = set(expected.lower().split())
return len(actual & reference) / len(reference) if reference else 0.0
print(exact_match("Paris", " paris ")) # True
print(token_recall("Paris is the capital", "Paris capital of France"))Reference metrics are signals, not complete definitions of quality. Use them when the reference is authoritative and the task has stable wording. Add other checks for open-ended answers.
Deterministic Checks
A deterministic check produces the same result every time for the same output. Check properties such as:
- Valid JSON and required fields.
- A permitted value, such as
urgent,normal, orlow. - Maximum length, prohibited terms, or required citations.
- A numerical answer within an allowed range.
These checks are usually cheaper and easier to audit than semantic scoring. They cannot reliably decide whether a long explanation is correct.
LLM-as-Judge
An LLM judge scores an answer against explicit criteria. Give it the question, relevant context, answer, and a strict output format. Parse its response, and record malformed responses separately. A judge can assess faithfulness or helpfulness, but it may prefer longer answers, imitate the answer's confidence, or agree with its own model family.
Human Labels and Judge Validation
Human evaluation supplies labels for qualities such as correctness, relevance, and safety. Write labeling guidelines with examples, edge cases, and an escalation rule. Have multiple reviewers label a sample, then resolve disagreements before treating labels as ground truth.
Validate the judge on this labeled sample. Report agreement, inspect false positives and false negatives, and test position, verbosity, and demographic biases. If agreement is weak, revise the criterion or use humans for that decision.
Exercise
Why should a malformed LLM-judge response not automatically count as a pass?
Show answer
It provides no valid evidence for the criterion, so it should be recorded as unparseable and reviewed separately.