MCPcopy Index your code
hub / github.com/algorithmicsuperintelligence/optillm / extract_answer

Function extract_answer

scripts/eval_math500_benchmark.py:49–78  ·  view source on GitHub ↗

Extract the answer from a math solution response.

(response: str)

Source from the content-addressed store, hash-verified

47 return dataset
48
49def extract_answer(response: str) -> Optional[str]:
50 """Extract the answer from a math solution response."""
51 if not response:
52 logger.debug("Empty response received")
53 return None
54
55 # Find the last \boxed{...} in the response
56 start_idx = response.rfind('\\boxed{')
57 if start_idx == -1:
58 logger.debug("No \\boxed{} found in response")
59 return None
60
61 # Find the matching closing brace
62 brace_count = 1
63 pos = start_idx + 7 # length of '\boxed{'
64
65 while pos < len(response) and brace_count > 0:
66 if response[pos] == '{':
67 brace_count += 1
68 elif response[pos] == '}':
69 brace_count -= 1
70 pos += 1
71
72 if brace_count == 0:
73 answer = response[start_idx + 7:pos - 1]
74 logger.debug(f"Extracted answer: {answer}")
75 return answer.strip()
76
77 logger.debug("No matching closing brace found")
78 return None
79
80def normalize_number(num_str: str) -> str:
81 """Helper function to normalize number representation."""

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected