-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCryptoDataAgent.py
More file actions
48 lines (39 loc) · 1.53 KB
/
CryptoDataAgent.py
File metadata and controls
48 lines (39 loc) · 1.53 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from agents import OpenAIChatCompletionsModel, Agent, Runner, set_tracing_disabled, AsyncOpenAI, function_tool
import dotenv
import os
import asyncio
import requests
dotenv.load_dotenv()
GEMINI_API_KEY = os.environ.get('GEMINI_API_KEY')
set_tracing_disabled(True)
client = AsyncOpenAI(
base_url='https://generativelanguage.googleapis.com/v1beta/openai/.',
api_key=GEMINI_API_KEY
)
model = OpenAIChatCompletionsModel('gemini-2.0-flash', openai_client=client)
@function_tool
def get_crypto_price(name: str):
"""
This tool provides real time information for coins
args: name: str
"""
print(f"Getting {name} Price...")
coins = requests.get("https://api.coinlore.net/api/tickers/").json()["data"]
for coin in coins:
if coin['name'].lower() == name.lower() or \
coin['symbol'].lower() == name.lower() or \
coin['nameid'] == name.lower():
price = coin['price_usd']
print(f"Fetched Price: {price}")
return {"name": coin["name"], "symbol": coin["symbol"], "price_usd": price}
return {"error": f"Coin '{name}' not found."}
CryptoDataAgent = Agent(
name='CryptoDataAgent',
instructions='You are a Crypto Agent. You provide real time rates using get_crypto_price tool. EXPECT the user always asks about coins. NEVER respond to any other TOPIC.',
model=model,
tools=[get_crypto_price]
)
async def main():
results = await Runner.run(CryptoDataAgent, 'What is the price of btc?')
print(results.final_output)
asyncio.run(main())