Repository navigation
Expand file tree
/
Copy pathrun_eval.py
More file actions
293 lines (255 loc) · 9.61 KB
/
Copy pathrun_eval.py
File metadata and controls
293 lines (255 loc) · 9.61 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python3
"""
Evaluate LLM responses against benchmark answers.
Usage (run from repo root):
python run_eval.py responses/math_hard_gpt-5.2_20260226.jsonl
python run_eval.py responses/all_all_model_ts.jsonl --no-fallback
"""
from __future__ import annotations
import argparse
import json
import os
import sys
from pathlib import Path
from typing import Any, Dict, Tuple
REPO_ROOT = Path(__file__).resolve().parent
DATA_DIR = REPO_ROOT / "src" / "data"
# Import verification pipeline from longcot package.
# If the package isn't installed, register src/ as the longcot package
# so that relative imports within the verification modules work.
try:
from longcot._verifier import verify
from longcot._parsing import extract_solution
from longcot._types import (
ChemistryVerifyOptions,
MathVerifyOptions,
Question,
VerifyOptions,
)
except ImportError:
_src = REPO_ROOT / "src"
if not _src.is_dir():
print("Error: longcot package not found. Run: uv sync", file=sys.stderr)
sys.exit(1)
import types as _types_mod
_pkg = _types_mod.ModuleType("longcot")
_pkg.__path__ = [str(_src)]
sys.modules["longcot"] = _pkg
from longcot._verifier import verify # type: ignore[no-redef]
from longcot._parsing import extract_solution # type: ignore[no-redef]
from longcot._types import ( # type: ignore[no-redef]
ChemistryVerifyOptions,
MathVerifyOptions,
Question,
VerifyOptions,
)
DOMAINS = ("logic", "cs", "chemistry", "chess", "math")
DIFFICULTIES = ("easy", "medium", "hard")
# ---------------------------------------------------------------------------
# Question loading
# ---------------------------------------------------------------------------
QuestionKey = Tuple[str, str, str]
def load_all_questions() -> Dict[QuestionKey, Question]:
"""Load every shipped question into a {(domain, difficulty, question_id): Question} lookup."""
questions: Dict[QuestionKey, Question] = {}
seen_question_ids: dict[str, QuestionKey] = {}
for domain in DOMAINS:
domain_dir = DATA_DIR / domain
if not domain_dir.is_dir():
continue
for diff in DIFFICULTIES:
path = domain_dir / f"{diff}.json"
if not path.exists():
continue
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
for q in data.get("questions", []):
qid = str(q["question_id"])
key = (domain, diff, qid)
if qid in seen_question_ids:
prev = seen_question_ids[qid]
raise ValueError(
f"Duplicate question_id {qid!r} found in {prev[0]}/{prev[1]} and {domain}/{diff}"
)
seen_question_ids[qid] = key
questions[key] = Question(
question_id=qid,
domain=domain,
difficulty=diff,
prompt=q["prompt"],
problem=q.get("problem"),
answer=q.get("answer"),
)
return questions
# ---------------------------------------------------------------------------
# Evaluation
# ---------------------------------------------------------------------------
def evaluate(
responses_path: str,
*,
verify_options: VerifyOptions | None = None,
) -> Dict[str, Any]:
"""Evaluate a responses JSONL file and return a results dict."""
questions = load_all_questions()
responses: list[Dict[str, Any]] = []
with open(responses_path, "r", encoding="utf-8") as f:
for line in f:
if line.strip():
responses.append(json.loads(line))
correct = 0
incorrect = 0
failed = 0
wrong_formatting = 0
details: list[Dict[str, Any]] = []
for resp in responses:
qid = str(resp.get("question_id", "unknown"))
domain = resp.get("domain")
difficulty = resp.get("difficulty")
successful = resp.get("successful", False)
if not isinstance(domain, str) or not isinstance(difficulty, str):
raise ValueError(
"Each response row must include string fields 'domain' and 'difficulty'."
)
# Category 1: API / submission failure
if not successful:
failed += 1
details.append(
{
"question_id": qid,
"domain": domain,
"difficulty": difficulty,
"status": "failed",
}
)
continue
response_text = resp.get("response_text", "")
# Look up the question for verification
question = questions.get((domain, difficulty, qid))
if question is None:
print(
f" Warning: question_id {qid!r} not found in {domain}/{difficulty}, counting as failed"
)
failed += 1
details.append(
{
"question_id": qid,
"domain": domain,
"difficulty": difficulty,
"status": "failed",
"reason": "missing_question",
}
)
continue
# Formatting check: missing `solution =` is counted, but verification still runs.
has_wrong_formatting = extract_solution(response_text) is None
if has_wrong_formatting:
wrong_formatting += 1
# Verify correctness
try:
is_correct = verify(question, response_text, options=verify_options)
except Exception as e:
incorrect += 1
detail = {
"question_id": qid,
"domain": domain,
"difficulty": difficulty,
"status": "incorrect",
"error": str(e)[:200],
}
if has_wrong_formatting:
detail["wrong_formatting"] = True
details.append(detail)
continue
if is_correct:
correct += 1
detail = {
"question_id": qid,
"domain": domain,
"difficulty": difficulty,
"status": "correct",
}
if has_wrong_formatting:
detail["wrong_formatting"] = True
details.append(detail)
else:
incorrect += 1
detail = {
"question_id": qid,
"domain": domain,
"difficulty": difficulty,
"status": "incorrect",
}
if has_wrong_formatting:
detail["wrong_formatting"] = True
details.append(detail)
total = len(responses)
verified = correct + incorrect
return {
"total": total,
"correct": correct,
"incorrect": incorrect,
"failed": failed,
"wrong_formatting": wrong_formatting,
"accuracy": correct / verified if verified > 0 else 0.0,
"overall_accuracy": correct / total if total > 0 else 0.0,
"details": details,
}
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser(
description="Evaluate LLM responses against benchmark answers",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""\
Examples:
python run_eval.py responses/math_hard_gpt-5.2_20260226.jsonl
python run_eval.py responses/all_all_model_ts.jsonl --no-fallback
""",
)
parser.add_argument("responses", help="Path to responses JSONL file")
parser.add_argument("--output", help="Output JSON path (default: results/<name>.json)")
parser.add_argument(
"--no-fallback", action="store_true",
help="Disable math/chemistry fallback judges (avoids needing GEMINI_API_KEY)",
)
args = parser.parse_args()
verify_options = None
if args.no_fallback:
verify_options = VerifyOptions(
math=MathVerifyOptions(enable_fallback=False),
chemistry=ChemistryVerifyOptions(enable_fallback=False),
)
# --- Warn about math fallback ---
if not args.no_fallback:
has_key = bool(os.environ.get("GEMINI_API_KEY") or os.environ.get("GOOGLE_API_KEY"))
if not has_key:
print(
"Warning: GEMINI_API_KEY / GOOGLE_API_KEY not set. "
"Math/chemistry fallback judges are unavailable without an API key.\n"
" Set the env var, or pass --no-fallback to silence this warning.\n"
)
# --- Evaluate ---
print(f"Evaluating: {args.responses}")
results = evaluate(args.responses, verify_options=verify_options)
# --- Output path ---
if args.output:
output_path = args.output
else:
name = Path(args.responses).stem
output_path = f"results/{name}.json"
out = Path(output_path)
out.parent.mkdir(parents=True, exist_ok=True)
with open(out, "w", encoding="utf-8") as f:
json.dump(results, f, indent=2)
# --- Summary ---
print(f" Total: {results['total']}")
print(f" Correct: {results['correct']}")
print(f" Incorrect: {results['incorrect']}")
print(f" Failed: {results['failed']}")
print(f" Wrong formatting: {results['wrong_formatting']}")
print(f" Accuracy: {results['accuracy']:.1%}")
print(f" Overall accuracy: {results['overall_accuracy']:.1%}")
print(f" Saved to: {output_path}")
if __name__ == "__main__":
main()