Spaces:
Running

File size: 3,620 Bytes
effcad2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""Export Beam's 71 ForesightBench eval rollouts (plus headline numbers) for the static Space.



    python scripts/export_rollouts.py --trajectories ../data/trajectories \

        --eval ../benchmark-release/eval_set.jsonl --out site/data/

"""
from __future__ import annotations

import argparse
import glob
import json
from pathlib import Path
from statistics import mean

from foresightbench_env.data import load_split
from foresightbench_env.scoring import score_completion

ROOT = Path(__file__).resolve().parents[1]
PATTERN = "*_reflection_beam-1.0-501b-a23b_{sid}.json"
PAPER_BEAM_ERROR = 0.292
PAPER_COPY_BASELINE_ERROR = 0.327


def main() -> None:
    ap = argparse.ArgumentParser()
    ap.add_argument("--trajectories", type=Path, required=True)
    ap.add_argument("--eval", type=Path, required=True)
    ap.add_argument("--out", type=Path, default=ROOT / "site" / "data")
    a = ap.parse_args()

    eval_rows = [json.loads(l) for l in a.eval.open(encoding="utf-8") if l.strip()]
    rl_eval_ids = {r["sample_id"] for r in load_split("eval")}
    n_train = len(load_split("train"))

    rollouts = []
    for row in eval_rows:
        files = sorted(glob.glob(str(a.trajectories / PATTERN.format(sid=row["sample_id"]))))
        if not files:
            raise SystemExit(f"missing Beam trajectory for {row['sample_id']}")
        traj = json.loads(Path(files[-1]).read_text(encoding="utf-8"))
        msg = traj["response"]["choices"][0]["message"]
        content = msg.get("content") or ""
        s = score_completion(content, row)
        rollouts.append({
            "sample_id": row["sample_id"],
            "arxiv_id": row["arxiv_id"],
            "title": row["title"],
            "area": row["meta"]["area"],
            "component": row["meta"].get("component"),
            "prompt": row["prompt"],
            "reasoning": msg.get("reasoning") or msg.get("reasoning_content") or "",
            "content": content,
            "asked": row["asked"],
            "predictions": s["values"],
            "truth": [t["method_value"] for t in row["target"]],
            "baseline": [t["baseline_value"] for t in row["target"]],
            "slot_errors": [round(e, 4) for e in s["slot_errors"]],
            "score": round(s["numeric_reward"], 4),
            "parse_ok": s["parse_ok"],
            "in_rl_eval": row["sample_id"] in rl_eval_ids,
            "usage": traj["response"].get("usage", {}),
            "trace_file": Path(files[-1]).name,
        })

    beam_err = mean(1 - r["score"] for r in rollouts)
    if abs(beam_err - PAPER_BEAM_ERROR) > 0.005:
        raise SystemExit(f"recomputed Beam error {beam_err:.4f} is not within 0.005 of {PAPER_BEAM_ERROR}")
    headline = {
        "beam_eval_error": round(beam_err, 4),
        "copy_baseline_error": PAPER_COPY_BASELINE_ERROR,
        "n_eval": len(rollouts),
        "n_rl_train": n_train,
        "n_rl_eval": len(rl_eval_ids),
        "n_papers": len({r["arxiv_id"] for r in rollouts}),
        "beam_direction_right_pct": None,
        "by_area": {
            area: round(mean(1 - r["score"] for r in rollouts if r["area"] == area), 3)
            for area in sorted({r["area"] for r in rollouts})
        },
    }
    a.out.mkdir(parents=True, exist_ok=True)
    (a.out / "rollouts.json").write_text(json.dumps(rollouts, ensure_ascii=False), encoding="utf-8")
    (a.out / "headline.json").write_text(json.dumps(headline, indent=1), encoding="utf-8")
    print(json.dumps(headline, indent=1))


if __name__ == "__main__":
    main()