openai-agents 0.0.5__py3-none-any.whl → 0.0.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of openai-agents might be problematic. Click here for more details.
- agents/__init__.py +12 -0
- agents/models/openai_provider.py +13 -0
- agents/tracing/__init__.py +12 -0
- agents/tracing/create.py +121 -1
- agents/tracing/span_data.py +96 -0
- agents/tracing/util.py +5 -0
- agents/voice/__init__.py +51 -0
- agents/voice/events.py +47 -0
- agents/voice/exceptions.py +8 -0
- agents/voice/imports.py +11 -0
- agents/voice/input.py +88 -0
- agents/voice/model.py +193 -0
- agents/voice/models/__init__.py +0 -0
- agents/voice/models/openai_model_provider.py +97 -0
- agents/voice/models/openai_stt.py +457 -0
- agents/voice/models/openai_tts.py +54 -0
- agents/voice/pipeline.py +151 -0
- agents/voice/pipeline_config.py +46 -0
- agents/voice/result.py +287 -0
- agents/voice/utils.py +37 -0
- agents/voice/workflow.py +93 -0
- {openai_agents-0.0.5.dist-info → openai_agents-0.0.6.dist-info}/METADATA +6 -1
- {openai_agents-0.0.5.dist-info → openai_agents-0.0.6.dist-info}/RECORD +25 -10
- {openai_agents-0.0.5.dist-info → openai_agents-0.0.6.dist-info}/WHEEL +0 -0
- {openai_agents-0.0.5.dist-info → openai_agents-0.0.6.dist-info}/licenses/LICENSE +0 -0
agents/__init__.py
CHANGED
|
@@ -73,8 +73,11 @@ from .tracing import (
|
|
|
73
73
|
Span,
|
|
74
74
|
SpanData,
|
|
75
75
|
SpanError,
|
|
76
|
+
SpeechGroupSpanData,
|
|
77
|
+
SpeechSpanData,
|
|
76
78
|
Trace,
|
|
77
79
|
TracingProcessor,
|
|
80
|
+
TranscriptionSpanData,
|
|
78
81
|
add_trace_processor,
|
|
79
82
|
agent_span,
|
|
80
83
|
custom_span,
|
|
@@ -89,7 +92,10 @@ from .tracing import (
|
|
|
89
92
|
set_trace_processors,
|
|
90
93
|
set_tracing_disabled,
|
|
91
94
|
set_tracing_export_api_key,
|
|
95
|
+
speech_group_span,
|
|
96
|
+
speech_span,
|
|
92
97
|
trace,
|
|
98
|
+
transcription_span,
|
|
93
99
|
)
|
|
94
100
|
from .usage import Usage
|
|
95
101
|
|
|
@@ -211,6 +217,9 @@ __all__ = [
|
|
|
211
217
|
"handoff_span",
|
|
212
218
|
"set_trace_processors",
|
|
213
219
|
"set_tracing_disabled",
|
|
220
|
+
"speech_group_span",
|
|
221
|
+
"transcription_span",
|
|
222
|
+
"speech_span",
|
|
214
223
|
"trace",
|
|
215
224
|
"Trace",
|
|
216
225
|
"TracingProcessor",
|
|
@@ -223,6 +232,9 @@ __all__ = [
|
|
|
223
232
|
"GenerationSpanData",
|
|
224
233
|
"GuardrailSpanData",
|
|
225
234
|
"HandoffSpanData",
|
|
235
|
+
"SpeechGroupSpanData",
|
|
236
|
+
"SpeechSpanData",
|
|
237
|
+
"TranscriptionSpanData",
|
|
226
238
|
"set_default_openai_key",
|
|
227
239
|
"set_default_openai_client",
|
|
228
240
|
"set_default_openai_api",
|
agents/models/openai_provider.py
CHANGED
|
@@ -34,6 +34,19 @@ class OpenAIProvider(ModelProvider):
|
|
|
34
34
|
project: str | None = None,
|
|
35
35
|
use_responses: bool | None = None,
|
|
36
36
|
) -> None:
|
|
37
|
+
"""Create a new OpenAI provider.
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
api_key: The API key to use for the OpenAI client. If not provided, we will use the
|
|
41
|
+
default API key.
|
|
42
|
+
base_url: The base URL to use for the OpenAI client. If not provided, we will use the
|
|
43
|
+
default base URL.
|
|
44
|
+
openai_client: An optional OpenAI client to use. If not provided, we will create a new
|
|
45
|
+
OpenAI client using the api_key and base_url.
|
|
46
|
+
organization: The organization to use for the OpenAI client.
|
|
47
|
+
project: The project to use for the OpenAI client.
|
|
48
|
+
use_responses: Whether to use the OpenAI responses API.
|
|
49
|
+
"""
|
|
37
50
|
if openai_client is not None:
|
|
38
51
|
assert api_key is None and base_url is None, (
|
|
39
52
|
"Don't provide api_key or base_url if you provide openai_client"
|
agents/tracing/__init__.py
CHANGED
|
@@ -10,7 +10,10 @@ from .create import (
|
|
|
10
10
|
guardrail_span,
|
|
11
11
|
handoff_span,
|
|
12
12
|
response_span,
|
|
13
|
+
speech_group_span,
|
|
14
|
+
speech_span,
|
|
13
15
|
trace,
|
|
16
|
+
transcription_span,
|
|
14
17
|
)
|
|
15
18
|
from .processor_interface import TracingProcessor
|
|
16
19
|
from .processors import default_exporter, default_processor
|
|
@@ -24,6 +27,9 @@ from .span_data import (
|
|
|
24
27
|
HandoffSpanData,
|
|
25
28
|
ResponseSpanData,
|
|
26
29
|
SpanData,
|
|
30
|
+
SpeechGroupSpanData,
|
|
31
|
+
SpeechSpanData,
|
|
32
|
+
TranscriptionSpanData,
|
|
27
33
|
)
|
|
28
34
|
from .spans import Span, SpanError
|
|
29
35
|
from .traces import Trace
|
|
@@ -54,9 +60,15 @@ __all__ = [
|
|
|
54
60
|
"GuardrailSpanData",
|
|
55
61
|
"HandoffSpanData",
|
|
56
62
|
"ResponseSpanData",
|
|
63
|
+
"SpeechGroupSpanData",
|
|
64
|
+
"SpeechSpanData",
|
|
65
|
+
"TranscriptionSpanData",
|
|
57
66
|
"TracingProcessor",
|
|
58
67
|
"gen_trace_id",
|
|
59
68
|
"gen_span_id",
|
|
69
|
+
"speech_group_span",
|
|
70
|
+
"speech_span",
|
|
71
|
+
"transcription_span",
|
|
60
72
|
]
|
|
61
73
|
|
|
62
74
|
|
agents/tracing/create.py
CHANGED
|
@@ -13,6 +13,9 @@ from .span_data import (
|
|
|
13
13
|
GuardrailSpanData,
|
|
14
14
|
HandoffSpanData,
|
|
15
15
|
ResponseSpanData,
|
|
16
|
+
SpeechGroupSpanData,
|
|
17
|
+
SpeechSpanData,
|
|
18
|
+
TranscriptionSpanData,
|
|
16
19
|
)
|
|
17
20
|
from .spans import Span
|
|
18
21
|
from .traces import Trace
|
|
@@ -181,7 +184,11 @@ def generation_span(
|
|
|
181
184
|
"""
|
|
182
185
|
return GLOBAL_TRACE_PROVIDER.create_span(
|
|
183
186
|
span_data=GenerationSpanData(
|
|
184
|
-
input=input,
|
|
187
|
+
input=input,
|
|
188
|
+
output=output,
|
|
189
|
+
model=model,
|
|
190
|
+
model_config=model_config,
|
|
191
|
+
usage=usage,
|
|
185
192
|
),
|
|
186
193
|
span_id=span_id,
|
|
187
194
|
parent=parent,
|
|
@@ -304,3 +311,116 @@ def guardrail_span(
|
|
|
304
311
|
parent=parent,
|
|
305
312
|
disabled=disabled,
|
|
306
313
|
)
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def transcription_span(
|
|
317
|
+
model: str | None = None,
|
|
318
|
+
input: str | None = None,
|
|
319
|
+
input_format: str | None = "pcm",
|
|
320
|
+
output: str | None = None,
|
|
321
|
+
model_config: Mapping[str, Any] | None = None,
|
|
322
|
+
span_id: str | None = None,
|
|
323
|
+
parent: Trace | Span[Any] | None = None,
|
|
324
|
+
disabled: bool = False,
|
|
325
|
+
) -> Span[TranscriptionSpanData]:
|
|
326
|
+
"""Create a new transcription span. The span will not be started automatically, you should
|
|
327
|
+
either do `with transcription_span() ...` or call `span.start()` + `span.finish()` manually.
|
|
328
|
+
|
|
329
|
+
Args:
|
|
330
|
+
model: The name of the model used for the speech-to-text.
|
|
331
|
+
input: The audio input of the speech-to-text transcription, as a base64 encoded string of
|
|
332
|
+
audio bytes.
|
|
333
|
+
input_format: The format of the audio input (defaults to "pcm").
|
|
334
|
+
output: The output of the speech-to-text transcription.
|
|
335
|
+
model_config: The model configuration (hyperparameters) used.
|
|
336
|
+
span_id: The ID of the span. Optional. If not provided, we will generate an ID. We
|
|
337
|
+
recommend using `util.gen_span_id()` to generate a span ID, to guarantee that IDs are
|
|
338
|
+
correctly formatted.
|
|
339
|
+
parent: The parent span or trace. If not provided, we will automatically use the current
|
|
340
|
+
trace/span as the parent.
|
|
341
|
+
disabled: If True, we will return a Span but the Span will not be recorded.
|
|
342
|
+
|
|
343
|
+
Returns:
|
|
344
|
+
The newly created speech-to-text span.
|
|
345
|
+
"""
|
|
346
|
+
return GLOBAL_TRACE_PROVIDER.create_span(
|
|
347
|
+
span_data=TranscriptionSpanData(
|
|
348
|
+
input=input,
|
|
349
|
+
input_format=input_format,
|
|
350
|
+
output=output,
|
|
351
|
+
model=model,
|
|
352
|
+
model_config=model_config,
|
|
353
|
+
),
|
|
354
|
+
span_id=span_id,
|
|
355
|
+
parent=parent,
|
|
356
|
+
disabled=disabled,
|
|
357
|
+
)
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
def speech_span(
|
|
361
|
+
model: str | None = None,
|
|
362
|
+
input: str | None = None,
|
|
363
|
+
output: str | None = None,
|
|
364
|
+
output_format: str | None = "pcm",
|
|
365
|
+
model_config: Mapping[str, Any] | None = None,
|
|
366
|
+
first_content_at: str | None = None,
|
|
367
|
+
span_id: str | None = None,
|
|
368
|
+
parent: Trace | Span[Any] | None = None,
|
|
369
|
+
disabled: bool = False,
|
|
370
|
+
) -> Span[SpeechSpanData]:
|
|
371
|
+
"""Create a new speech span. The span will not be started automatically, you should either do
|
|
372
|
+
`with speech_span() ...` or call `span.start()` + `span.finish()` manually.
|
|
373
|
+
|
|
374
|
+
Args:
|
|
375
|
+
model: The name of the model used for the text-to-speech.
|
|
376
|
+
input: The text input of the text-to-speech.
|
|
377
|
+
output: The audio output of the text-to-speech as base64 encoded string of PCM audio bytes.
|
|
378
|
+
output_format: The format of the audio output (defaults to "pcm").
|
|
379
|
+
model_config: The model configuration (hyperparameters) used.
|
|
380
|
+
first_content_at: The time of the first byte of the audio output.
|
|
381
|
+
span_id: The ID of the span. Optional. If not provided, we will generate an ID. We
|
|
382
|
+
recommend using `util.gen_span_id()` to generate a span ID, to guarantee that IDs are
|
|
383
|
+
correctly formatted.
|
|
384
|
+
parent: The parent span or trace. If not provided, we will automatically use the current
|
|
385
|
+
trace/span as the parent.
|
|
386
|
+
disabled: If True, we will return a Span but the Span will not be recorded.
|
|
387
|
+
"""
|
|
388
|
+
return GLOBAL_TRACE_PROVIDER.create_span(
|
|
389
|
+
span_data=SpeechSpanData(
|
|
390
|
+
model=model,
|
|
391
|
+
input=input,
|
|
392
|
+
output=output,
|
|
393
|
+
output_format=output_format,
|
|
394
|
+
model_config=model_config,
|
|
395
|
+
first_content_at=first_content_at,
|
|
396
|
+
),
|
|
397
|
+
span_id=span_id,
|
|
398
|
+
parent=parent,
|
|
399
|
+
disabled=disabled,
|
|
400
|
+
)
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
def speech_group_span(
|
|
404
|
+
input: str | None = None,
|
|
405
|
+
span_id: str | None = None,
|
|
406
|
+
parent: Trace | Span[Any] | None = None,
|
|
407
|
+
disabled: bool = False,
|
|
408
|
+
) -> Span[SpeechGroupSpanData]:
|
|
409
|
+
"""Create a new speech group span. The span will not be started automatically, you should
|
|
410
|
+
either do `with speech_group_span() ...` or call `span.start()` + `span.finish()` manually.
|
|
411
|
+
|
|
412
|
+
Args:
|
|
413
|
+
input: The input text used for the speech request.
|
|
414
|
+
span_id: The ID of the span. Optional. If not provided, we will generate an ID. We
|
|
415
|
+
recommend using `util.gen_span_id()` to generate a span ID, to guarantee that IDs are
|
|
416
|
+
correctly formatted.
|
|
417
|
+
parent: The parent span or trace. If not provided, we will automatically use the current
|
|
418
|
+
trace/span as the parent.
|
|
419
|
+
disabled: If True, we will return a Span but the Span will not be recorded.
|
|
420
|
+
"""
|
|
421
|
+
return GLOBAL_TRACE_PROVIDER.create_span(
|
|
422
|
+
span_data=SpeechGroupSpanData(input=input),
|
|
423
|
+
span_id=span_id,
|
|
424
|
+
parent=parent,
|
|
425
|
+
disabled=disabled,
|
|
426
|
+
)
|
agents/tracing/span_data.py
CHANGED
|
@@ -186,3 +186,99 @@ class GuardrailSpanData(SpanData):
|
|
|
186
186
|
"name": self.name,
|
|
187
187
|
"triggered": self.triggered,
|
|
188
188
|
}
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
class TranscriptionSpanData(SpanData):
|
|
192
|
+
__slots__ = (
|
|
193
|
+
"input",
|
|
194
|
+
"output",
|
|
195
|
+
"model",
|
|
196
|
+
"model_config",
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
def __init__(
|
|
200
|
+
self,
|
|
201
|
+
input: str | None = None,
|
|
202
|
+
input_format: str | None = "pcm",
|
|
203
|
+
output: str | None = None,
|
|
204
|
+
model: str | None = None,
|
|
205
|
+
model_config: Mapping[str, Any] | None = None,
|
|
206
|
+
):
|
|
207
|
+
self.input = input
|
|
208
|
+
self.input_format = input_format
|
|
209
|
+
self.output = output
|
|
210
|
+
self.model = model
|
|
211
|
+
self.model_config = model_config
|
|
212
|
+
|
|
213
|
+
@property
|
|
214
|
+
def type(self) -> str:
|
|
215
|
+
return "transcription"
|
|
216
|
+
|
|
217
|
+
def export(self) -> dict[str, Any]:
|
|
218
|
+
return {
|
|
219
|
+
"type": self.type,
|
|
220
|
+
"input": {
|
|
221
|
+
"data": self.input or "",
|
|
222
|
+
"format": self.input_format,
|
|
223
|
+
},
|
|
224
|
+
"output": self.output,
|
|
225
|
+
"model": self.model,
|
|
226
|
+
"model_config": self.model_config,
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
class SpeechSpanData(SpanData):
|
|
231
|
+
__slots__ = ("input", "output", "model", "model_config", "first_byte_at")
|
|
232
|
+
|
|
233
|
+
def __init__(
|
|
234
|
+
self,
|
|
235
|
+
input: str | None = None,
|
|
236
|
+
output: str | None = None,
|
|
237
|
+
output_format: str | None = "pcm",
|
|
238
|
+
model: str | None = None,
|
|
239
|
+
model_config: Mapping[str, Any] | None = None,
|
|
240
|
+
first_content_at: str | None = None,
|
|
241
|
+
):
|
|
242
|
+
self.input = input
|
|
243
|
+
self.output = output
|
|
244
|
+
self.output_format = output_format
|
|
245
|
+
self.model = model
|
|
246
|
+
self.model_config = model_config
|
|
247
|
+
self.first_content_at = first_content_at
|
|
248
|
+
|
|
249
|
+
@property
|
|
250
|
+
def type(self) -> str:
|
|
251
|
+
return "speech"
|
|
252
|
+
|
|
253
|
+
def export(self) -> dict[str, Any]:
|
|
254
|
+
return {
|
|
255
|
+
"type": self.type,
|
|
256
|
+
"input": self.input,
|
|
257
|
+
"output": {
|
|
258
|
+
"data": self.output or "",
|
|
259
|
+
"format": self.output_format,
|
|
260
|
+
},
|
|
261
|
+
"model": self.model,
|
|
262
|
+
"model_config": self.model_config,
|
|
263
|
+
"first_content_at": self.first_content_at,
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
class SpeechGroupSpanData(SpanData):
|
|
268
|
+
__slots__ = "input"
|
|
269
|
+
|
|
270
|
+
def __init__(
|
|
271
|
+
self,
|
|
272
|
+
input: str | None = None,
|
|
273
|
+
):
|
|
274
|
+
self.input = input
|
|
275
|
+
|
|
276
|
+
@property
|
|
277
|
+
def type(self) -> str:
|
|
278
|
+
return "speech-group"
|
|
279
|
+
|
|
280
|
+
def export(self) -> dict[str, Any]:
|
|
281
|
+
return {
|
|
282
|
+
"type": self.type,
|
|
283
|
+
"input": self.input,
|
|
284
|
+
}
|
agents/tracing/util.py
CHANGED
agents/voice/__init__.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
from .events import VoiceStreamEvent, VoiceStreamEventAudio, VoiceStreamEventLifecycle
|
|
2
|
+
from .exceptions import STTWebsocketConnectionError
|
|
3
|
+
from .input import AudioInput, StreamedAudioInput
|
|
4
|
+
from .model import (
|
|
5
|
+
StreamedTranscriptionSession,
|
|
6
|
+
STTModel,
|
|
7
|
+
STTModelSettings,
|
|
8
|
+
TTSModel,
|
|
9
|
+
TTSModelSettings,
|
|
10
|
+
VoiceModelProvider,
|
|
11
|
+
)
|
|
12
|
+
from .models.openai_model_provider import OpenAIVoiceModelProvider
|
|
13
|
+
from .models.openai_stt import OpenAISTTModel, OpenAISTTTranscriptionSession
|
|
14
|
+
from .models.openai_tts import OpenAITTSModel
|
|
15
|
+
from .pipeline import VoicePipeline
|
|
16
|
+
from .pipeline_config import VoicePipelineConfig
|
|
17
|
+
from .result import StreamedAudioResult
|
|
18
|
+
from .utils import get_sentence_based_splitter
|
|
19
|
+
from .workflow import (
|
|
20
|
+
SingleAgentVoiceWorkflow,
|
|
21
|
+
SingleAgentWorkflowCallbacks,
|
|
22
|
+
VoiceWorkflowBase,
|
|
23
|
+
VoiceWorkflowHelper,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
__all__ = [
|
|
27
|
+
"AudioInput",
|
|
28
|
+
"StreamedAudioInput",
|
|
29
|
+
"STTModel",
|
|
30
|
+
"STTModelSettings",
|
|
31
|
+
"TTSModel",
|
|
32
|
+
"TTSModelSettings",
|
|
33
|
+
"VoiceModelProvider",
|
|
34
|
+
"StreamedAudioResult",
|
|
35
|
+
"SingleAgentVoiceWorkflow",
|
|
36
|
+
"OpenAIVoiceModelProvider",
|
|
37
|
+
"OpenAISTTModel",
|
|
38
|
+
"OpenAITTSModel",
|
|
39
|
+
"VoiceStreamEventAudio",
|
|
40
|
+
"VoiceStreamEventLifecycle",
|
|
41
|
+
"VoiceStreamEvent",
|
|
42
|
+
"VoicePipeline",
|
|
43
|
+
"VoicePipelineConfig",
|
|
44
|
+
"get_sentence_based_splitter",
|
|
45
|
+
"VoiceWorkflowHelper",
|
|
46
|
+
"VoiceWorkflowBase",
|
|
47
|
+
"SingleAgentWorkflowCallbacks",
|
|
48
|
+
"StreamedTranscriptionSession",
|
|
49
|
+
"OpenAISTTTranscriptionSession",
|
|
50
|
+
"STTWebsocketConnectionError",
|
|
51
|
+
]
|
agents/voice/events.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import Literal, Union
|
|
5
|
+
|
|
6
|
+
from typing_extensions import TypeAlias
|
|
7
|
+
|
|
8
|
+
from .imports import np, npt
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass
|
|
12
|
+
class VoiceStreamEventAudio:
|
|
13
|
+
"""Streaming event from the VoicePipeline"""
|
|
14
|
+
|
|
15
|
+
data: npt.NDArray[np.int16 | np.float32] | None
|
|
16
|
+
"""The audio data."""
|
|
17
|
+
|
|
18
|
+
type: Literal["voice_stream_event_audio"] = "voice_stream_event_audio"
|
|
19
|
+
"""The type of event."""
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass
|
|
23
|
+
class VoiceStreamEventLifecycle:
|
|
24
|
+
"""Streaming event from the VoicePipeline"""
|
|
25
|
+
|
|
26
|
+
event: Literal["turn_started", "turn_ended", "session_ended"]
|
|
27
|
+
"""The event that occurred."""
|
|
28
|
+
|
|
29
|
+
type: Literal["voice_stream_event_lifecycle"] = "voice_stream_event_lifecycle"
|
|
30
|
+
"""The type of event."""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass
|
|
34
|
+
class VoiceStreamEventError:
|
|
35
|
+
"""Streaming event from the VoicePipeline"""
|
|
36
|
+
|
|
37
|
+
error: Exception
|
|
38
|
+
"""The error that occurred."""
|
|
39
|
+
|
|
40
|
+
type: Literal["voice_stream_event_error"] = "voice_stream_event_error"
|
|
41
|
+
"""The type of event."""
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
VoiceStreamEvent: TypeAlias = Union[
|
|
45
|
+
VoiceStreamEventAudio, VoiceStreamEventLifecycle, VoiceStreamEventError
|
|
46
|
+
]
|
|
47
|
+
"""An event from the `VoicePipeline`, streamed via `StreamedAudioResult.stream()`."""
|
agents/voice/imports.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
try:
|
|
2
|
+
import numpy as np
|
|
3
|
+
import numpy.typing as npt
|
|
4
|
+
import websockets
|
|
5
|
+
except ImportError as _e:
|
|
6
|
+
raise ImportError(
|
|
7
|
+
"`numpy` + `websockets` are required to use voice. You can install them via the optional "
|
|
8
|
+
"dependency group: `pip install openai-agents[voice]`."
|
|
9
|
+
) from _e
|
|
10
|
+
|
|
11
|
+
__all__ = ["np", "npt", "websockets"]
|
agents/voice/input.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import base64
|
|
5
|
+
import io
|
|
6
|
+
import wave
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
|
|
9
|
+
from ..exceptions import UserError
|
|
10
|
+
from .imports import np, npt
|
|
11
|
+
|
|
12
|
+
DEFAULT_SAMPLE_RATE = 24000
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _buffer_to_audio_file(
|
|
16
|
+
buffer: npt.NDArray[np.int16 | np.float32],
|
|
17
|
+
frame_rate: int = DEFAULT_SAMPLE_RATE,
|
|
18
|
+
sample_width: int = 2,
|
|
19
|
+
channels: int = 1,
|
|
20
|
+
) -> tuple[str, io.BytesIO, str]:
|
|
21
|
+
if buffer.dtype == np.float32:
|
|
22
|
+
# convert to int16
|
|
23
|
+
buffer = np.clip(buffer, -1.0, 1.0)
|
|
24
|
+
buffer = (buffer * 32767).astype(np.int16)
|
|
25
|
+
elif buffer.dtype != np.int16:
|
|
26
|
+
raise UserError("Buffer must be a numpy array of int16 or float32")
|
|
27
|
+
|
|
28
|
+
audio_file = io.BytesIO()
|
|
29
|
+
with wave.open(audio_file, "w") as wav_file:
|
|
30
|
+
wav_file.setnchannels(channels)
|
|
31
|
+
wav_file.setsampwidth(sample_width)
|
|
32
|
+
wav_file.setframerate(frame_rate)
|
|
33
|
+
wav_file.writeframes(buffer.tobytes())
|
|
34
|
+
audio_file.seek(0)
|
|
35
|
+
|
|
36
|
+
# (filename, bytes, content_type)
|
|
37
|
+
return ("audio.wav", audio_file, "audio/wav")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass
|
|
41
|
+
class AudioInput:
|
|
42
|
+
"""Static audio to be used as input for the VoicePipeline."""
|
|
43
|
+
|
|
44
|
+
buffer: npt.NDArray[np.int16 | np.float32]
|
|
45
|
+
"""
|
|
46
|
+
A buffer containing the audio data for the agent. Must be a numpy array of int16 or float32.
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
frame_rate: int = DEFAULT_SAMPLE_RATE
|
|
50
|
+
"""The sample rate of the audio data. Defaults to 24000."""
|
|
51
|
+
|
|
52
|
+
sample_width: int = 2
|
|
53
|
+
"""The sample width of the audio data. Defaults to 2."""
|
|
54
|
+
|
|
55
|
+
channels: int = 1
|
|
56
|
+
"""The number of channels in the audio data. Defaults to 1."""
|
|
57
|
+
|
|
58
|
+
def to_audio_file(self) -> tuple[str, io.BytesIO, str]:
|
|
59
|
+
"""Returns a tuple of (filename, bytes, content_type)"""
|
|
60
|
+
return _buffer_to_audio_file(self.buffer, self.frame_rate, self.sample_width, self.channels)
|
|
61
|
+
|
|
62
|
+
def to_base64(self) -> str:
|
|
63
|
+
"""Returns the audio data as a base64 encoded string."""
|
|
64
|
+
if self.buffer.dtype == np.float32:
|
|
65
|
+
# convert to int16
|
|
66
|
+
self.buffer = np.clip(self.buffer, -1.0, 1.0)
|
|
67
|
+
self.buffer = (self.buffer * 32767).astype(np.int16)
|
|
68
|
+
elif self.buffer.dtype != np.int16:
|
|
69
|
+
raise UserError("Buffer must be a numpy array of int16 or float32")
|
|
70
|
+
|
|
71
|
+
return base64.b64encode(self.buffer.tobytes()).decode("utf-8")
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class StreamedAudioInput:
|
|
75
|
+
"""Audio input represented as a stream of audio data. You can pass this to the `VoicePipeline`
|
|
76
|
+
and then push audio data into the queue using the `add_audio` method.
|
|
77
|
+
"""
|
|
78
|
+
|
|
79
|
+
def __init__(self):
|
|
80
|
+
self.queue: asyncio.Queue[npt.NDArray[np.int16 | np.float32]] = asyncio.Queue()
|
|
81
|
+
|
|
82
|
+
async def add_audio(self, audio: npt.NDArray[np.int16 | np.float32]):
|
|
83
|
+
"""Adds more audio data to the stream.
|
|
84
|
+
|
|
85
|
+
Args:
|
|
86
|
+
audio: The audio data to add. Must be a numpy array of int16 or float32.
|
|
87
|
+
"""
|
|
88
|
+
await self.queue.put(audio)
|