Add reference grader with set comparison and text normalisation

Compares answer lists against a reference as sets with precision and recall minimums; text is casefolded, NFC and whitespace normalised.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
torbenandClaude Sonnet 5.5 committed 2026-12-04 23:02:45 +01:00
1 parent a124734966
commit 9a7dc6ceb3
5 files changed
+369 -4

No files matched your search

+37
View File
@@ -143,3 +143,40 @@ def test_unquoted_no_stays_string(tmp_path: Path) -> None:
first = eval_set.cases[0].expect[0]
assert isinstance(first, EqualsCheck)
assert first.value is True
_REF_HEAD = (
'input: {}\nexpect:\n - grader: "reference"\n path: "/e"\n'
" min_precision: 0.5\n min_recall: 0.5\n"
)
@pytest.mark.parametrize(
"body",
[
' items: ["a", {n: "b"}]\n',
' items: [{a: "x"}, {b: "y"}]\n',
' items: ["a", " A"]\n',
' items: [{a: "x"}, {a: "X "}]\n',
" items: [{}]\n",
' items: [["a"]]\n',
' items: [{a: ["x"]}]\n',
' where: {kind: "e"}\n items: ["a"]\n',
" items: [.nan]\n",
' where: {n: .inf}\n items: [{a: "x"}]\n',
"min_recall",
"min_precision",
],
)
def test_invalid_reference_raises(tmp_path: Path, body: str) -> None:
if body.startswith("min_"): # minimum missing
text = _REF_HEAD.replace(f" {body}: 0.5\n", "") + ' items: ["a"]\n'
else:
text = _REF_HEAD + body
with pytest.raises(EvalSetError, match=r"bad\.yaml"):
load_eval_set(make_set(tmp_path, cases={"bad": text}))
def test_valid_reference_loads(tmp_path: Path) -> None:
text = _REF_HEAD + ' where: {kind: "e"}\n items: [{a: "x"}, {a: "y"}]\n'
assert load_eval_set(make_set(tmp_path, cases={"ok": text})).cases[0].expect