Spaces:
Sleeping
Sleeping
File size: 1,338 Bytes
49cd06a |
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 26 27 28 29 30 31 |
from smolagents import tool
import requests
@tool
def convert_currency(amount: float, from_currency: str, to_currency: str) -> str:
"""Convert an amount from one currency to another
Args:
amount: Amount to convert
from_currency: Source currency code (e.g., USD)
to_currency: Target currency code (e.g., EUR)
"""
try:
# Using ExchangeRate-API
base_url = "https://api.exchangerate-api.com/v4/latest"
# Get exchange rates for the base currency
response = requests.get(f"{base_url}/{from_currency.upper()}")
data = response.json()
if response.status_code == 200:
# Get the exchange rate for the target currency
if to_currency.upper() in data['rates']:
rate = data['rates'][to_currency.upper()]
converted_amount = amount * rate
return f"{amount} {from_currency.upper()} = {converted_amount:.2f} {to_currency.upper()} (Rate: 1 {from_currency.upper()} = {rate:.4f} {to_currency.upper()})"
else:
return f"Error: Target currency {to_currency.upper()} not found"
else:
return f"Error: {data.get('error', 'Failed to fetch exchange rates')}"
except Exception as e:
return f"Error converting currency: {str(e)}" |