forked from ikiruneo/millionaire-bench
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmillionaire-run.py
More file actions
530 lines (453 loc) · 20.3 KB
/
Copy pathmillionaire-run.py
File metadata and controls
530 lines (453 loc) · 20.3 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
import os
import asyncio
from opperai import Opper
from typing import Literal, Dict, List, Any
from pydantic import BaseModel
import json
import datetime
from pathlib import Path
opper = Opper(http_bearer=os.getenv("OPPER_API_KEY"))
# Select models available:
models = [
# "anthropic/claude-3.5-haiku",
# "anthropic/claude-sonnet-4",
# "berget/gpt-oss-120b",
"gcp/gemini-2.5-pro",
# "gcp/gemini-2.5-flash",
# "gcp/gemini-2.5-flash-lite",
# "groq/gemma2-9b-it",
# "groq/gpt-oss-20b",
# "groq/llama-3.1-8b-instant",
# "groq/moonshotai/kimi-k2-instruct",
# "mistral/mistral-medium-2508-eu",
# "openai/gpt-5",
# "openai/gpt-5-mini",
# "openai/gpt-5-nano",
# "xai/grok-4",
]
# Load the millionaire questions data
with open("fragen_antworten_en.json", "r") as f:
data = json.load(f)
# Who Wants to Be a Millionaire earnings ladder (German format)
EARNINGS_LADDER = {
1: 50, # Level 1: 50€
2: 100, # Level 2: 100
3: 200, # Level 3: 200€
4: 300, # Level 4: 300€
5: 500, # Level 5: 500€ (safe haven)
6: 1000, # Level 6: 1.000€
7: 2000, # Level 7: 2.000€
8: 4000, # Level 8: 4.000€
9: 8000, # Level 9: 8.000€
10: 16000, # Level 10: 16.000€ (safe haven)
11: 32000, # Level 11: 32.000€
12: 64000, # Level 12: 64.000€
13: 125000, # Level 13: 125.000€
14: 500000, # Level 14: 500.000€
15: 1000000, # Level 15: 1.000.000€
}
# No safe havens - earnings are simply based on questions answered correctly
class ModelResponse(BaseModel):
response_letter: Literal["A", "B", "C", "D"]
class QuestionResult(BaseModel):
question_level: int
question_text: str
question_text_en: str
options: Dict[str, str]
correct_answer: str
generated_answer: str
is_correct: bool
earnings_at_level: int
class ProgramResult(BaseModel):
program_id: int
questions_answered: int
final_earnings: int
eliminated_at_level: int | None
question_results: List[QuestionResult]
class ModelEvaluation(BaseModel):
model: str
total_programs: int
successful_programs: int
total_earnings: int
average_earnings: float
programs: List[ProgramResult]
timestamp: str
# Semaphore for concurrency control
semaphore = asyncio.Semaphore(15)
async def generate_response(
question_data: Dict[str, Any], model: str, parent_span_id: str
) -> tuple[str, str]:
"""Generate response for a single question using the specified model with retries."""
# Format question for the model
question_text = (
f"{question_data['question']}\n\nOptions:\n{question_data['options_str']}"
)
max_retries = 3
last_exception = None
for attempt in range(
max_retries + 1
): # 0, 1, 2, 3 (total of 4 attempts including first)
try:
response = await opper.call_async(
name="millionaire-question",
instructions="You are playing 'Who Wants to Be a Millionaire' in german. Read the question carefully and select the correct answer letter (A, B, C, or D). Only respond with the letter.",
output_schema=ModelResponse,
input=question_text,
model=model,
parent_span_id=parent_span_id,
)
return response.json_payload["response_letter"], response.span_id
except Exception as e:
last_exception = e
if attempt < max_retries:
print(
f"Attempt {attempt + 1} failed for {model}, retrying... Error: {e}"
)
# Small delay before retry
await asyncio.sleep(1)
else:
print(
f"All {max_retries + 1} attempts failed for {model}. Final error: {e}"
)
return "ERROR", None # Return ERROR and no span_id
def calculate_final_earnings(questions_answered: int, eliminated: bool) -> int:
"""Calculate final earnings based on questions answered correctly."""
if questions_answered == 0:
return 0
# Simply return earnings for the last question answered correctly
return EARNINGS_LADDER[questions_answered]
async def run_single_program(
program_data: Dict[str, Any], model: str, parent_span_id: str
) -> ProgramResult:
"""Run a single millionaire program for a model."""
async with semaphore:
program_id = program_data["program"]
questions = program_data["questions"]
question_results = []
# Create a child span for this specific program
program_span = await opper.spans.create_async(
name=f"millionaire-program-{program_id}",
input=f"Program {program_id} with {len(questions)} questions",
parent_id=parent_span_id,
)
try:
for i, question in enumerate(questions, 1):
try:
generated_answer, question_span_id = await generate_response(
question, model, program_span.id
)
correct_answer = question["answer"]
# Treat ERROR as incorrect answer
is_correct = (
generated_answer == correct_answer
and generated_answer != "ERROR"
)
# Add metrics to the question span if we have a span_id
if question_span_id:
try:
# Add correctness metric (1 for correct, 0 for incorrect)
await opper.span_metrics.create_metric_async(
span_id=question_span_id,
dimension="question_correct",
value=1 if is_correct else 0,
comment=f"Question {i} correctness (1=correct, 0=incorrect)",
)
# Add earnings metric
earnings_at_level = EARNINGS_LADDER[i]
await opper.span_metrics.create_metric_async(
span_id=question_span_id,
dimension="potential_earnings",
value=earnings_at_level,
comment=f"Potential earnings at level {i}: eur {earnings_at_level:,}",
)
# Add question level metric
await opper.span_metrics.create_metric_async(
span_id=question_span_id,
dimension="question_level",
value=i,
comment=f"Question difficulty level (1-15)",
)
except Exception as metric_error:
print(
f"Failed to add metrics for question {i}: {metric_error}"
)
question_result = QuestionResult(
question_level=i,
question_text=question["question"],
question_text_en=question["question_en"],
options=question["options"],
correct_answer=correct_answer,
generated_answer=generated_answer,
is_correct=is_correct,
earnings_at_level=EARNINGS_LADDER[i],
)
question_results.append(question_result)
# If wrong answer, eliminate
if not is_correct:
final_earnings = calculate_final_earnings(
i - 1, eliminated=True
)
result = ProgramResult(
program_id=program_id,
questions_answered=i - 1,
final_earnings=final_earnings,
eliminated_at_level=i,
question_results=question_results,
)
# Update program span with elimination result
await opper.spans.update_async(
span_id=program_span.id,
output=f"Eliminated at question {i}. Final earnings: eur {final_earnings:,}",
)
# Add program-level metrics
try:
await opper.span_metrics.create_metric_async(
span_id=program_span.id,
dimension="final_earnings",
value=final_earnings,
comment=f"Final earnings for program {program_id}: eur {final_earnings:,}",
)
await opper.span_metrics.create_metric_async(
span_id=program_span.id,
dimension="questions_answered",
value=i - 1,
comment=f"Questions answered correctly before elimination",
)
await opper.span_metrics.create_metric_async(
span_id=program_span.id,
dimension="program_success",
value=0,
comment="Program eliminated (0=eliminated, 1=completed)",
)
except Exception as metric_error:
print(f"Failed to add program metrics: {metric_error}")
return result
except Exception as e:
print(f"Error in program {program_id}, question {i}: {e}")
# Treat as elimination
final_earnings = calculate_final_earnings(i - 1, eliminated=True)
result = ProgramResult(
program_id=program_id,
questions_answered=i - 1,
final_earnings=final_earnings,
eliminated_at_level=i,
question_results=question_results,
)
# Update program span with error result
await opper.spans.update_async(
span_id=program_span.id,
output=f"Error at question {i}: {e}. Final earnings: eur {final_earnings:,}",
)
# Add program-level metrics for error case
try:
await opper.span_metrics.create_metric_async(
span_id=program_span.id,
dimension="final_earnings",
value=final_earnings,
comment=f"Final earnings for program {program_id}: eur {final_earnings:,}",
)
await opper.span_metrics.create_metric_async(
span_id=program_span.id,
dimension="questions_answered",
value=i - 1,
comment=f"Questions answered correctly before error",
)
await opper.span_metrics.create_metric_async(
span_id=program_span.id,
dimension="program_success",
value=0,
comment="Program failed due to error (0=eliminated, 1=completed)",
)
except Exception as metric_error:
print(f"Failed to add program metrics: {metric_error}")
return result
# Completed all questions successfully
final_earnings = calculate_final_earnings(len(questions), eliminated=False)
result = ProgramResult(
program_id=program_id,
questions_answered=len(questions),
final_earnings=final_earnings,
eliminated_at_level=None,
question_results=question_results,
)
# Update program span with success result
await opper.spans.update_async(
span_id=program_span.id,
output=f"Completed all {len(questions)} questions! Final earnings: eur {final_earnings:,}",
)
# Add program-level metrics for successful completion
try:
await opper.span_metrics.create_metric_async(
span_id=program_span.id,
dimension="final_earnings",
value=final_earnings,
comment=f"Final earnings for program {program_id}: eur {final_earnings:,}",
)
await opper.span_metrics.create_metric_async(
span_id=program_span.id,
dimension="questions_answered",
value=len(questions),
comment=f"All {len(questions)} questions answered correctly",
)
await opper.span_metrics.create_metric_async(
span_id=program_span.id,
dimension="program_success",
value=1,
comment="Program completed successfully (0=eliminated, 1=completed)",
)
except Exception as metric_error:
print(f"Failed to add program metrics: {metric_error}")
return result
except Exception as e:
# Update program span with general error
await opper.spans.update_async(
span_id=program_span.id, output=f"Program failed with error: {e}"
)
raise
async def evaluate_model(model: str) -> ModelEvaluation:
"""Evaluate a single model across all 45 programs."""
print(f"Starting evaluation for model: {model}")
# Create parent span for this model evaluation
parent_span = await opper.spans.create_async(
name=f"millionaire-evaluation-{model.replace('/', '-')}",
input=f"Evaluating {model} on {len(data)} millionaire programs",
)
# Run all programs concurrently
tasks = []
for program_data in data:
task = run_single_program(program_data, model, parent_span.id)
tasks.append(task)
# Execute all programs with concurrency control
program_results = await asyncio.gather(*tasks, return_exceptions=True)
# Filter out exceptions and count successes
valid_results = []
for result in program_results:
if isinstance(result, Exception):
print(f"Error in program: {result}")
else:
valid_results.append(result)
# Calculate statistics
total_programs = len(valid_results)
successful_programs = len(
[r for r in valid_results if r.eliminated_at_level is None]
)
total_earnings = sum(r.final_earnings for r in valid_results)
average_earnings = total_earnings / total_programs if total_programs > 0 else 0
# Update parent span with results
await opper.spans.update_async(
span_id=parent_span.id,
output=f"Completed {total_programs} programs. Total earnings: eur {total_earnings:,}. Average: eur {average_earnings:.2f}",
)
evaluation = ModelEvaluation(
model=model,
total_programs=total_programs,
successful_programs=successful_programs,
total_earnings=total_earnings,
average_earnings=average_earnings,
programs=valid_results,
timestamp=datetime.datetime.now().isoformat(),
)
print(
f"Completed evaluation for {model}: {successful_programs}/{total_programs} programs successful, eur {total_earnings:,} total earnings"
)
return evaluation
def save_results(evaluation: ModelEvaluation):
"""Save evaluation results to JSON file."""
results_dir = Path("millionaire_results")
results_dir.mkdir(exist_ok=True)
# Clean model name for filename
safe_model_name = evaluation.model.replace("/", "_").replace(":", "_")
filename = f"result_{safe_model_name}.json"
filepath = results_dir / filename
# Convert to dict for JSON serialization
result_dict = evaluation.model_dump()
with open(filepath, "w", encoding="utf-8") as f:
json.dump(result_dict, f, indent=2, ensure_ascii=False)
print(f"Results saved to: {filepath}")
async def run_all_evaluations():
"""Run evaluations for all models."""
print(
f"Starting millionaire benchmark evaluation with {len(models)} models and {len(data)} programs"
)
print(f"Concurrency: 15 simultaneous programs per model")
print(f"Total questions to be asked: {len(models) * len(data) * 15}")
all_results = []
for model in models:
try:
evaluation = await evaluate_model(model)
save_results(evaluation)
all_results.append(evaluation)
# Brief summary
print(f"\n--- Summary for {model} ---")
print(
f"Programs completed successfully: {evaluation.successful_programs}/{evaluation.total_programs}"
)
print(f"Total earnings: eur {evaluation.total_earnings:,}")
print(f"Average earnings per program: eur {evaluation.average_earnings:.2f}")
print(
f"Best program earnings: eur {max(r.final_earnings for r in evaluation.programs) if evaluation.programs else 0:,}"
)
except Exception as e:
print(f"Failed to evaluate model {model}: {e}")
continue
# Generate summary report
if all_results:
print(f"\n{'='*50}")
print("FINAL SUMMARY")
print(f"{'='*50}")
for evaluation in sorted(
all_results, key=lambda x: x.average_earnings, reverse=True
):
print(
f"{evaluation.model:30} | Avg: eur {evaluation.average_earnings:8.2f} | Total: eur {evaluation.total_earnings:10,} | Success: {evaluation.successful_programs:2d}/{evaluation.total_programs}"
)
# Save combined results
combined_results = {
"timestamp": datetime.datetime.now().isoformat(),
"total_models": len(all_results),
"total_programs_per_model": len(data),
"evaluations": [eval.model_dump() for eval in all_results],
}
with open(
"millionaire_results/combined_results.json", "w", encoding="utf-8"
) as f:
json.dump(combined_results, f, indent=2, ensure_ascii=False)
# Create summarized leaderboard
leaderboard = []
for evaluation in sorted(
all_results, key=lambda x: x.average_earnings, reverse=True
):
leaderboard_entry = {
"model": evaluation.model,
"total_earnings": evaluation.total_earnings,
"average_earnings": round(evaluation.average_earnings, 2),
"successful_programs": evaluation.successful_programs,
"total_programs": evaluation.total_programs,
"success_rate": (
round(
(evaluation.successful_programs / evaluation.total_programs)
* 100,
1,
)
if evaluation.total_programs > 0
else 0.0
),
"max_earnings_single_program": (
max(r.final_earnings for r in evaluation.programs)
if evaluation.programs
else 0
),
}
leaderboard.append(leaderboard_entry)
leaderboard_summary = {
"timestamp": datetime.datetime.now().isoformat(),
"total_models_evaluated": len(all_results),
"total_programs_per_model": len(data),
"leaderboard": leaderboard,
}
with open("millionaire_results/leaderboard.json", "w", encoding="utf-8") as f:
json.dump(leaderboard_summary, f, indent=2, ensure_ascii=False)
print(f"\nCombined results saved to: millionaire_results/combined_results.json")
print(f"Leaderboard saved to: millionaire_results/leaderboard.json")
if __name__ == "__main__":
asyncio.run(run_all_evaluations())