Final_Assignment / tools /final_answer.py
Daniel Sellmeier
Verbesserte Antwortformatierung und Parsing-Kompatibilität für exakte Antwortabgleiche
0f8361a
from typing import Any, Optional
from smolagents.tools import Tool
class FinalAnswerTool(Tool):
name = "final_answer"
description = "Provides a final answer to the given problem in the exact format needed."
inputs = {'answer': {'type': 'any', 'description': 'The final answer to the problem'}}
output_type = "any"
def forward(self, answer: Any) -> Any:
# Wenn die Antwort ein Wörterbuch ist, versuchen wir, die strukturierte Antwort zu extrahieren
if isinstance(answer, dict) and 'answer' in answer:
answer = answer['answer']
# Wenn die Antwort ein langer Text mit Abschnitten ist, versuchen wir, die kurze Version zu extrahieren
if isinstance(answer, str) and "### 1. Task outcome (short version):" in answer:
# Extrahiere die kurze Antwort
short_answer_start = answer.find("### 1. Task outcome (short version):") + len("### 1. Task outcome (short version):")
# Suche nach dem Ende der kurzen Antwort (dem Beginn des nächsten Abschnitts)
next_section = answer.find("### 2.", short_answer_start)
if next_section != -1:
short_answer = answer[short_answer_start:next_section].strip()
else:
short_answer = answer[short_answer_start:].strip()
# Entferne mögliche Formatierungen
short_answer = short_answer.strip()
return short_answer
return answer
def __init__(self, *args, **kwargs):
self.is_initialized = False