Sub topic 2
Writing requirements you can test
This chapter assumes you have read a product requirement or user story, know roughly what a large language model does, and can read short Python and JSON. A requirement for an AI feature must name a measurable share of good outputs, not one exact output. The model can phrase answers differently each time, so the test asks how often an answer meets the standard.
Writing requirements you can test
A testable requirement has four parts: the inputs it covers, the quality dimension, the measurement method, and a threshold. Add a separate zero-tolerance rule for any critical failure.
REQ-3 Groundedness
For questions in the "refund policy" category, at least 95% of answers must make only claims supported by the retrieved policy text, as judged by the groundedness grader.
No answer may state a refund amount or deadline that contradicts the policy (severity: critical, tolerance: 0 cases).Avoid words like "accurate" or "helpful" without a definition. Replace them with a check someone else could run.
Building an evaluation dataset
An evaluation dataset is a file of test cases. Each case holds an input, a category, a type, simple checks, and the severity of a failure. Include typical cases, edge cases, and adversarial cases such as prompts that try to override policy. Store each case as one line in a JSONL file.
{"id": "refund-014", "category": "refund_policy", "type": "edge_case", "input": "I bought this 45 days ago and it arrived damaged. Can I still return it?", "must_include": ["damaged"], "must_not_include": ["guaranteed refund"], "severity_if_failed": "critical"}Grading outputs: code checks, human review, and model judges
Use the cheapest grader that can answer the question:
- Code checks handle exact facts, such as required phrases or forbidden phrases. They are fast and repeatable.
- Human review handles tone and nuance. It is slow, so sample a subset.
- Model judges score qualities like groundedness. Calibrate them against human labels before trusting them.
def grade_case(case, output):
text = output.lower()
includes_ok = all(term.lower() in text for term in case.get("must_include", []))
excludes_ok = not any(term.lower() in text for term in case.get("must_not_include", []))
return includes_ok and excludes_okTurning grades into metrics
Aggregate grades into a pass rate for each requirement. Report critical failures separately, because one critical failure can block a release even when the overall rate looks good.
def pass_rate(results):
return sum(r["passed"] for r in results) / len(results) if results else 0.0Exercise
REQ-3 requires at least 95% of refund-policy answers to be grounded, and it has zero tolerance for contradictory refund deadlines. Your run has 40 refund cases, 38 pass, and one critical case fails. Does REQ-3 pass?
Show answer
No. 38 of 40 is 95%, which meets the rate, but the critical failure breaks the zero-tolerance rule, so the requirement fails.