File size: 863 Bytes
a473704
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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