-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
61 lines (49 loc) · 1.62 KB
/
main.py
File metadata and controls
61 lines (49 loc) · 1.62 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
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
from dotenv import load_dotenv
from agents import Agent, Runner, AsyncOpenAI, OpenAIChatCompletionsModel, function_tool
from agents.run import RunConfig
import asyncio
import http.client
import json
load_dotenv()
gemini_api_key = os.getenv('GEMINI_API_KEY')
# Check if the API key is present; if not, raise an error
if not gemini_api_key:
raise ValueError("GEMINI_API_KEY is not set. Please ensure it is defined in your .env file.")
external_client = AsyncOpenAI(
api_key=gemini_api_key,
base_url ="https://generativelanguage.googleapis.com/v1beta/openai/",
)
model = OpenAIChatCompletionsModel(
openai_client=external_client,
model="gemini-2.0-flash",
)
config = RunConfig(
model =model,
model_provider=external_client,
tracing_disabled=True,
)
@function_tool
async def fetch_coin_price(symbol: str) -> str:
conn = http.client.HTTPSConnection("api.binance.com")
endpoint = f"/api/v3/ticker/price?symbol={symbol.upper()}"
conn.request("GET", endpoint)
response = conn.getresponse()
if response.status != 200:
return f"Failed to fetch price for: {symbol.upper()}"
data = json.loads(response.read())
return f"The current price of {data['symbol']} is {data['price']} USD."
agent = Agent(
name="crypto-agent",
instructions="An agent that provides information about cryptocurrencies.",
tools=[fetch_coin_price],
)
async def main():
result = await Runner.run(
agent,
"What is the current price of BTCUSDT?",
run_config=config
)
print(f"Result: {result.final_output}")
if __name__ == "__main__":
asyncio.run(main())