-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathagent.py
316 lines (264 loc) · 12.6 KB
/
agent.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import asyncio
import base64
import logging
import os
from builtins import anext
from typing import Any
from agora.rtc.rtc_connection import RTCConnection, RTCConnInfo
from attr import dataclass
from agora_realtime_ai_api.rtc import Channel, ChatMessage, RtcEngine, RtcOptions
from .logger import setup_logger
from .realtime.struct import InputAudioBufferCommitted, InputAudioBufferSpeechStarted, InputAudioBufferSpeechStopped, InputAudioTranscription, ItemCreated, ItemInputAudioTranscriptionCompleted, RateLimitsUpdated, ResponseAudioDelta, ResponseAudioDone, ResponseAudioTranscriptDelta, ResponseAudioTranscriptDone, ResponseContentPartAdded, ResponseContentPartDone, ResponseCreated, ResponseDone, ResponseOutputItemAdded, ResponseOutputItemDone, ServerVADUpdateParams, SessionUpdate, SessionUpdateParams, SessionUpdated, Voices, to_json
from .realtime.connection import RealtimeApiConnection
from .tools import ClientToolCallResponse, ToolContext
from .utils import PCMWriter
# Set up the logger with color and timestamp support
logger = setup_logger(name=__name__, log_level=logging.INFO)
def _monitor_queue_size(queue: asyncio.Queue, queue_name: str, threshold: int = 5) -> None:
queue_size = queue.qsize()
if queue_size > threshold:
logger.warning(f"Queue {queue_name} size exceeded {threshold}: current size {queue_size}")
async def wait_for_remote_user(channel: Channel) -> int:
remote_users = list(channel.remote_users.keys())
if len(remote_users) > 0:
return remote_users[0]
future = asyncio.Future[int]()
channel.once("user_joined", lambda conn, user_id: future.set_result(user_id))
try:
# Wait for the remote user with a timeout of 30 seconds
remote_user = await asyncio.wait_for(future, timeout=15.0)
return remote_user
except KeyboardInterrupt:
future.cancel()
except Exception as e:
logger.error(f"Error waiting for remote user: {e}")
raise
@dataclass(frozen=True, kw_only=True)
class InferenceConfig:
system_message: str | None = None
turn_detection: ServerVADUpdateParams | None = None # MARK: CHECK!
voice: Voices | None = None
class RealtimeKitAgent:
engine: RtcEngine
channel: Channel
connection: RealtimeApiConnection
audio_queue: asyncio.Queue[bytes] = asyncio.Queue()
message_queue: asyncio.Queue[ResponseAudioTranscriptDelta] = (
asyncio.Queue()
)
message_done_queue: asyncio.Queue[ResponseAudioTranscriptDone] = (
asyncio.Queue()
)
tools: ToolContext | None = None
_client_tool_futures: dict[str, asyncio.Future[ClientToolCallResponse]]
@classmethod
async def setup_and_run_agent(
cls,
*,
engine: RtcEngine,
options: RtcOptions,
inference_config: InferenceConfig,
tools: ToolContext | None,
) -> None:
channel = engine.create_channel(options)
await channel.connect()
try:
async with RealtimeApiConnection(
base_uri=os.getenv("REALTIME_API_BASE_URI", "wss://api.openai.com"),
api_key=os.getenv("OPENAI_API_KEY"),
verbose=False,
) as connection:
await connection.send_request(
SessionUpdate(
session=SessionUpdateParams(
# MARK: check this
turn_detection=inference_config.turn_detection,
tools=tools.model_description() if tools else [],
tool_choice="auto",
input_audio_format="pcm16",
output_audio_format="pcm16",
instructions=inference_config.system_message,
voice=inference_config.voice,
model=os.environ.get("OPENAI_MODEL", "gpt-4o-realtime-preview"),
modalities=["text", "audio"],
temperature=0.8,
max_response_output_tokens="inf",
input_audio_transcription=InputAudioTranscription(model="whisper-1")
)
)
)
start_session_message = await anext(connection.listen())
# assert isinstance(start_session_message, messages.StartSession)
logger.info(
f"Session started: {start_session_message.session.id} model: {start_session_message.session.model}"
)
agent = cls(
connection=connection,
tools=tools,
channel=channel,
)
await agent.run()
finally:
await channel.disconnect()
await connection.close()
def __init__(
self,
*,
connection: RealtimeApiConnection,
tools: ToolContext | None,
channel: Channel,
) -> None:
self.connection = connection
self.tools = tools
self._client_tool_futures = {}
self.channel = channel
self.subscribe_user = None
self.write_pcm = os.environ.get("WRITE_AGENT_PCM", "false") == "true"
logger.info(f"Write PCM: {self.write_pcm}")
async def run(self) -> None:
try:
def log_exception(t: asyncio.Task[Any]) -> None:
if not t.cancelled() and t.exception():
logger.error(
"unhandled exception",
exc_info=t.exception(),
)
def on_stream_message(agora_local_user, user_id, stream_id, data, length) -> None:
logger.info(f"Received stream message with length: {length}")
self.channel.on("stream_message", on_stream_message)
logger.info("Waiting for remote user to join")
self.subscribe_user = await wait_for_remote_user(self.channel)
logger.info(f"Subscribing to user {self.subscribe_user}")
await self.channel.subscribe_audio(self.subscribe_user)
async def on_user_left(
agora_rtc_conn: RTCConnection, user_id: int, reason: int
):
logger.info(f"User left: {user_id}")
if self.subscribe_user == user_id:
self.subscribe_user = None
logger.info("Subscribed user left, disconnecting")
await self.channel.disconnect()
self.channel.on("user_left", on_user_left)
disconnected_future = asyncio.Future[None]()
def callback(agora_rtc_conn: RTCConnection, conn_info: RTCConnInfo, reason):
logger.info(f"Connection state changed: {conn_info.state}")
if conn_info.state == 1:
if not disconnected_future.done():
disconnected_future.set_result(None)
self.channel.on("connection_state_changed", callback)
asyncio.create_task(self.rtc_to_model()).add_done_callback(log_exception)
asyncio.create_task(self.model_to_rtc()).add_done_callback(log_exception)
asyncio.create_task(self._process_model_messages()).add_done_callback(
log_exception
)
await disconnected_future
logger.info("Agent finished running")
except asyncio.CancelledError:
logger.info("Agent cancelled")
except Exception as e:
logger.error(f"Error running agent: {e}")
raise
async def rtc_to_model(self) -> None:
while self.subscribe_user is None or self.channel.get_audio_frames(self.subscribe_user) is None:
await asyncio.sleep(0.1)
audio_frames = self.channel.get_audio_frames(self.subscribe_user)
# Initialize PCMWriter for receiving audio
pcm_writer = PCMWriter(prefix="rtc_to_model", write_pcm=self.write_pcm)
try:
async for audio_frame in audio_frames:
# Process received audio (send to model)
_monitor_queue_size(self.audio_queue, "audio_queue")
await self.connection.send_audio_data(audio_frame.data)
# Write PCM data if enabled
await pcm_writer.write(audio_frame.data)
await asyncio.sleep(0) # Yield control to allow other tasks to run
except asyncio.CancelledError:
# Write any remaining PCM data before exiting
await pcm_writer.flush()
raise # Re-raise the exception to propagate cancellation
async def model_to_rtc(self) -> None:
# Initialize PCMWriter for sending audio
pcm_writer = PCMWriter(prefix="model_to_rtc", write_pcm=self.write_pcm)
try:
while True:
# Get audio frame from the model output
frame = await self.audio_queue.get()
# Process sending audio (to RTC)
await self.channel.push_audio_frame(frame)
# Write PCM data if enabled
await pcm_writer.write(frame)
except asyncio.CancelledError:
# Write any remaining PCM data before exiting
await pcm_writer.flush()
raise # Re-raise the cancelled exception to properly exit the task
async def _process_model_messages(self) -> None:
async for message in self.connection.listen():
# logger.info(f"Received message {message=}")
match message:
case ResponseAudioDelta():
# logger.info("Received audio message")
self.audio_queue.put_nowait(base64.b64decode(message.delta))
# loop.call_soon_threadsafe(self.audio_queue.put_nowait, base64.b64decode(message.delta))
logger.debug(f"TMS:ResponseAudioDelta: response_id:{message.response_id},item_id: {message.item_id}")
case ResponseAudioTranscriptDelta():
# logger.info(f"Received text message {message=}")
asyncio.create_task(self.channel.chat.send_message(
ChatMessage(
message=to_json(message), msg_id=message.item_id
)
))
case ResponseAudioTranscriptDone():
logger.info(f"Text message done: {message=}")
asyncio.create_task(self.channel.chat.send_message(
ChatMessage(
message=to_json(message), msg_id=message.item_id
)
))
case InputAudioBufferSpeechStarted():
await self.channel.clear_sender_audio_buffer()
# clear the audio queue so audio stops playing
while not self.audio_queue.empty():
self.audio_queue.get_nowait()
logger.info(f"TMS:InputAudioBufferSpeechStarted: item_id: {message.item_id}")
case InputAudioBufferSpeechStopped():
logger.info(f"TMS:InputAudioBufferSpeechStopped: item_id: {message.item_id}")
pass
case ItemInputAudioTranscriptionCompleted():
logger.info(f"ItemInputAudioTranscriptionCompleted: {message=}")
asyncio.create_task(self.channel.chat.send_message(
ChatMessage(
message=to_json(message), msg_id=message.item_id
)
))
# InputAudioBufferCommitted
case InputAudioBufferCommitted():
pass
case ItemCreated():
pass
# ResponseCreated
case ResponseCreated():
pass
# ResponseDone
case ResponseDone():
pass
# ResponseOutputItemAdded
case ResponseOutputItemAdded():
pass
# ResponseContenPartAdded
case ResponseContentPartAdded():
pass
# ResponseAudioDone
case ResponseAudioDone():
pass
# ResponseContentPartDone
case ResponseContentPartDone():
pass
# ResponseOutputItemDone
case ResponseOutputItemDone():
pass
case SessionUpdated():
pass
case RateLimitsUpdated():
pass
case _:
logger.warning(f"Unhandled message {message=}")