|
| 1 | +""" |
| 2 | +Eval Protocol example: AIME2025 chat completion evaluation |
| 3 | +
|
| 4 | +This example mirrors gpt-oss's AIME 2025 evaluation using OpenAI-compatible |
| 5 | +chat completions. It evaluates whether the assistant's final answer matches the |
| 6 | +ground-truth integer, extracting answers from \\boxed{...} or fallback digits. |
| 7 | +""" |
| 8 | + |
| 9 | +import re |
| 10 | +from typing import Any, Dict, List, Optional, Union |
| 11 | + |
| 12 | +from eval_protocol import EvaluateResult, MetricResult, reward_function |
| 13 | +from eval_protocol.models import Message |
| 14 | + |
| 15 | + |
| 16 | +def _extract_boxed_text(text: str) -> str: |
| 17 | + """ |
| 18 | + Extract the last occurrence of a boxed answer (\\boxed{...} or \\framebox{...}). |
| 19 | + If none found, fall back to the last integer found in the text. |
| 20 | + """ |
| 21 | + if not text: |
| 22 | + return "" |
| 23 | + |
| 24 | + pattern_boxed = r"boxed{(.*?)}|framebox{(.*?)}" |
| 25 | + matches = re.findall(pattern_boxed, text, re.DOTALL) |
| 26 | + if matches: |
| 27 | + # Iterate from the end to prioritize the final boxed answer |
| 28 | + for match in matches[::-1]: |
| 29 | + for group in match: |
| 30 | + if group: |
| 31 | + return group.split(",")[-1].strip() |
| 32 | + |
| 33 | + # Fallback: last integer in the text |
| 34 | + matches_digits = re.findall(r"\d+", text, re.DOTALL) |
| 35 | + if matches_digits: |
| 36 | + return matches_digits[-1] |
| 37 | + return "" |
| 38 | + |
| 39 | + |
| 40 | +def _normalize_to_int_or_none(s: str) -> Optional[int]: |
| 41 | + if s is None: |
| 42 | + return None |
| 43 | + # Only take leading digits |
| 44 | + m = re.match(r"\d+", str(s).strip()) |
| 45 | + if not m: |
| 46 | + return None |
| 47 | + try: |
| 48 | + return int(m.group(0)) |
| 49 | + except ValueError: |
| 50 | + return None |
| 51 | + |
| 52 | + |
| 53 | +@reward_function(id="aime2025_exact_match") |
| 54 | +def evaluate( |
| 55 | + messages: Union[List[Message], List[Dict[str, Any]]], |
| 56 | + ground_truth: Optional[str] = None, |
| 57 | + **kwargs, |
| 58 | +) -> EvaluateResult: |
| 59 | + """ |
| 60 | + Score 1.0 if extracted final answer equals the ground-truth integer, else 0.0. |
| 61 | + """ |
| 62 | + if not messages: |
| 63 | + return EvaluateResult( |
| 64 | + score=0.0, |
| 65 | + reason="No messages provided", |
| 66 | + is_score_valid=False, |
| 67 | + metrics={ |
| 68 | + "parse_status": MetricResult(score=0.0, is_score_valid=False, reason="empty messages") |
| 69 | + }, |
| 70 | + ) |
| 71 | + |
| 72 | + last_msg = messages[-1] |
| 73 | + content = last_msg["content"] if isinstance(last_msg, dict) else (last_msg.content or "") |
| 74 | + |
| 75 | + extracted_text = _extract_boxed_text(content) |
| 76 | + extracted_int = _normalize_to_int_or_none(extracted_text) |
| 77 | + gt_int = _normalize_to_int_or_none(ground_truth if ground_truth is not None else "") |
| 78 | + |
| 79 | + is_valid = extracted_int is not None and gt_int is not None |
| 80 | + score = 1.0 if (is_valid and extracted_int == gt_int) else 0.0 |
| 81 | + |
| 82 | + metrics: Dict[str, MetricResult] = { |
| 83 | + "exact_match": MetricResult( |
| 84 | + score=score, |
| 85 | + is_score_valid=is_valid, |
| 86 | + reason=( |
| 87 | + "Parsed both integers and they matched" |
| 88 | + if score == 1.0 |
| 89 | + else ( |
| 90 | + "Parsed integers did not match" |
| 91 | + if is_valid |
| 92 | + else "Failed to parse integer from prediction or ground truth" |
| 93 | + ) |
| 94 | + ), |
| 95 | + data={ |
| 96 | + "extracted_text": extracted_text, |
| 97 | + "extracted_int": extracted_int, |
| 98 | + "ground_truth_int": gt_int, |
| 99 | + }, |
| 100 | + ) |
| 101 | + } |
| 102 | + |
| 103 | + return EvaluateResult( |
| 104 | + score=score, |
| 105 | + reason=("Answer correct" if score == 1.0 else "Answer incorrect"), |
| 106 | + is_score_valid=is_valid, |
| 107 | + metrics=metrics, |
| 108 | + ) |
| 109 | + |
| 110 | + |
0 commit comments