#!/usr/bin/env python3 """Score model replies against the frozen eval sets in eval/. Input is a predictions JSONL with one line per eval item: {"id": "...", "reply": "..."}. Ids are unique across eval files, so one predictions file can cover all of them. Items without a reply count as failures and are reported separately. English held-out perplexity is not scored here: it needs the model, so use kubertrainer's test-model.py with eval/english_heldout.jsonl as the test file. """ import argparse import json import re from pathlib import Path NAME_PATTERN = re.compile(r"\b[a-z]+-\d{5}-\d+\b") def first_json(reply): """Return the first JSON object or array in the reply, or None if there is none.""" text = re.sub(r"^```(?:json)?\s*|\s*```$", "", reply.strip()) decoder = json.JSONDecoder() for index, char in enumerate(text): if char in "{[": try: value, _ = decoder.raw_decode(text[index:]) return value except ValueError: continue return None def score_tool(item, reply): call = first_json(reply) result = {"parsed": isinstance(call, dict)} if not result["parsed"]: return {**result, "exact": False, "name": False, "false_tool": False} expected = item["expected"] name = call.get("name") if expected["name"] is None: result.update(exact=name is None, name=name is None, false_tool=name is not None) else: result.update(exact=name == expected["name"] and call.get("arguments") == expected["arguments"], name=name == expected["name"], false_tool=False) return result def score_manifest(item, reply): found = set(NAME_PATTERN.findall(reply)) return {"exact": found == set(item["expected"])} def score_concept(item, reply): lowered = reply.lower() hits = [term for term in item["key_terms"] if term.lower() in lowered] return {"recall": len(hits) / len(item["key_terms"]), "all_terms": len(hits) == len(item["key_terms"])} def load(path): return [json.loads(line) for line in Path(path).read_text(encoding="utf-8").splitlines() if line.strip()] def mean(values): return round(sum(values) / len(values), 3) if values else None def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--predictions", required=True, type=Path) parser.add_argument("--eval-dir", type=Path, default=Path("eval")) parser.add_argument("--report", type=Path, help="Optional JSON file for the full report") args = parser.parse_args() replies = {row["id"]: row["reply"] for row in load(args.predictions)} report, missing = {}, [] scored = [] # (item, result) pairs, so missing replies cannot shift the alignment for item in load(args.eval_dir / "tool_calls.jsonl"): if item["id"] not in replies: missing.append(item["id"]) continue scored.append((item, score_tool(item, replies[item["id"]]))) results = [result for _, result in scored] with_call = [result for item, result in scored if item["expected"]["name"]] without_call = [result for item, result in scored if not item["expected"]["name"]] report["tool_calls"] = { "items": len(results), "json_parse_rate": mean([r["parsed"] for r in results]), "exact_match": mean([r["exact"] for r in results]), "tool_name_accuracy": mean([r["name"] for r in with_call]), "false_tool_rate": mean([r["false_tool"] for r in without_call]), } manifests = load(args.eval_dir / "manifest_reasoning.jsonl") manifest_results = [] for item in manifests: if item["id"] not in replies: missing.append(item["id"]) continue manifest_results.append(score_manifest(item, replies[item["id"]])) report["manifest_reasoning"] = {"items": len(manifest_results), "exact_match": mean([r["exact"] for r in manifest_results])} concepts = load(args.eval_dir / "concepts.jsonl") concept_results = [] for item in concepts: if item["id"] not in replies: missing.append(item["id"]) continue concept_results.append(score_concept(item, replies[item["id"]])) report["concepts"] = {"items": len(concept_results), "key_term_recall": mean([r["recall"] for r in concept_results]), "all_terms_present": mean([r["all_terms"] for r in concept_results])} report["missing_replies"] = len(missing) print(json.dumps(report, indent=2)) if args.report: args.report.write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8") if __name__ == "__main__": main()