|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import inspect |
| 4 | +from typing import ( |
| 5 | + Any, |
| 6 | + Callable, |
| 7 | + Literal, |
| 8 | + Union, |
| 9 | +) |
| 10 | + |
| 11 | +from typing_extensions import NotRequired, TypeAlias, TypedDict |
| 12 | + |
| 13 | +from ..model_settings import ToolChoice |
| 14 | +from ..tool import FunctionTool |
| 15 | +from ..util._types import MaybeAwaitable |
| 16 | + |
| 17 | + |
| 18 | +class RealtimeClientMessage(TypedDict): |
| 19 | + type: str # explicitly required |
| 20 | + other_data: NotRequired[dict[str, Any]] |
| 21 | + |
| 22 | + |
| 23 | +class UserInputText(TypedDict): |
| 24 | + type: Literal["input_text"] |
| 25 | + text: str |
| 26 | + |
| 27 | + |
| 28 | +class RealtimeUserInputMessage(TypedDict): |
| 29 | + type: Literal["message"] |
| 30 | + role: Literal["user"] |
| 31 | + content: list[UserInputText] |
| 32 | + |
| 33 | + |
| 34 | +RealtimeUserInput: TypeAlias = Union[str, RealtimeUserInputMessage] |
| 35 | + |
| 36 | + |
| 37 | +RealtimeAudioFormat: TypeAlias = Union[Literal["pcm16", "g711_ulaw", "g711_alaw"], str] |
| 38 | + |
| 39 | + |
| 40 | +class RealtimeInputAudioTranscriptionConfig(TypedDict): |
| 41 | + language: NotRequired[str] |
| 42 | + model: NotRequired[Literal["gpt-4o-transcribe", "gpt-4o-mini-transcribe", "whisper-1"] | str] |
| 43 | + prompt: NotRequired[str] |
| 44 | + |
| 45 | + |
| 46 | +class RealtimeTurnDetectionConfig(TypedDict): |
| 47 | + """Turn detection config. Allows extra vendor keys if needed.""" |
| 48 | + |
| 49 | + type: NotRequired[Literal["semantic_vad", "server_vad"]] |
| 50 | + create_response: NotRequired[bool] |
| 51 | + eagerness: NotRequired[Literal["auto", "low", "medium", "high"]] |
| 52 | + interrupt_response: NotRequired[bool] |
| 53 | + prefix_padding_ms: NotRequired[int] |
| 54 | + silence_duration_ms: NotRequired[int] |
| 55 | + threshold: NotRequired[float] |
| 56 | + |
| 57 | + |
| 58 | +class RealtimeSessionConfig(TypedDict): |
| 59 | + api_key: NotRequired[APIKeyOrKeyFunc] |
| 60 | + model: NotRequired[str] |
| 61 | + instructions: NotRequired[str] |
| 62 | + modalities: NotRequired[list[Literal["text", "audio"]]] |
| 63 | + voice: NotRequired[str] |
| 64 | + |
| 65 | + input_audio_format: NotRequired[RealtimeAudioFormat] |
| 66 | + output_audio_format: NotRequired[RealtimeAudioFormat] |
| 67 | + input_audio_transcription: NotRequired[RealtimeInputAudioTranscriptionConfig] |
| 68 | + turn_detection: NotRequired[RealtimeTurnDetectionConfig] |
| 69 | + |
| 70 | + tool_choice: NotRequired[ToolChoice] |
| 71 | + tools: NotRequired[list[FunctionTool]] |
| 72 | + |
| 73 | + |
| 74 | +APIKeyOrKeyFunc = str | Callable[[], MaybeAwaitable[str]] |
| 75 | +"""Either an API key or a function that returns an API key.""" |
| 76 | + |
| 77 | + |
| 78 | +async def get_api_key(key: APIKeyOrKeyFunc | None) -> str | None: |
| 79 | + """Get the API key from the key or key function.""" |
| 80 | + if key is None: |
| 81 | + return None |
| 82 | + elif isinstance(key, str): |
| 83 | + return key |
| 84 | + |
| 85 | + result = key() |
| 86 | + if inspect.isawaitable(result): |
| 87 | + return await result |
| 88 | + return result |
| 89 | + |
| 90 | + # TODO (rm) Add tracing support |
| 91 | + # tracing: NotRequired[RealtimeTracingConfig | None] |
0 commit comments