Sborole-Final-Assignment / tools /FinalAnswerTool.py
Sborole's picture
Update tools/FinalAnswerTool.py
a6e0d09 verified
raw
history blame
1.98 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 = """
Takes the agent's computed result and formats the final answer.
If the question asks for time rounded to the nearest *thousand hours*,
you MUST:
1. First compute total time in hours.
2. THEN divide by FINAL ANSWER BY 1000 to convert to 'thousands of hours'.
3. Round that result to the nearest integer.
4. Output ONLY that integer (e.g., 17),
Always output a clean string with ONLY the final answer.
STRICT RULES:
- Do NOT use Markdown formatting.
- Do NOT use bold formatting.
- Do NOT surround the answer with **asterisks**.
- Do NOT add explanations, units, or extra words.
- If the answer is a number, output ONLY the number (e.g., 17000).
- Output must be plain ASCII text, no special symbols.
"""
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