Dips-1991 commited on
Commit
34f23aa
·
verified ·
1 Parent(s): 80327f6

Update tools/final_answer.py

Browse files
Files changed (1) hide show
  1. tools/final_answer.py +23 -8
tools/final_answer.py CHANGED
@@ -1,14 +1,29 @@
1
- from typing import Any, Optional
2
- from smolagents.tools import Tool
3
 
4
  class FinalAnswerTool(Tool):
5
  name = "final_answer"
6
- description = "Provides a final answer to the given problem."
7
- inputs = {'answer': {'type': 'any', 'description': 'The final answer to the problem'}}
8
- output_type = "any"
 
 
 
9
 
10
- def forward(self, answer: Any) -> Any:
11
- return answer
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  def __init__(self, *args, **kwargs):
14
- self.is_initialized = False
 
1
+ from typing import Any
2
+ from smolagents.tools import Tool, AgentImage
3
 
4
  class FinalAnswerTool(Tool):
5
  name = "final_answer"
6
+ description = "Provides the final formatted response to the user."
7
+ inputs = {
8
+ 'answer': {'type': 'any', 'description': 'The final answer to the problem'},
9
+ 'image': {'type': 'any', 'description': 'Optional image result to be displayed'}
10
+ }
11
+ output_type = "string"
12
 
13
+ def forward(self, answer: Any = "", image: Any = None) -> str:
14
+ # Handle AgentImage rendering
15
+ if isinstance(image, AgentImage):
16
+ try:
17
+ image_html = image.display() # or image.to_html()
18
+ except:
19
+ image_html = "[Image could not be displayed]"
20
+ # Combine text + image if both are provided
21
+ if answer:
22
+ return f"{answer}\n\n{image_html}"
23
+ else:
24
+ return image_html
25
+ else:
26
+ return str(answer)
27
 
28
  def __init__(self, *args, **kwargs):
29
+ super().__init__(*args, **kwargs)