dkolarova commited on
Commit
6208c4f
·
verified ·
1 Parent(s): bb3becf

Create tools.py

Browse files
Files changed (1) hide show
  1. tools.py +30 -0
tools.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from tool_registry import tool
2
+ import urllib.request
3
+ import json
4
+
5
+ @tool()
6
+ def convert_currency(amount: float, from_currency: str, to_currency: str) -> str:
7
+ """Converts currency using latest exchange rates.
8
+
9
+ Parameters:
10
+ - amount: Amount to convert
11
+ - from_currency: Source currency code (e.g., USD)
12
+ - to_currency: Target currency code (e.g., EUR)
13
+ """
14
+ try:
15
+ url = f"https://open.er-api.com/v6/latest/{from_currency.upper()}"
16
+ with urllib.request.urlopen(url) as response:
17
+ data = json.loads(response.read())
18
+
19
+ if "rates" not in data:
20
+ return "Error: Could not fetch exchange rates"
21
+
22
+ rate = data["rates"].get(to_currency.upper())
23
+ if not rate:
24
+ return f"Error: No rate found for {to_currency}"
25
+
26
+ converted = amount * rate
27
+ return f"{amount} {from_currency.upper()} = {converted:.2f} {to_currency.upper()}"
28
+
29
+ except Exception as e:
30
+ return f"Error converting currency: {str(e)}"