File size: 2,105 Bytes
2c23242
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fe992b
 
 
 
2c23242
 
 
 
 
 
 
 
d6cadf9
2c23242
 
 
 
 
8259463
 
2c23242
b171242
 
2c23242
8259463
 
2c23242
8fe992b
2c23242
 
 
d6cadf9
2c23242
b171242
8fe992b
8259463
2c23242
 
60457ee
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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."
#     inputs = {'answer': {'type': 'any', 'description': 'The final answer to the problem'}}
#     output_type = "any"

#     def forward(self, answer: Any) -> Any:
#         return answer

#     def __init__(self, *args, **kwargs):
#         self.is_initialized = False

from typing import Any
from smolagents.tools import Tool

class FinalAnswerTool(Tool):
    name = "final_answer"
    description = "Formats and presents final answers in a human-readable format."
    inputs = {'answer': {'type': 'any', 'description': 'The final answer, which could be job listings or a general response'}}
    output_type = "string"

    def forward(self, answer: Any) -> str:
        """
        Determines the type of answer and formats it accordingly.
        """
        # Case 1: If the answer is a simple string, return it directly
        if isinstance(answer, str):
            return f"📌 **Final Answer:**\n\n{answer}"
        
        # Case 2: If the answer is a list of job listings, format them properly
        elif isinstance(answer, list) and all(isinstance(job, dict) for job in answer):
            if not answer:
                return "⚠️ No job listings found."

            formatted_output = "**🔍 Job Listings Found**\n\n"

            for idx, job in enumerate(answer, start=1):
                title = job.get("Title", "Unknown Job Title")
                company = job.get("Company", "Unknown Company")
                location = job.get("Location", "Anywhere")

                formatted_output += (
                    f"**{idx}. {title}**\n"
                    f"   - **Company:** {company}\n"
                    f"   - **Location:** {location}\n\n"
                )
            return formatted_output.strip()

        # Case 3: If it's an unexpected format, return it as a formatted string
        else:
            return f"📌 **Final Answer:**\n\n```{str(answer)}```"