File size: 2,663 Bytes
fa0c0b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Build the exact judge input for one brief from the released files. No other construction is valid.

    from judge_input import load_bundle, user_message
    bundle = load_bundle("path/to/ma-bench")          # reads data/cases.jsonl, data/rubric_items.jsonl, scoring/judge_prompt.md
    system, user = user_message(bundle, "C3", brief_text)

The judge sees, per case, the rubric as one JSON object {"items": [...]} whose items carry exactly these ten keys in this
naming: id, step, question, pass_condition, fail_condition, source_fact, artifact_anchor, source, tier, severity
(pretty-printed, keys sorted). The case header fields come from data/cases.jsonl. This is byte-identical to the input
used when the judge was validated against the expert panel.
"""

from __future__ import annotations

import json
import re
from pathlib import Path

RUBRIC_KEYS = {  # released column -> key the judge sees
  "item_id": "id",
  "step": "step",
  "question": "question",
  "pass_condition": "pass_condition",
  "fail_condition": "fail_condition",
  "source_fact": "source_fact",
  "artifact_anchor": "artifact_anchor",
  "source_documents": "source",
  "tier": "tier",
  "severity": "severity",
}


def load_bundle(root: str | Path) -> dict:
  root = Path(root)
  cases = {c["case_id"]: c for c in map(json.loads, open(root / "data" / "cases.jsonl", encoding="utf-8"))}
  items = [json.loads(line) for line in open(root / "data" / "rubric_items.jsonl", encoding="utf-8")]
  md = (root / "scoring" / "judge_prompt.md").read_text(encoding="utf-8")
  blocks = re.findall(r"```\n(.*?)\n```", md, re.S)
  task_md = (root / "scoring" / "task_prompt.md").read_text(encoding="utf-8")
  task_blocks = re.findall(r"```\n(.*?)\n```", task_md, re.S)
  return {"cases": cases, "items": items, "system": blocks[0], "user_template": blocks[1], "task_template": task_blocks[0]}


def rubric_json(bundle: dict, case_id: str) -> str:
  rows = [{RUBRIC_KEYS[k]: i[k] for k in RUBRIC_KEYS} for i in bundle["items"] if i["case_id"] == case_id]
  return json.dumps({"items": rows}, indent=2, sort_keys=True)


def task_text(bundle: dict, case_id: str) -> str:
  return bundle["task_template"].replace("{op}", bundle["cases"][case_id]["operation"])


def user_message(bundle: dict, case_id: str, brief_text: str) -> tuple[str, str]:
  c = bundle["cases"][case_id]
  user = bundle["user_template"].format(
    case_id=c["case_id"],
    planning_level=c["planning_level"],
    document_type=c["document_type"],
    difficulty=c["difficulty"],
    task=task_text(bundle, case_id),
    rubric_json=rubric_json(bundle, case_id),
    final_content=brief_text,
  )
  return bundle["system"], user