Scores each case against its recorded response, prints one line per case plus a summary, and exits 0, 1 or 2 (load error). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
44 lines
1.6 KiB
Python
44 lines
1.6 KiB
Python
"""Run an eval set and format the report."""
|
|
|
|
from chemenu.evals.graders import score_case
|
|
from chemenu.evals.model import CaseResult, EvalSet, ResponseError, ResponseSource, SetResult
|
|
|
|
|
|
def run_eval_set(eval_set: EvalSet, responses: ResponseSource) -> SetResult:
|
|
"""Score every case, in case order."""
|
|
cases: list[CaseResult] = []
|
|
for case in eval_set.cases:
|
|
try:
|
|
raw = responses.response_for(case.id)
|
|
except ResponseError as exc:
|
|
cases.append(CaseResult(case.id, 0.0, False, (), f"cannot read response: {exc}"))
|
|
continue
|
|
cases.append(score_case(case, raw, eval_set.output_model, eval_set.threshold))
|
|
return SetResult(eval_set.name, eval_set.threshold, tuple(cases))
|
|
|
|
|
|
def _reason(case: CaseResult) -> str:
|
|
if case.error is not None:
|
|
reason = case.error
|
|
else:
|
|
reason = "; ".join(f"{c.label}: {c.detail}" for c in case.checks if not c.passed)
|
|
# Validation messages span several lines; keep one report line per case.
|
|
return " ".join(reason.split())
|
|
|
|
|
|
def format_report(result: SetResult) -> str:
|
|
"""One line per case, then a summary line."""
|
|
lines: list[str] = []
|
|
for case in result.cases:
|
|
line = f"{'PASS' if case.passed else 'FAIL'} {case.case_id} {case.score:.2f}"
|
|
reason = _reason(case)
|
|
if reason:
|
|
line += f" {reason}"
|
|
lines.append(line)
|
|
passed = sum(case.passed for case in result.cases)
|
|
lines.append(
|
|
f"{result.name}: {passed}/{len(result.cases)} passed, "
|
|
f"mean {result.mean_score:.2f}, threshold {result.threshold}"
|
|
)
|
|
return "\n".join(lines)
|