-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
688 lines (628 loc) · 24.3 KB
/
__init__.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import base64
import json
import uuid
from collections.abc import AsyncIterable, AsyncIterator, Awaitable, Callable
from typing import Literal, Optional
from aiohttp import ClientSession, WSMsgType
from azure.core.credentials import AzureKeyCredential
from azure.core.credentials_async import AsyncTokenCredential
from rtclient.models import (
AssistantContentPart,
AssistantMessageItem,
AudioFormat,
ClientMessageBase,
ErrorMessage,
FunctionCallItem,
FunctionCallOutputItem,
FunctionToolChoice,
InputAudioBufferAppendMessage,
InputAudioBufferClearedMessage,
InputAudioBufferClearMessage,
InputAudioBufferCommitMessage,
InputAudioBufferCommittedMessage,
InputAudioBufferSpeechStartedMessage,
InputAudioBufferSpeechStoppedMessage,
InputAudioContentPart,
InputAudioTranscription,
InputTextContentPart,
Item,
ItemCreatedMessage,
ItemCreateMessage,
ItemDeletedMessage,
ItemDeleteMessage,
ItemInputAudioTranscriptionCompletedMessage,
ItemInputAudioTranscriptionFailedMessage,
ItemParamStatus,
ItemTruncatedMessage,
ItemTruncateMessage,
MessageItem,
MessageItemType,
MessageRole,
Modality,
NoTurnDetection,
OutputTextContentPart,
RateLimits,
RateLimitsUpdatedMessage,
RealtimeError,
Response,
ResponseAudioDeltaMessage,
ResponseAudioDoneMessage,
ResponseAudioTranscriptDeltaMessage,
ResponseAudioTranscriptDoneMessage,
ResponseCancelledDetails,
ResponseCancelMessage,
ResponseContentPartAddedMessage,
ResponseContentPartDoneMessage,
ResponseCreatedMessage,
ResponseCreateMessage,
ResponseCreateParams,
ResponseDoneMessage,
ResponseFailedDetails,
ResponseFunctionCallArgumentsDeltaMessage,
ResponseFunctionCallArgumentsDoneMessage,
ResponseFunctionCallItem,
ResponseFunctionCallOutputItem,
ResponseIncompleteDetails,
ResponseItem,
ResponseItemAudioContentPart,
ResponseItemBase,
ResponseItemContentPart,
ResponseItemInputAudioContentPart,
ResponseItemInputTextContentPart,
ResponseItemStatus,
ResponseItemTextContentPart,
ResponseMessageItem,
ResponseOutputItemAddedMessage,
ResponseOutputItemDoneMessage,
ResponseStatus,
ResponseStatusDetails,
ResponseTextDeltaMessage,
ResponseTextDoneMessage,
ServerMessageBase,
ServerMessageType,
ServerVAD,
Session,
SessionCreatedMessage,
SessionUpdatedMessage,
SessionUpdateMessage,
SessionUpdateParams,
SystemContentPart,
SystemMessageItem,
Temperature,
ToolChoice,
ToolsDefinition,
TurnDetection,
Usage,
UserContentPart,
UserMessageItem,
UserMessageType,
Voice,
create_message_from_dict,
)
from rtclient.util.message_queue import MessageQueue
class RTLowLevelClient:
def __init__(
self,
url: Optional[str] = None,
token_credential: Optional[AsyncTokenCredential] = None,
key_credential: Optional[AzureKeyCredential] = None,
model: Optional[str] = None,
azure_deployment: Optional[str] = None,
):
self._is_azure_openai = url is not None
if self._is_azure_openai:
if key_credential is None and token_credential is None:
raise ValueError("key_credential or token_credential is required for Azure OpenAI")
if azure_deployment is None:
raise ValueError("azure_deployment is required for Azure OpenAI")
else:
if key_credential is None:
raise ValueError("key_credential is required for OpenAI")
if model is None:
raise ValueError("model is required for OpenAI")
self._url = url if self._is_azure_openai else "wss://api.openai.com/"
self._token_credential = token_credential
self._key_credential = key_credential
self._session = ClientSession(base_url=self._url)
self._model = model
self._azure_deployment = azure_deployment
self.request_id: Optional[uuid.UUID] = None
def _user_agent(self):
return "ms-rtclient-0.4.3"
async def _get_auth(self):
if self._token_credential:
scope = "https://cognitiveservices.azure.com/.default"
token = await self._token_credential.get_token(scope)
return {"Authorization": f"Bearer {token.token}"}
elif self._key_credential:
return {"api-key": self._key_credential.key}
else:
return {}
async def connect(self):
self.request_id = uuid.uuid4()
if self._is_azure_openai:
auth_headers = await self._get_auth()
headers = {"x-ms-client-request-id": str(self.request_id), "User-Agent": self._user_agent(), **auth_headers}
self.ws = await self._session.ws_connect(
"/openai/realtime",
headers=headers,
params={"deployment": self._azure_deployment, "api-version": "2024-10-01-preview"},
)
else:
headers = {
"Authorization": f"Bearer {self._key_credential.key}",
"openai-beta": "realtime=v1",
"User-Agent": self._user_agent(),
}
self.ws = await self._session.ws_connect("/v1/realtime", headers=headers, params={"model": self._model}, proxy='http://127.0.0.1:7890')
async def send(self, message: UserMessageType):
message._is_azure = self._is_azure_openai
message_json = message.model_dump_json(exclude_unset=True)
await self.ws.send_str(message_json)
async def recv(self) -> ServerMessageType | None:
if self.ws.closed:
return None
websocket_message = await self.ws.receive()
if websocket_message.type == WSMsgType.TEXT:
data = json.loads(websocket_message.data)
return create_message_from_dict(data)
else:
return None
def __aiter__(self) -> AsyncIterator[ServerMessageType | None]:
return self
async def __anext__(self):
message = await self.recv()
if message is None:
raise StopAsyncIteration
return message
async def close(self):
await self.ws.close()
await self._session.close()
@property
def closed(self) -> bool:
return self.ws.closed
async def __aenter__(self):
await self.connect()
return self
async def __aexit__(self, *args):
await self.close()
RTMessageContentChunkType = Literal["audio_transcript", "text", "audio", "tool_call_arguments"]
class RTMessageContentChunk:
def __init__(self, type: RTMessageContentChunkType, data: str, index: int):
self.type = type
self.data = data
self.index = index
class RTInputItem:
def __init__(
self,
id: str,
audio_start_ms: int,
has_transcription: bool,
receive: Callable[[], Awaitable[Optional[ServerMessageType]]],
):
self.id = id
self._has_transcription = has_transcription
self._receive = receive
self.previous_id: Optional[str] = None
self.audio_start_ms = audio_start_ms
self.audio_end_ms: Optional[int] = None
self.transcript: Optional[str] = None
self.commited: bool = False
self.error: Optional[RealtimeError] = None
def __await__(self):
async def resolve():
while True:
server_message = await self._receive()
if server_message is None:
break
match server_message.type:
case "input_audio_buffer.speech_stopped":
self.audio_end_ms = server_message.audio_end_ms
case "conversation.item.created":
self.previous_id = server_message.previous_item_id
if not self._has_transcription:
return
case "conversation.item.input_audio_transcription.completed":
self.transcript = server_message.transcript
return
case "conversation.item.input_audio_transcription.failed":
self.error = server_message.error
return
case "input_audio_buffer.committed":
self.commited = True
case _:
pass
return resolve().__await__()
class RTOutputItem:
def __init__(
self,
id: str,
response_id: str,
previous_id: Optional[str],
receive: Callable[[], Awaitable[Optional[ServerMessageType]]],
):
self.id = id
self.response_id = response_id
self.previous_id = previous_id
self.receive = receive
def __aiter__(self) -> AsyncIterator[RTMessageContentChunk]:
return self
async def __anext__(self):
while True:
# TODO: This loop is to allow ignoring some of the inner response messages,
# next iteration should properly extract meaning out of them and expose via relevant abstractions.
server_message = await self.receive()
if server_message is None or server_message.type == "response.output_item.done":
raise StopAsyncIteration
if server_message.type == "response.audio_transcript.delta":
return RTMessageContentChunk("audio_transcript", server_message.delta, server_message.content_index)
elif server_message.type == "response.audio.delta":
return RTMessageContentChunk("audio", server_message.delta, server_message.content_index)
elif server_message.type == "response.text.delta":
return RTMessageContentChunk("text", server_message.delta, server_message.content_index)
elif server_message.type == "response.function_call_arguments.delta":
return RTMessageContentChunk("tool_call_arguments", server_message.delta, server_message.output_index)
class RTResponse:
def __init__(
self,
id: str,
previous_id: Optional[str],
receive: Callable[[], Awaitable[Optional[ServerMessageType]]],
):
self.id = id
self.previous_id = previous_id
self._receive = receive
self._response_queue = MessageQueue(lambda: self._receive_response_message(), self._response_message_classifier)
self._item_queue = MessageQueue(lambda: self._response_queue.receive("ITEM"), self._item_id_extractor)
async def _receive_response_message(self):
return await self._receive()
def _response_message_classifier(self, message: ServerMessageType) -> Optional[str]:
if message.type in [
"response.done",
"response.output_item.added",
]:
return "RESPONSE"
elif message.type in [
"conversation.item.created",
"conversation.item.truncated",
"conversation.item.deleted",
"response.output_item.done",
"response.content_part.added",
"response.content_part.done",
"response.audio_transcript.delta",
"response.audio_transcript.done",
"response.audio.delta",
"response.audio.done",
"response.function_call_arguments.delta",
"response.function_call_arguments.done",
"response.text.delta",
"response.text.done",
]:
return "ITEM"
return None
def _item_id_extractor(self, message: ServerMessageType) -> Optional[str]:
if message.type in [
"conversation.item.created",
"response.output_item.done",
]:
return message.item.id
elif message.type in [
"conversation.item.truncated",
"conversation.item.deleted",
"response.content_part.added",
"response.content_part.done",
"response.audio_transcript.delta",
"response.audio_transcript.done",
"response.audio.delta",
"response.audio.done",
"response.function_call_arguments.delta",
"response.function_call_arguments.done",
"response.text.delta",
"response.text.done",
]:
return message.item_id
else:
return None
def __aiter__(self) -> AsyncIterator[RTOutputItem]:
return self
async def __anext__(self):
control_message = await self._response_queue.receive("RESPONSE")
if control_message is None or control_message.type == "response.done":
raise StopAsyncIteration
if control_message.type == "response.output_item.added":
item_id = control_message.item.id
return RTOutputItem(item_id, self.id, None, lambda: self._item_queue.receive(item_id))
raise ValueError(f"Unexpected message type {control_message.type}")
class RTClient:
def __init__(
self,
url: Optional[str] = None,
token_credential: Optional[AsyncTokenCredential] = None,
key_credential: Optional[AzureKeyCredential] = None,
model: Optional[str] = None,
azure_deployment: Optional[str] = None,
):
self._client = RTLowLevelClient(url, token_credential, key_credential, model, azure_deployment)
self._message_queue = MessageQueue(self._receive_message, self._message_id_extractor)
self._item_queue = MessageQueue(self._receive_item_message, self._item_id_extractor)
self._response_map: dict[str, str] = {}
self._transcription_enabled = False
@property
def request_id(self) -> uuid.UUID | None:
return self._client.request_id
async def _receive_message(self):
async for message in self._client:
return message
return None
def _message_id_extractor(self, message: ServerMessageType) -> Optional[str]:
if message.type in [
"session.created",
"input_audio_buffer.cleared",
"rate_limits.updated",
"error",
]:
return "SESSION"
elif message.type in [
"input_audio_buffer.speech_started",
"response.created",
]:
return "SESSION-ITEM"
elif message.type in [
"response.done",
"response.output_item.added",
"input_audio_buffer.speech_stopped",
"input_audio_buffer.committed",
"conversation.item.created",
"conversation.item.truncated",
"conversation.item.deleted",
"conversation.item.input_audio_transcription.completed",
"conversation.item.input_audio_transcription.failed",
"response.output_item.done",
"response.content_part.added",
"response.content_part.done",
"response.audio_transcript.delta",
"response.audio_transcript.done",
"response.audio.delta",
"response.audio.done",
"response.function_call_arguments.delta",
"response.function_call_arguments.done",
"response.text.delta",
"response.text.done",
]:
return "ITEM"
return None
async def _receive_item_message(self):
return await self._message_queue.receive("ITEM")
def _item_id_extractor(self, message: ServerMessageType) -> Optional[str]:
match message.type:
case "response.done":
return message.response.id
case "response.output_item.added":
self._response_map[message.item.id] = message.response_id
return message.response_id
case "input_audio_buffer.speech_stopped":
return message.item_id
case "input_audio_buffer.committed":
return message.item_id
case "conversation.item.created":
if message.item.id in self._response_map:
return self._response_map[message.item.id]
return message.item.id
case "conversation.item.truncated":
if message.item_id in self._response_map:
return self._response_map[message.item_id]
return message.item_id
case "conversation.item.deleted":
if message.item_id in self._response_map:
return self._response_map[message.item_id]
return message.item_id
case "conversation.item.input_audio_transcription.completed":
return message.item_id
case "conversation.item.input_audio_transcription.failed":
return message.item_id
case "response.output_item.done":
self._response_map.pop(message.item.id, None)
return message.response_id
case "response.content_part.added":
return message.response_id
case "response.content_part.done":
return message.response_id
case "response.audio_transcript.delta":
return message.response_id
case "response.audio_transcript.done":
return message.response_id
case "response.audio.delta":
return message.response_id
case "response.audio.done":
return message.response_id
case "response.function_call_arguments.delta":
return message.response_id
case "response.function_call_arguments.done":
return message.response_id
case "response.text.delta":
return message.response_id
case "response.text.done":
return message.response_id
case _:
return None
async def configure(
self,
model: Optional[str] = None,
modalities: Optional[set[Modality]] = None,
voice: Optional[Voice] = None,
instructions: Optional[str] = None,
input_audio_format: Optional[AudioFormat] = None,
output_audio_format: Optional[AudioFormat] = None,
input_audio_transcription: Optional[InputAudioTranscription] = None,
turn_detection: Optional[TurnDetection] = None,
tools: Optional[ToolsDefinition] = None,
tool_choice: Optional[ToolChoice] = None,
temperature: Optional[Temperature] = None,
max_response_output_tokens: Optional[int] = None,
):
self._transcription_enabled = input_audio_transcription is not None
session_update_params = SessionUpdateParams()
if model is not None:
session_update_params.model = model
if modalities is not None:
session_update_params.modalities = modalities
if voice is not None:
session_update_params.voice = voice
if instructions is not None:
session_update_params.instructions = instructions
if input_audio_format is not None:
session_update_params.input_audio_format = input_audio_format
if output_audio_format is not None:
session_update_params.output_audio_format = output_audio_format
if input_audio_transcription is not None:
session_update_params.input_audio_transcription = input_audio_transcription
if turn_detection is not None:
session_update_params.turn_detection = turn_detection
if tools is not None:
session_update_params.tools = tools
if tool_choice is not None:
session_update_params.tool_choice = tool_choice
if temperature is not None:
session_update_params.temperature = temperature
if max_response_output_tokens is not None:
session_update_params.max_response_output_tokens = max_response_output_tokens
await self._client.send(SessionUpdateMessage(session=session_update_params))
async def send_audio(self, audio: bytes):
base64_encoded = base64.b64encode(audio).decode("utf-8")
await self._client.send(InputAudioBufferAppendMessage(audio=base64_encoded))
async def commit_audio(self):
await self._client.send(InputAudioBufferCommitMessage())
async def clear_audio(self):
await self._client.send(InputAudioBufferClearMessage())
async def send_item(self, item: Item):
await self._client.send(ItemCreateMessage(item=item))
async def generate_response(self):
await self._client.send(ResponseCreateMessage())
async def control_messages(self) -> AsyncIterable[ServerMessageType]:
while True:
message = await self._message_queue.receive("SESSION")
if message is None:
break
yield message
async def items(self) -> AsyncIterable[RTInputItem | RTResponse]:
while True:
message = await self._message_queue.receive("SESSION-ITEM")
if message is None:
break
elif message.type == "input_audio_buffer.speech_started":
item_id = message.item_id
yield RTInputItem(
item_id,
message.audio_start_ms,
self._transcription_enabled,
lambda: self._item_queue.receive(item_id),
)
elif message.type == "response.created":
response_id = message.response.id
yield RTResponse(response_id, None, lambda: self._item_queue.receive(response_id))
else:
raise ValueError(f"Unexpected message type {message.type}")
async def connect(self):
await self._client.connect()
async def close(self):
await self._client.close()
async def __aenter__(self):
await self.connect()
return self
async def __aexit__(self, *args):
await self.close()
__all__ = [
"Voice",
"AudioFormat",
"Modality",
"NoTurnDetection",
"ServerVAD",
"TurnDetection",
"FunctionToolChoice",
"ToolChoice",
"MessageRole",
"InputAudioTranscription",
"ClientMessageBase",
"Temperature",
"ToolsDefinition",
"SessionUpdateParams",
"SessionUpdateMessage",
"InputAudioBufferAppendMessage",
"InputAudioBufferCommitMessage",
"InputAudioBufferClearMessage",
"MessageItemType",
"InputTextContentPart",
"InputAudioContentPart",
"OutputTextContentPart",
"SystemContentPart",
"UserContentPart",
"AssistantContentPart",
"ItemParamStatus",
"SystemMessageItem",
"UserMessageItem",
"AssistantMessageItem",
"MessageItem",
"FunctionCallItem",
"FunctionCallOutputItem",
"Item",
"ItemCreateMessage",
"ItemTruncateMessage",
"ItemDeleteMessage",
"ResponseCreateParams",
"ResponseCreateMessage",
"ResponseCancelMessage",
"RealtimeError",
"ServerMessageBase",
"ErrorMessage",
"Session",
"SessionCreatedMessage",
"SessionUpdatedMessage",
"InputAudioBufferCommittedMessage",
"InputAudioBufferClearedMessage",
"InputAudioBufferSpeechStartedMessage",
"InputAudioBufferSpeechStoppedMessage",
"ResponseItemStatus",
"ResponseItemInputTextContentPart",
"ResponseItemInputAudioContentPart",
"ResponseItemTextContentPart",
"ResponseItemAudioContentPart",
"ResponseItemContentPart",
"ResponseItemBase",
"ResponseMessageItem",
"ResponseFunctionCallItem",
"ResponseFunctionCallOutputItem",
"ResponseItem",
"ItemCreatedMessage",
"ItemTruncatedMessage",
"ItemDeletedMessage",
"ItemInputAudioTranscriptionCompletedMessage",
"ItemInputAudioTranscriptionFailedMessage",
"ResponseStatus",
"ResponseCancelledDetails",
"ResponseIncompleteDetails",
"ResponseFailedDetails",
"ResponseStatusDetails",
"Usage",
"Response",
"ResponseCreatedMessage",
"ResponseDoneMessage",
"ResponseOutputItemAddedMessage",
"ResponseOutputItemDoneMessage",
"ResponseContentPartAddedMessage",
"ResponseContentPartDoneMessage",
"ResponseTextDeltaMessage",
"ResponseTextDoneMessage",
"ResponseAudioTranscriptDeltaMessage",
"ResponseAudioTranscriptDoneMessage",
"ResponseAudioDeltaMessage",
"ResponseAudioDoneMessage",
"ResponseFunctionCallArgumentsDeltaMessage",
"ResponseFunctionCallArgumentsDoneMessage",
"RateLimits",
"RateLimitsUpdatedMessage",
"UserMessageType",
"ServerMessageType",
"create_message_from_dict",
]