MY_First_agent / tools /get_public_ip_address.py
Pabed's picture
Upload get_public_ip_address.py
a473704 verified
raw
history blame contribute delete
863 Bytes
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