Sub topic 6
Evaluation: Golden Sets, Programmatic Checks, and Model-Graded Scoring
Assume you know the classic machine learning lifecycle, can call a Python or HTTP-based model API, and understand basic production logging. This chapter shows how to test applications whose outputs are open-ended text.
Why ordinary tests are not enough
A traditional function often has a small set of correct outputs. An LLM application may produce several valid answers for the same input. Small changes in wording, model state, or sampling settings can change the response.
Testing only for exact string equality is therefore too strict. Testing only whether the request succeeded is too weak. A useful evaluation checks whether the output satisfies the task.
An evaluation set is a collection of representative inputs and checks. A golden set is a stable, reviewed evaluation set that represents important behavior. It should include normal cases, difficult cases, and known failure cases.
Each case commonly contains:
- A stable identifier.
- The user input.
- Expected properties or a reference answer.
- One or more checks.
- Optional metadata, such as risk level or product area.
| Check type | What it measures | Typical use |
|---|---|---|
| Programmatic | Rules that code can verify | JSON validity, required terms, length |
| Reference-based | Similarity to an approved answer | Summaries or classifications |
| Model-graded | Judgment about meaning or quality | Helpfulness, groundedness, tone |
Build checks around behavior
Programmatic checks are fast, repeatable, and inexpensive. Use them whenever the requirement can be stated as a rule. For example, a support-ticket summary might need to mention the issue and stay below 60 words.
The following harness runs each case multiple times. The pass rate captures nondeterminism instead of hiding it behind one result.
def run_eval(generate, cases, runs=3):
"""generate(text) -> str."""
report = []
for case in cases:
passes = 0
for _ in range(runs):
output = generate(case["input"])
passed = all(check(output) for check in case["checks"])
passes += int(passed)
report.append({
"id": case["id"],
"pass_rate": passes / runs,
})
return report
def gate(report, minimum=0.9):
failing = [
item["id"]
for item in report
if item["pass_rate"] < minimum
]
return len(failing) == 0, failing
def demo_generate(text):
return f"Summary: {text}"
cases = [{
"id": "duplicate_charge",
"input": "My invoice shows a duplicate charge.",
"checks": [
lambda output: "charge" in output.lower(),
lambda output: len(output.split()) <= 60,
],
}]
report = run_eval(demo_generate, cases, runs=3)
allowed, failing = gate(report)
print(report)
print("release allowed:", allowed)
print("failing cases:", failing)A high pass rate does not prove that the application is good. A weak check can pass while the answer is misleading. Review the checks themselves and inspect failed outputs.
Add model-graded checks carefully
A model grader can compare an output with a rubric. A rubric should name the dimensions and provide clear scoring rules, such as:
- Factuality: does the answer match the supplied information?
- Completeness: does it cover the required points?
- Helpfulness: does it answer the user’s question?
- Safety: does it avoid unsupported or dangerous advice?
Use a separate, stable grader configuration when possible. Store the grader model, rubric version, and score with the evaluation result. Model grading is useful for meaning, but it can be inconsistent or favor fluent answers over correct ones. Sample borderline results for human review.
Exercise
Why should an evaluation run each case more than once when the application uses a nondeterministic model?
Show answer
Repeated runs estimate the case’s pass rate. They reveal flaky behavior that a single pass or fail would hide.