Da-Rabbit-Hole commited on
Commit
1112a98
·
verified ·
1 Parent(s): 664c3ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -46
app.py CHANGED
@@ -1,49 +1,47 @@
1
- from smolagents import tool
2
- import requests
3
- import pandas as pd
4
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
5
- # @tool
6
- # def my_custom_tool(arg1:str, arg2:int): str #it's important to specify the return type
7
- # #Keep this format for the description / args / args description but feel free to modify the tool
8
- # """A tool that does nothing yet
9
- # Args:
10
- # arg1: the first argument
11
- # arg2: the second argument
12
- # """
13
- # # return
14
-
15
-
16
- SYSTEM_PROMPT= """This is a tool to get current crypto token market prices in usd currency using the coingecko api.
17
-
18
- This is the crypto token prices tool
19
-
20
- args1 (str): Input for crypto_ids is a string
21
- args2: (str): Output for currency is an srting
22
-
23
  Returns:
24
- (str): You should return all valid requests with the crypto id, and its current price in usd denominations.
25
- """
 
26
 
27
  @tool
28
- # Function to get current prices of specified cryptocurrencies
29
- def get_crypto_prices(crypto_ids='tokens', currency='usd'): str
30
-
31
- url = "https://pro-api.coingecko.com/api/v3/simple/price?&x_cg_demo_api_key=CG-4TqTQv39JuPvRFmz3dz5Kcff"
32
-
33
- response = requests.get(url)
34
-
35
- if response.status_code == 200:
36
- data = response.json()
37
- print (pd.DataFrame(data).T) # Transpose for better readability
38
- else:
39
- print("Failed to retrieve data")
40
-
41
- # Specify the cryptocurrencies you want to check
42
- cryptos_ids = ['bitcoin&vs_currencies=usd&x_cg_pro_api_key=CG-4TqTQv39JuPvRFmz3dz5Kcff',
43
- 'ethereum&vs_currencies=usd&x_cg_pro_api_key=CG-4TqTQv39JuPvRFmz3dz5Kcff',
44
- 'ripple&vs_currencies=usd&x_cg_pro_api_key=CG-4TqTQv39JuPvRFmz3dz5Kcff']
45
-
46
- # Get current prices
47
- prices = get_crypto_prices(cryptos_ids)
48
-
49
- print(prices)
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import tool
2
+ import requests
3
+ import pandas as pd
4
+
5
+ @tool def my_custom_tool(arg1: str, arg2: int) -> str:
6
+ """A placeholder tool that simply echoes its inputs.
7
+ Args:
8
+ arg1: The first argument.
9
+ arg2: The second argument.
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  Returns:
11
+ A short status string.
12
+ """
13
+ return f"arg1={arg1}, arg2={arg2}"
14
 
15
  @tool
16
+ def get_crypto_prices(crypto_ids: list[str], currency: str = "usd") -> dict:
17
+ """Get current crypto prices using CoinGecko Simple Price.
18
+ Args:
19
+ crypto_ids: List of CoinGecko coin IDs, e.g. ["bitcoin","ethereum","ripple"].
20
+ currency: Fiat currency code, e.g. "usd".
21
+ Returns:
22
+ Dict mapping coin_id -> price in the requested currency.
23
+ """
24
+ if not crypto_ids:
25
+ return {"error": "crypto_ids must be a non-empty list"}
26
+
27
+ url = "https://api.coingecko.com/api/v3/simple/price"
28
+ params = {"ids": ",".join(crypto_ids), "vs_currencies": currency.lower()}
29
+
30
+ try:
31
+ r = requests.get(url, params=params, timeout=10)
32
+ r.raise_for_status()
33
+ except Exception as e:
34
+ return {"error": f"request failed: {e}"}
35
+
36
+ data = r.json() # shape: {"bitcoin":{"usd":12345.6}, ...}
37
+ # flatten to { "bitcoin": 12345.6, ... }
38
+ return {coin: payload.get(currency.lower()) for coin, payload in data.items()}
39
+
40
+ # Example usage outside the agent:
41
+ cryptos = ["bitcoin", "ethereum", "ripple"]
42
+ prices = get_crypto_prices(cryptos, currency="usd")
43
+ print(prices)
44
+
45
+ # Optional: nice tabular view for humans
46
+ df = pd.DataFrame.from_dict(prices, orient="index", columns=["usd"])
47
+ print(df)