preslaff's picture
Removed API key
60457ee unverified
# 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)}```"