Extract the final answer from model response. Args: text: Model response text using_boxed: Whether to extract from boxed format Returns: Extracted answer string
(text: str, using_boxed: bool = True)
| 48 | |
| 49 | |
| 50 | def _extract_number(text: str, using_boxed: bool = True) -> str: |
| 51 | """ |
| 52 | Extract the final answer from model response. |
| 53 | |
| 54 | Args: |
| 55 | text: Model response text |
| 56 | using_boxed: Whether to extract from boxed format |
| 57 | |
| 58 | Returns: |
| 59 | Extracted answer string |
| 60 | """ |
| 61 | if not using_boxed: |
| 62 | # Try to extract answer after #### marker (GSM8K format) |
| 63 | if "####" in text: |
| 64 | answer_part = text.split('####')[-1].strip() |
| 65 | # Extract any number or expression |
| 66 | numbers = re.findall(r'[-+]?\d+', answer_part) |
| 67 | if numbers: |
| 68 | return numbers[-1] |
| 69 | # If no #### marker, try to find the last number in the text |
| 70 | numbers = re.findall(r'[-+]?\d+', text) |
| 71 | if numbers: |
| 72 | return numbers[-1] |
| 73 | return text.strip() |
| 74 | |
| 75 | elif "boxed{" in text: |
| 76 | # Extract the boxed number/expression |
| 77 | numbers = re.findall(r'boxed\{(.*?)\}', text) |
| 78 | if numbers: |
| 79 | # Extract numeric value from boxed content |
| 80 | boxed_content = numbers[-1] |
| 81 | # Try to extract number from boxed content |
| 82 | numeric_match = re.search(r'[-+]?\d+', boxed_content) |
| 83 | if numeric_match: |
| 84 | return numeric_match.group() |
| 85 | return boxed_content.strip() |
| 86 | |
| 87 | # Fallback: try to find the last number in the text |
| 88 | numbers = re.findall(r'[-+]?\d+', text) |
| 89 | if numbers: |
| 90 | return numbers[-1] |
| 91 | |
| 92 | return text.strip() |
| 93 | |
| 94 | |
| 95 | def math_equal_reward_fn(question: str, answer1: str, answer2: str, standard_answer: str) -> List[float]: |
no outgoing calls
no test coverage detected