-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
160 lines (130 loc) · 4.83 KB
/
server.py
File metadata and controls
160 lines (130 loc) · 4.83 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import asyncio
import json
import logging
import os
from contextlib import asynccontextmanager
from dotenv import load_dotenv
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.middleware.cors import CORSMiddleware
from openai import AsyncAzureOpenAI
from pydantic import BaseModel
from agents import Agent, Runner, SQLiteSession, function_tool, set_default_openai_api, set_default_openai_client, set_tracing_disabled
from context_manager import trim_and_summarize
load_dotenv()
AZURE_OPENAI_API_KEY = os.getenv("AZURE_OPENAI_API_KEY", "")
AZURE_OPENAI_ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT", "")
AZURE_OPENAI_API_VERSION = os.getenv("AZURE_OPENAI_API_VERSION", "2025-03-01-preview")
AZURE_OPENAI_DEPLOYMENT = os.getenv("AZURE_OPENAI_DEPLOYMENT", "gpt-4.1-mini")
AZURE_OPENAI_SUMMARY_DEPLOYMENT = os.getenv("AZURE_OPENAI_SUMMARY_DEPLOYMENT", "gpt-4.1-mini")
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(name)-20s %(levelname)-7s %(message)s")
log = logging.getLogger("server")
azure_client = AsyncAzureOpenAI(
api_key=AZURE_OPENAI_API_KEY,
azure_endpoint=AZURE_OPENAI_ENDPOINT,
api_version=AZURE_OPENAI_API_VERSION,
)
set_default_openai_client(client=azure_client, use_for_tracing=False)
set_default_openai_api("chat_completions")
set_tracing_disabled(disabled=True)
SYSTEM_PROMPT = (
"You are a helpful, friendly assistant. "
"When you see a '[Previous conversation summary]' message, treat it as "
"a reliable recap of the earlier parts of the conversation that were "
"trimmed to save context space. Use it to maintain continuity.\n"
"You can check the weather using the get_weather tool."
)
@function_tool
def get_weather(city: str) -> str:
"""Get the current weather for a city."""
return f"The weather in {city} is 22 °C and sunny."
agent = Agent(
name="ContextManagedAssistant",
instructions=SYSTEM_PROMPT,
model=AZURE_OPENAI_DEPLOYMENT,
tools=[get_weather],
)
session = SQLiteSession("demo_session", "conversation_history.db")
class ConnectionManager:
def __init__(self):
self.active_connections: list[WebSocket] = []
async def connect(self, websocket: WebSocket):
await websocket.accept()
self.active_connections.append(websocket)
def disconnect(self, websocket: WebSocket):
if websocket in self.active_connections:
self.active_connections.remove(websocket)
async def broadcast(self, message: dict):
for connection in self.active_connections:
try:
await connection.send_json(message)
except Exception as e:
log.error(f"Error broadcasting message: {e}")
manager = ConnectionManager()
@asynccontextmanager
async def lifespan(app: FastAPI):
# Setup
yield
# Teardown
app = FastAPI(lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
async def trigger_event(event: dict):
await manager.broadcast(event)
class ChatMessage(BaseModel):
message: str
@app.post("/api/chat")
async def chat_endpoint(msg: ChatMessage):
user_input = msg.message
if user_input.lower() == "clear":
await session.clear_session()
await trigger_event({"type": "session_cleared"})
return {"response": "Session cleared."}
# Trim & Summarize
await trim_and_summarize(
session=session,
system_prompt=SYSTEM_PROMPT,
azure_client=azure_client,
summary_deployment=AZURE_OPENAI_SUMMARY_DEPLOYMENT,
event_callback=trigger_event
)
# Run Agent
try:
await trigger_event({"type": "agent_running"})
result = await Runner.run(
agent,
user_input,
session=session,
)
# Optionally send updated full session state
items = await session.get_items()
await trigger_event({
"type": "agent_response",
"response": result.final_output,
"session_items": items
})
return {"response": result.final_output}
except Exception as e:
log.exception("Agent run failed")
await trigger_event({"type": "error", "message": str(e)})
return {"error": str(e)}
@app.get("/api/session")
async def get_session():
items = await session.get_items()
return {"items": items}
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await manager.connect(websocket)
try:
while True:
data = await websocket.receive_text()
# We don't really expect client to send much here except maybe ping
except WebSocketDisconnect:
manager.disconnect(websocket)
if __name__ == "__main__":
import uvicorn
uvicorn.run("server:app", host="0.0.0.0", port=8000, reload=True)