Spaces:
Sleeping
Sleeping
Added currency argument
Browse files
app.py
CHANGED
@@ -28,13 +28,13 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's important to specify the ret
|
|
28 |
return "What magic will you build ?"
|
29 |
|
30 |
@tool
|
31 |
-
def fetch_active_crypto() -> Optional[Dict[str, Any]]:
|
32 |
-
"""A tool that fetches
|
33 |
-
|
34 |
-
|
35 |
|
36 |
Returns:
|
37 |
-
Optional[Dict[str, Any]]: A dictionary containing cryptocurrencies by market cap,
|
38 |
or None if an error occurs.
|
39 |
"""
|
40 |
# url = 'https://sandbox-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
|
@@ -42,7 +42,7 @@ def fetch_active_crypto() -> Optional[Dict[str, Any]]:
|
|
42 |
parameters = {
|
43 |
'start': '1',
|
44 |
'limit': '5000',
|
45 |
-
'convert':
|
46 |
}
|
47 |
headers = {
|
48 |
'Accepts': 'application/json',
|
@@ -58,9 +58,10 @@ def fetch_active_crypto() -> Optional[Dict[str, Any]]:
|
|
58 |
response.raise_for_status() # Raise an exception for HTTP errors
|
59 |
data = json.loads(response.text)
|
60 |
|
|
|
61 |
if 'data' in data:
|
62 |
-
sorted_crypto = sorted(data['data'], key=lambda x: x['quote'][
|
63 |
-
return {crypto['name']: crypto['quote'][
|
64 |
else:
|
65 |
print("No data found in the response.")
|
66 |
return None
|
|
|
28 |
return "What magic will you build ?"
|
29 |
|
30 |
@tool
|
31 |
+
def fetch_active_crypto(currency: str = 'USD') -> Optional[Dict[str, Any]]:
|
32 |
+
"""A tool that fetches and reverse sorts by market_cap all active crypto in currency.
|
33 |
+
Args:
|
34 |
+
currency: A string representing the currency the value is returned in (default: 'USD').
|
35 |
|
36 |
Returns:
|
37 |
+
Optional[Dict[str, Any]]: A dictionary containing the top 10 cryptocurrencies by market cap,
|
38 |
or None if an error occurs.
|
39 |
"""
|
40 |
# url = 'https://sandbox-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
|
|
|
42 |
parameters = {
|
43 |
'start': '1',
|
44 |
'limit': '5000',
|
45 |
+
'convert': currency
|
46 |
}
|
47 |
headers = {
|
48 |
'Accepts': 'application/json',
|
|
|
58 |
response.raise_for_status() # Raise an exception for HTTP errors
|
59 |
data = json.loads(response.text)
|
60 |
|
61 |
+
# Extract the top 10 cryptocurrencies by market cap
|
62 |
if 'data' in data:
|
63 |
+
sorted_crypto = sorted(data['data'], key=lambda x: x['quote'][currency]['market_cap'], reverse=True)
|
64 |
+
return {crypto['name']: crypto['quote'][currency] for crypto in sorted_crypto}
|
65 |
else:
|
66 |
print("No data found in the response.")
|
67 |
return None
|