from typing import Any from smolagents.tools import Tool import requests class GetPublicIPAddressTool(Tool): name = "get_public_ip_address" description = "Fetches the public IP address of the machine." inputs = {} output_type = "string" def forward(self) -> str: try: response = requests.get("https://api.ipify.org?format=json", timeout=5) response.raise_for_status() ip = response.json()["ip"] return f"The public IP address is: {ip}" except requests.exceptions.RequestException as e: return f"Network error: {e}" except KeyError: return "Error: Invalid API response" except Exception as e: return f"Unexpected error: {e}" def __init__(self, *args, **kwargs): self.is_initialized = False