-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwithout_stream.py
More file actions
74 lines (56 loc) · 1.45 KB
/
without_stream.py
File metadata and controls
74 lines (56 loc) · 1.45 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
62
63
64
65
66
67
68
69
70
71
72
73
74
from agents import (
Agent,
Runner,
RunConfig,
AsyncOpenAI,
function_tool,
OpenAIChatCompletionsModel,
enable_verbose_stdout_logging,
ModelSettings,
)
from agents.agent import StopAtTools
from openai.types.responses import ResponseTextDeltaEvent
import os
from dotenv import load_dotenv
import random
import chainlit as cl
import asyncio
load_dotenv()
enable_verbose_stdout_logging()
gemini_api_key = os.getenv("GEMINI_API_KEY")
external_client = AsyncOpenAI(
api_key=gemini_api_key,
base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
)
model = OpenAIChatCompletionsModel(
model="gemini-2.0-flash", openai_client=external_client
)
config = RunConfig(
model=model,
model_provider=external_client,
tracing_disabled=True,
)
@function_tool()
def weather_tool(city="new york"):
return f"the weather in the {city} is sunny"
@function_tool
def greet_user(user="Hasnain"):
return f"Hello! {user}"
agent = Agent(
name="Hasnain Assistent",
instructions="you are my assistant, use tools only when asked",
model=model,
model_settings=ModelSettings(
tool_choice="required",
),
tool_use_behavior=StopAtTools(stop_at_tool_names=["weather_tool", "greet_user"]),
tools=[weather_tool, greet_user],
)
async def main():
result = await Runner.run(
agent,
input="!",
run_config=config,
)
print(result.final_output)
asyncio.run(main())