File size: 1,023 Bytes
cf37a8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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. This tool must be used to submit the final answer."
    inputs = {'answer': {'type': 'any', 'description': 'The final answer to the problem'}}
    output_type = "any"

    def forward(self, answer: Any) -> Any:
        if answer is None or answer == "":
            return "This is a default answer."
        
        # Convert answer to string if it's not already
        if not isinstance(answer, str):
            answer = str(answer)
            
        # Remove any leading/trailing whitespace
        answer = answer.strip()
        
        # If the answer is still empty after stripping, return default
        if not answer:
            return "This is a default answer."
            
        return answer

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.is_initialized = True