Spaces:
Sleeping
Sleeping
File size: 1,587 Bytes
0f8361a |
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 |
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 |