-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathapp.py
137 lines (106 loc) · 4.2 KB
/
app.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import os
from dotenv import load_dotenv
from typing import cast, List
import chainlit as cl
from agents import Agent, Runner, AsyncOpenAI, OpenAIChatCompletionsModel
from agents.run import RunConfig
from agents.tool import function_tool
from agents.run_context import RunContextWrapper
# Load the environment variables from the .env file
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."
)
@cl.set_starters # type: ignore
async def set_starts() -> List[cl.Starter]:
return [
cl.Starter(
label="Greetings",
message="Hello! What can you help me with today?",
),
cl.Starter(
label="Weather",
message="Find the weather in Karachi.",
),
]
class MyContext:
def __init__(self, user_id: str):
self.user_id = user_id
self.seen_messages = []
@function_tool
@cl.step(type="weather tool")
def get_weather(location: str, unit: str = "C") -> str:
"""
Fetch the weather for a given location, returning a short description.
"""
# Example logic
return f"The weather in {location} is 22 degrees {unit}."
@function_tool
@cl.step(type="greeting tool")
def greet_user(context: RunContextWrapper[MyContext], greeting: str) -> str:
user_id = context.context.user_id
return f"Hello {user_id}, you said: {greeting}"
@cl.on_chat_start
async def start():
# Reference: https://ai.google.dev/gemini-api/docs/openai
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
)
"""Set up the chat session when a user connects."""
# Initialize an empty chat history in the session.
cl.user_session.set("chat_history", [])
cl.user_session.set("config", config)
agent: Agent = Agent(
name="Assistant",
tools=[greet_user, get_weather],
instructions="You are a helpful assistant. Call greet_user tool to greet the user. Always greet the user when session starts.",
model=model,
)
cl.user_session.set("agent", agent)
await cl.Message(
content="Welcome to the Ajmal Khan AI Assistant! How can I help you today?"
).send()
@cl.on_message
async def main(message: cl.Message):
"""Process incoming messages and generate responses."""
# Send a thinking message
msg = cl.Message(content="Thinking...")
await msg.send()
agent: Agent = cast(Agent, cl.user_session.get("agent"))
config: RunConfig = cast(RunConfig, cl.user_session.get("config"))
# Retrieve the chat history from the session.
history = cl.user_session.get("chat_history") or []
# Append the user's message to the history.
history.append({"role": "user", "content": message.content})
my_ctx = MyContext(user_id="Zia")
try:
print("\n[CALLING_AGENT_WITH_CONTEXT]\n", history, "\n")
result = Runner.run_sync(agent, history, run_config=config, context=my_ctx)
response_content = result.final_output
# Update the thinking message with the actual response
msg.content = response_content
await msg.update()
# Append the assistant's response to the history.
history.append({"role": "developer", "content": response_content})
# NOTE: Here we are appending the response to the history as a developer message.
# This is a BUG in the agents library.
# The expected behavior is to append the response to the history as an assistant message.
# Update the session with the new history.
cl.user_session.set("chat_history", history)
# Optional: Log the interaction
print(f"User: {message.content}")
print(f"Assistant: {response_content}")
except Exception as e:
msg.content = f"Error: {str(e)}"
await msg.update()
print(f"Error: {str(e)}")