qa-agent / tools /final_answer.py
Jan Krüger
QA Agent for Certification
8d4d62e
from smolagents import tool
@tool
def final_answer(answer: str) -> str:
"""
Tool to provide the final, precise answer to a test question.
IMPORTANT: This tool should receive only the direct answer without any explanations,
prefixes like "The answer is", or additional formatting.
Args:
answer: The precise, direct answer (e.g., "42", "Paris", "Yes", "2023-10-15")
Returns:
The clean final answer string
"""
# Clean the answer to ensure precision
clean_answer = str(answer).strip()
# Remove common prefixes that add unnecessary verbosity
prefixes_to_remove = [
"the answer is ",
"based on my research, ",
"according to my findings, ",
"the result is ",
"my answer is ",
"i found that ",
"the correct answer is ",
"after searching, ",
]
lower_answer = clean_answer.lower()
for prefix in prefixes_to_remove:
if lower_answer.startswith(prefix):
clean_answer = clean_answer[len(prefix):]
break
# Remove trailing periods for single-word answers (but keep for sentences)
if len(clean_answer.split()) == 1 and clean_answer.endswith('.'):
clean_answer = clean_answer[:-1]
return clean_answer.strip()
class FinalAnswerTool:
"""Compatibility class for the final answer tool"""
def __call__(self, answer: str) -> str:
result = final_answer(answer)
if isinstance(result, str):
return result
elif isinstance(result, NotImplementedError):
raise result
else:
return str(result)