Sborole-Final-Assignment / tools /FinalAnswerTool.py
Sborole's picture
Update tools/FinalAnswerTool.py
c7726e4 verified
raw
history blame
1.27 kB
from smolagents import LiteLLMModel
from src.settings import Settings
from src.utils import InputTokenRateLimiter
from smolagents.tools import Tool
from litellm import completion
import os
import re
settings = Settings()
print(settings.llm_model_id)
class FinalAnswerTool(Tool):
name = "final_answer"
description = "Return only the final answer as a clean string. Provides the exact, few comma separated words or a single final answer to the given question."
inputs = {
"answer": {"type": "string", "description": "The final, correctly formatted answer string."},
}
output_type = "string"
def forward(self, answer: str) -> str:
if not answer or str(answer).strip() == "":
return "I am unable to answer" # Fallback string for submission
answer = str(answer).strip()
match = re.search(r'boxed\{([^}]+)\}', answer)
if match:
answer = match.group(1)
# Remove LaTeX symbols and keep letters, digits, commas
answer = re.sub(r'[\$\{\}\\]', '', answer)
answer = re.sub(r'[^A-Za-z0-9,\.\-\+/ ]', ' ', answer)
answer = " ".join(answer.split()) # Remove extra spaces
if not answer:
return "I am unable to answer"
return answer