File size: 2,620 Bytes
c641d5f | 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 | """Conservative, exact-match-safe final answer formatting."""
from __future__ import annotations
import re
import unicodedata
from decimal import Decimal, InvalidOperation
_PREFIX = re.compile(
r"^\s*(?:final\s+answer|answer|submitted\s+answer)\s*:\s*", re.IGNORECASE
)
def format_answer(value: object) -> str:
"""Remove presentation noise without changing answer semantics or case."""
text = unicodedata.normalize("NFC", str(value or ""))
text = text.replace("\r\n", "\n").replace("\r", "\n").strip()
had_prefix = bool(_PREFIX.match(text))
text = _PREFIX.sub("", text).strip()
if text.startswith("```") and text.endswith("```"):
lines = text.splitlines()
if len(lines) >= 3:
text = "\n".join(lines[1:-1]).strip()
if len(text) >= 2 and text[0] == text[-1] and text[0] in {'"', "'", "`"}:
text = text[1:-1].strip()
text = re.split(
r"\n+(?:explanation|reasoning|evidence|source)s?\s*:",
text,
maxsplit=1,
flags=re.IGNORECASE,
)[0]
if had_prefix and "\n" in text:
text = next((line.strip() for line in text.splitlines() if line.strip()), "")
if had_prefix:
text = re.split(
r"\s+(?:because|since)\s+", text, maxsplit=1, flags=re.IGNORECASE
)[0].strip()
# GAIA submissions are scalar strings; collapse accidental wrapping while
# preserving meaningful punctuation, casing, currency symbols, and commas.
text = re.sub(r"\s+", " ", text).strip()
if not text:
raise ValueError("Agent returned an empty answer")
return text
def apply_requested_format(question: str, answer: object) -> str:
"""Apply only explicit output-format constraints from the question."""
text = format_answer(answer)
lowered = question.lower()
if "usd" in lowered and "two decimal" in lowered:
numeric = re.sub(r"[^0-9.\-]", "", text)
try:
return f"${Decimal(numeric):,.2f}"
except InvalidOperation:
return text
if "ascending order" in lowered and (
"comma-delimited" in lowered or "comma separated" in lowered
):
values = re.findall(r"-?\d+(?:\.\d+)?", text)
if values:
values.sort(key=Decimal)
return ", ".join(values)
if "alphabet" in lowered and ("comma" in lowered or "list" in lowered):
values = [item.strip() for item in text.split(",") if item.strip()]
if len(values) > 1:
return ", ".join(sorted(values, key=str.casefold))
return text
|