wphoenix commited on
Commit
8405e54
·
verified ·
1 Parent(s): a866710

Added currency argument

Browse files
Files changed (1) hide show
  1. app.py +9 -8
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 all active crypto by market cap in USD.
33
- This can also be used when a specific crypto is required, then filter by that crypto name to get the
34
- data for the specific ticker.
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': 'USD'
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']['USD']['market_cap'], reverse=True)
63
- return {crypto['name']: crypto['quote']['USD'] for crypto in sorted_crypto}
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