from tools_registry import tool import urllib.request import json @tool() def convert_currency(amount: float, from_currency: str, to_currency: str) -> str: """Converts currency using latest exchange rates. Parameters: - amount: Amount to convert - from_currency: Source currency code (e.g., USD) - to_currency: Target currency code (e.g., EUR) """ try: url = f"https://open.er-api.com/v6/latest/{from_currency.upper()}" with urllib.request.urlopen(url) as response: data = json.loads(response.read()) if "rates" not in data: return "Error: Could not fetch exchange rates" rate = data["rates"].get(to_currency.upper()) if not rate: return f"Error: No rate found for {to_currency}" converted = amount * rate return f"{amount} {from_currency.upper()} = {converted:.2f} {to_currency.upper()}" except Exception as e: return f"Error converting currency: {str(e)}"