Module livekit.plugins.openai
Sub-modules
livekit.plugins.openai.beta
livekit.plugins.openai.realtime
Functions
async def create_embeddings(*, input: list[str], model: models.EmbeddingModels = 'text-embedding-3-small', dimensions: int | None = None, api_key: str | None = None, http_session: aiohttp.ClientSession | None = None) ‑> list[livekit.plugins.openai.embeddings.EmbeddingData]
Classes
class EmbeddingData (index: int, embedding: list[float])
-
EmbeddingData(index: 'int', embedding: 'list[float]')
Expand source code
@dataclass class EmbeddingData: index: int embedding: list[float]
Class variables
var embedding : list[float]
var index : int
class LLM (*, model: str | ChatModels = 'gpt-4o', api_key: str | None = None, base_url: str | None = None, user: str | None = None, client: openai.AsyncClient | None = None, temperature: float | None = None)
-
Helper class that provides a standard way to create an ABC using inheritance.
Create a new instance of OpenAI LLM.
api_key
must be set to your OpenAI API key, either using the argument or by setting theOPENAI_API_KEY
environmental variable.Expand source code
class LLM(llm.LLM): def __init__( self, *, model: str | ChatModels = "gpt-4o", api_key: str | None = None, base_url: str | None = None, user: str | None = None, client: openai.AsyncClient | None = None, temperature: float | None = None, ) -> None: """ Create a new instance of OpenAI LLM. ``api_key`` must be set to your OpenAI API key, either using the argument or by setting the ``OPENAI_API_KEY`` environmental variable. """ super().__init__() self._opts = LLMOptions(model=model, user=user, temperature=temperature) self._client = client or openai.AsyncClient( api_key=api_key, base_url=base_url, http_client=httpx.AsyncClient( timeout=httpx.Timeout(connect=15.0, read=5.0, write=5.0, pool=5.0), follow_redirects=True, limits=httpx.Limits( max_connections=50, max_keepalive_connections=50, keepalive_expiry=120, ), ), ) self._running_fncs: MutableSet[asyncio.Task[Any]] = set() @staticmethod def with_azure( *, model: str | ChatModels = "gpt-4o", azure_endpoint: str | None = None, azure_deployment: str | None = None, api_version: str | None = None, api_key: str | None = None, azure_ad_token: str | None = None, azure_ad_token_provider: AsyncAzureADTokenProvider | None = None, organization: str | None = None, project: str | None = None, base_url: str | None = None, user: str | None = None, temperature: float | None = None, ) -> LLM: """ This automatically infers the following arguments from their corresponding environment variables if they are not provided: - `api_key` from `AZURE_OPENAI_API_KEY` - `organization` from `OPENAI_ORG_ID` - `project` from `OPENAI_PROJECT_ID` - `azure_ad_token` from `AZURE_OPENAI_AD_TOKEN` - `api_version` from `OPENAI_API_VERSION` - `azure_endpoint` from `AZURE_OPENAI_ENDPOINT` """ azure_client = openai.AsyncAzureOpenAI( azure_endpoint=azure_endpoint, azure_deployment=azure_deployment, api_version=api_version, api_key=api_key, azure_ad_token=azure_ad_token, azure_ad_token_provider=azure_ad_token_provider, organization=organization, project=project, base_url=base_url, ) # type: ignore return LLM(model=model, client=azure_client, user=user, temperature=temperature) @staticmethod def with_cerebras( *, model: str | CerebrasChatModels = "llama3.1-8b", api_key: str | None = None, base_url: str | None = "https://api.cerebras.ai/v1", client: openai.AsyncClient | None = None, user: str | None = None, temperature: float | None = None, ) -> LLM: """ Create a new instance of Cerebras LLM. ``api_key`` must be set to your Cerebras API key, either using the argument or by setting the ``CEREBRAS_API_KEY`` environmental variable. """ api_key = api_key or os.environ.get("CEREBRAS_API_KEY") if api_key is None: raise ValueError( "Cerebras API key is required, either as argument or set CEREBAAS_API_KEY environmental variable" ) return LLM( model=model, api_key=api_key, base_url=base_url, client=client, user=user, temperature=temperature, ) @staticmethod def with_fireworks( *, model: str = "accounts/fireworks/models/llama-v3p1-70b-instruct", api_key: str | None = None, base_url: str | None = "https://api.fireworks.ai/inference/v1", client: openai.AsyncClient | None = None, user: str | None = None, temperature: float | None = None, ) -> LLM: """ Create a new instance of Fireworks LLM. ``api_key`` must be set to your Fireworks API key, either using the argument or by setting the ``FIREWORKS_API_KEY`` environmental variable. """ api_key = api_key or os.environ.get("FIREWORKS_API_KEY") if api_key is None: raise ValueError( "Fireworks API key is required, either as argument or set FIREWORKS_API_KEY environmental variable" ) return LLM( model=model, api_key=api_key, base_url=base_url, client=client, user=user, temperature=temperature, ) @staticmethod def with_x_ai( *, model: str | XAIChatModels = "grok-2-public", api_key: str | None = None, base_url: str | None = "https://api.x.ai/v1", client: openai.AsyncClient | None = None, user: str | None = None, temperature: float | None = None, ): """ Create a new instance of XAI LLM. ``api_key`` must be set to your XAI API key, either using the argument or by setting the ``XAI_API_KEY`` environmental variable. """ api_key = api_key or os.environ.get("XAI_API_KEY") if api_key is None: raise ValueError( "XAI API key is required, either as argument or set XAI_API_KEY environmental variable" ) return LLM( model=model, api_key=api_key, base_url=base_url, client=client, user=user, temperature=temperature, ) @staticmethod def with_groq( *, model: str | GroqChatModels = "llama3-8b-8192", api_key: str | None = None, base_url: str | None = "https://api.groq.com/openai/v1", client: openai.AsyncClient | None = None, user: str | None = None, temperature: float | None = None, ) -> LLM: """ Create a new instance of Groq LLM. ``api_key`` must be set to your Groq API key, either using the argument or by setting the ``GROQ_API_KEY`` environmental variable. """ api_key = api_key or os.environ.get("GROQ_API_KEY") if api_key is None: raise ValueError( "Groq API key is required, either as argument or set GROQ_API_KEY environmental variable" ) return LLM( model=model, api_key=api_key, base_url=base_url, client=client, user=user, temperature=temperature, ) @staticmethod def with_deepseek( *, model: str | DeepSeekChatModels = "deepseek-chat", api_key: str | None = None, base_url: str | None = "https://api.deepseek.com/v1", client: openai.AsyncClient | None = None, user: str | None = None, temperature: float | None = None, ) -> LLM: """ Create a new instance of DeepSeek LLM. ``api_key`` must be set to your DeepSeek API key, either using the argument or by setting the ``DEEPSEEK_API_KEY`` environmental variable. """ api_key = api_key or os.environ.get("DEEPSEEK_API_KEY") if api_key is None: raise ValueError( "DeepSeek API key is required, either as argument or set DEEPSEEK_API_KEY environmental variable" ) return LLM( model=model, api_key=api_key, base_url=base_url, client=client, user=user, temperature=temperature, ) @staticmethod def with_octo( *, model: str | OctoChatModels = "llama-2-13b-chat", api_key: str | None = None, base_url: str | None = "https://text.octoai.run/v1", client: openai.AsyncClient | None = None, user: str | None = None, temperature: float | None = None, ) -> LLM: """ Create a new instance of OctoAI LLM. ``api_key`` must be set to your OctoAI API key, either using the argument or by setting the ``OCTOAI_TOKEN`` environmental variable. """ api_key = api_key or os.environ.get("OCTOAI_TOKEN") if api_key is None: raise ValueError( "OctoAI API key is required, either as argument or set OCTOAI_TOKEN environmental variable" ) return LLM( model=model, api_key=api_key, base_url=base_url, client=client, user=user, temperature=temperature, ) @staticmethod def with_ollama( *, model: str = "llama3.1", base_url: str | None = "http://localhost:11434/v1", client: openai.AsyncClient | None = None, temperature: float | None = None, ) -> LLM: """ Create a new instance of Ollama LLM. """ return LLM( model=model, api_key="ollama", base_url=base_url, client=client, temperature=temperature, ) @staticmethod def with_perplexity( *, model: str | PerplexityChatModels = "llama-3.1-sonar-small-128k-chat", api_key: str | None = None, base_url: str | None = "https://api.perplexity.ai", client: openai.AsyncClient | None = None, user: str | None = None, temperature: float | None = None, ) -> LLM: """ Create a new instance of PerplexityAI LLM. ``api_key`` must be set to your TogetherAI API key, either using the argument or by setting the ``PERPLEXITY_API_KEY`` environmental variable. """ api_key = api_key or os.environ.get("PERPLEXITY_API_KEY") if api_key is None: raise ValueError( "Perplexity AI API key is required, either as argument or set PERPLEXITY_API_KEY environmental variable" ) return LLM( model=model, api_key=api_key, base_url=base_url, client=client, user=user, temperature=temperature, ) @staticmethod def with_together( *, model: str | TogetherChatModels = "meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo", api_key: str | None = None, base_url: str | None = "https://api.together.xyz/v1", client: openai.AsyncClient | None = None, user: str | None = None, temperature: float | None = None, ) -> LLM: """ Create a new instance of TogetherAI LLM. ``api_key`` must be set to your TogetherAI API key, either using the argument or by setting the ``TOGETHER_API_KEY`` environmental variable. """ api_key = api_key or os.environ.get("TOGETHER_API_KEY") if api_key is None: raise ValueError( "Together AI API key is required, either as argument or set TOGETHER_API_KEY environmental variable" ) return LLM( model=model, api_key=api_key, base_url=base_url, client=client, user=user, temperature=temperature, ) @staticmethod def with_telnyx( *, model: str | TelnyxChatModels = "meta-llama/Meta-Llama-3.1-70B-Instruct", api_key: str | None = None, base_url: str | None = "https://api.telnyx.com/v2/ai", client: openai.AsyncClient | None = None, user: str | None = None, temperature: float | None = None, ) -> LLM: """ Create a new instance of Telnyx LLM. ``api_key`` must be set to your Telnyx API key, either using the argument or by setting the ``TELNYX_API_KEY`` environmental variable. """ api_key = api_key or os.environ.get("TELNYX_API_KEY") if api_key is None: raise ValueError( "Telnyx AI API key is required, either as argument or set TELNYX_API_KEY environmental variable" ) return LLM( model=model, api_key=api_key, base_url=base_url, client=client, user=user, temperature=temperature, ) @staticmethod def create_azure_client( *, model: str | ChatModels = "gpt-4o", azure_endpoint: str | None = None, azure_deployment: str | None = None, api_version: str | None = None, api_key: str | None = None, azure_ad_token: str | None = None, azure_ad_token_provider: AsyncAzureADTokenProvider | None = None, organization: str | None = None, project: str | None = None, base_url: str | None = None, user: str | None = None, temperature: float | None = None, ) -> LLM: logger.warning("This alias is deprecated. Use LLM.with_azure() instead") return LLM.with_azure( model=model, azure_endpoint=azure_endpoint, api_version=api_version, api_key=api_key, azure_ad_token=azure_ad_token, azure_ad_token_provider=azure_ad_token_provider, organization=organization, project=project, base_url=base_url, user=user, temperature=temperature, ) def chat( self, *, chat_ctx: llm.ChatContext, fnc_ctx: llm.FunctionContext | None = None, temperature: float | None = None, n: int | None = 1, parallel_tool_calls: bool | None = None, ) -> "LLMStream": opts: dict[str, Any] = dict() if fnc_ctx and len(fnc_ctx.ai_functions) > 0: fncs_desc = [] for fnc in fnc_ctx.ai_functions.values(): fncs_desc.append(llm._oai_api.build_oai_function_description(fnc)) opts["tools"] = fncs_desc if fnc_ctx and parallel_tool_calls is not None: opts["parallel_tool_calls"] = parallel_tool_calls user = self._opts.user or openai.NOT_GIVEN if temperature is None: temperature = self._opts.temperature messages = _build_oai_context(chat_ctx, id(self)) cmp = self._client.chat.completions.create( messages=messages, model=self._opts.model, n=n, temperature=temperature, stream_options={"include_usage": True}, stream=True, user=user, **opts, ) return LLMStream(self, oai_stream=cmp, chat_ctx=chat_ctx, fnc_ctx=fnc_ctx)
Ancestors
- LLM
- abc.ABC
- EventEmitter
- typing.Generic
Static methods
def create_azure_client(*, model: str | ChatModels = 'gpt-4o', azure_endpoint: str | None = None, azure_deployment: str | None = None, api_version: str | None = None, api_key: str | None = None, azure_ad_token: str | None = None, azure_ad_token_provider: AsyncAzureADTokenProvider | None = None, organization: str | None = None, project: str | None = None, base_url: str | None = None, user: str | None = None, temperature: float | None = None) ‑> livekit.plugins.openai.llm.LLM
def with_azure(*, model: str | ChatModels = 'gpt-4o', azure_endpoint: str | None = None, azure_deployment: str | None = None, api_version: str | None = None, api_key: str | None = None, azure_ad_token: str | None = None, azure_ad_token_provider: AsyncAzureADTokenProvider | None = None, organization: str | None = None, project: str | None = None, base_url: str | None = None, user: str | None = None, temperature: float | None = None) ‑> livekit.plugins.openai.llm.LLM
-
This automatically infers the following arguments from their corresponding environment variables if they are not provided: -
api_key
fromAZURE_OPENAI_API_KEY
-organization
fromOPENAI_ORG_ID
-project
fromOPENAI_PROJECT_ID
-azure_ad_token
fromAZURE_OPENAI_AD_TOKEN
-api_version
fromOPENAI_API_VERSION
-azure_endpoint
fromAZURE_OPENAI_ENDPOINT
def with_cerebras(*, model: str | CerebrasChatModels = 'llama3.1-8b', api_key: str | None = None, base_url: str | None = 'https://api.cerebras.ai/v1', client: openai.AsyncClient | None = None, user: str | None = None, temperature: float | None = None) ‑> livekit.plugins.openai.llm.LLM
-
Create a new instance of Cerebras LLM.
api_key
must be set to your Cerebras API key, either using the argument or by setting theCEREBRAS_API_KEY
environmental variable. def with_deepseek(*, model: str | DeepSeekChatModels = 'deepseek-chat', api_key: str | None = None, base_url: str | None = 'https://api.deepseek.com/v1', client: openai.AsyncClient | None = None, user: str | None = None, temperature: float | None = None) ‑> livekit.plugins.openai.llm.LLM
-
Create a new instance of DeepSeek LLM.
api_key
must be set to your DeepSeek API key, either using the argument or by setting theDEEPSEEK_API_KEY
environmental variable. def with_fireworks(*, model: str = 'accounts/fireworks/models/llama-v3p1-70b-instruct', api_key: str | None = None, base_url: str | None = 'https://api.fireworks.ai/inference/v1', client: openai.AsyncClient | None = None, user: str | None = None, temperature: float | None = None) ‑> livekit.plugins.openai.llm.LLM
-
Create a new instance of Fireworks LLM.
api_key
must be set to your Fireworks API key, either using the argument or by setting theFIREWORKS_API_KEY
environmental variable. def with_groq(*, model: str | GroqChatModels = 'llama3-8b-8192', api_key: str | None = None, base_url: str | None = 'https://api.groq.com/openai/v1', client: openai.AsyncClient | None = None, user: str | None = None, temperature: float | None = None) ‑> livekit.plugins.openai.llm.LLM
-
Create a new instance of Groq LLM.
api_key
must be set to your Groq API key, either using the argument or by setting theGROQ_API_KEY
environmental variable. def with_octo(*, model: str | OctoChatModels = 'llama-2-13b-chat', api_key: str | None = None, base_url: str | None = 'https://text.octoai.run/v1', client: openai.AsyncClient | None = None, user: str | None = None, temperature: float | None = None) ‑> livekit.plugins.openai.llm.LLM
-
Create a new instance of OctoAI LLM.
api_key
must be set to your OctoAI API key, either using the argument or by setting theOCTOAI_TOKEN
environmental variable. def with_ollama(*, model: str = 'llama3.1', base_url: str | None = 'http://localhost:11434/v1', client: openai.AsyncClient | None = None, temperature: float | None = None) ‑> livekit.plugins.openai.llm.LLM
-
Create a new instance of Ollama LLM.
def with_perplexity(*, model: str | PerplexityChatModels = 'llama-3.1-sonar-small-128k-chat', api_key: str | None = None, base_url: str | None = 'https://api.perplexity.ai', client: openai.AsyncClient | None = None, user: str | None = None, temperature: float | None = None) ‑> livekit.plugins.openai.llm.LLM
-
Create a new instance of PerplexityAI LLM.
api_key
must be set to your TogetherAI API key, either using the argument or by setting thePERPLEXITY_API_KEY
environmental variable. def with_telnyx(*, model: str | TelnyxChatModels = 'meta-llama/Meta-Llama-3.1-70B-Instruct', api_key: str | None = None, base_url: str | None = 'https://api.telnyx.com/v2/ai', client: openai.AsyncClient | None = None, user: str | None = None, temperature: float | None = None) ‑> livekit.plugins.openai.llm.LLM
-
Create a new instance of Telnyx LLM.
api_key
must be set to your Telnyx API key, either using the argument or by setting theTELNYX_API_KEY
environmental variable. def with_together(*, model: str | TogetherChatModels = 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo', api_key: str | None = None, base_url: str | None = 'https://api.together.xyz/v1', client: openai.AsyncClient | None = None, user: str | None = None, temperature: float | None = None) ‑> livekit.plugins.openai.llm.LLM
-
Create a new instance of TogetherAI LLM.
api_key
must be set to your TogetherAI API key, either using the argument or by setting theTOGETHER_API_KEY
environmental variable. def with_x_ai(*, model: str | XAIChatModels = 'grok-2-public', api_key: str | None = None, base_url: str | None = 'https://api.x.ai/v1', client: openai.AsyncClient | None = None, user: str | None = None, temperature: float | None = None)
-
Create a new instance of XAI LLM.
api_key
must be set to your XAI API key, either using the argument or by setting theXAI_API_KEY
environmental variable.
Methods
def chat(self, *, chat_ctx: llm.ChatContext, fnc_ctx: llm.FunctionContext | None = None, temperature: float | None = None, n: int | None = 1, parallel_tool_calls: bool | None = None) ‑> livekit.plugins.openai.llm.LLMStream
Inherited members
class LLMStream (llm: LLM, *, oai_stream: Awaitable[openai.AsyncStream[ChatCompletionChunk]], chat_ctx: llm.ChatContext, fnc_ctx: llm.FunctionContext | None)
-
Helper class that provides a standard way to create an ABC using inheritance.
Expand source code
class LLMStream(llm.LLMStream): def __init__( self, llm: LLM, *, oai_stream: Awaitable[openai.AsyncStream[ChatCompletionChunk]], chat_ctx: llm.ChatContext, fnc_ctx: llm.FunctionContext | None, ) -> None: super().__init__(llm, chat_ctx=chat_ctx, fnc_ctx=fnc_ctx) self._awaitable_oai_stream = oai_stream self._oai_stream: openai.AsyncStream[ChatCompletionChunk] | None = None # current function call that we're waiting for full completion (args are streamed) self._tool_call_id: str | None = None self._fnc_name: str | None = None self._fnc_raw_arguments: str | None = None async def _main_task(self) -> None: if not self._oai_stream: self._oai_stream = await self._awaitable_oai_stream try: async with self._oai_stream as stream: async for chunk in stream: for choice in chunk.choices: chat_chunk = self._parse_choice(chunk.id, choice) if chat_chunk is not None: self._event_ch.send_nowait(chat_chunk) if chunk.usage is not None: usage = chunk.usage self._event_ch.send_nowait( llm.ChatChunk( request_id=chunk.id, usage=llm.CompletionUsage( completion_tokens=usage.completion_tokens, prompt_tokens=usage.prompt_tokens, total_tokens=usage.total_tokens, ), ) ) except openai.APITimeoutError: raise APITimeoutError() except openai.APIStatusError as e: raise APIStatusError( e.message, status_code=e.status_code, request_id=e.request_id, body=e.body, ) except Exception as e: raise APIConnectionError() from e def _parse_choice(self, id: str, choice: Choice) -> llm.ChatChunk | None: delta = choice.delta # https://github.com/livekit/agents/issues/688 # the delta can be None when using Azure OpenAI using content filtering if delta is None: return None if delta.tool_calls: # check if we have functions to calls for tool in delta.tool_calls: if not tool.function: continue # oai may add other tools in the future call_chunk = None if self._tool_call_id and tool.id and tool.id != self._tool_call_id: call_chunk = self._try_build_function(id, choice) if tool.function.name: self._tool_call_id = tool.id self._fnc_name = tool.function.name self._fnc_raw_arguments = tool.function.arguments or "" elif tool.function.arguments: self._fnc_raw_arguments += tool.function.arguments # type: ignore if call_chunk is not None: return call_chunk if choice.finish_reason in ("tool_calls", "stop") and self._tool_call_id: # we're done with the tool calls, run the last one return self._try_build_function(id, choice) return llm.ChatChunk( request_id=id, choices=[ llm.Choice( delta=llm.ChoiceDelta(content=delta.content, role="assistant"), index=choice.index, ) ], ) def _try_build_function(self, id: str, choice: Choice) -> llm.ChatChunk | None: if not self._fnc_ctx: logger.warning("oai stream tried to run function without function context") return None if self._tool_call_id is None: logger.warning( "oai stream tried to run function but tool_call_id is not set" ) return None if self._fnc_name is None or self._fnc_raw_arguments is None: logger.warning( "oai stream tried to call a function but raw_arguments and fnc_name are not set" ) return None fnc_info = llm._oai_api.create_ai_function_info( self._fnc_ctx, self._tool_call_id, self._fnc_name, self._fnc_raw_arguments ) self._tool_call_id = self._fnc_name = self._fnc_raw_arguments = None self._function_calls_info.append(fnc_info) return llm.ChatChunk( request_id=id, choices=[ llm.Choice( delta=llm.ChoiceDelta( role="assistant", tool_calls=[fnc_info], content=choice.delta.content, ), index=choice.index, ) ], )
Ancestors
- LLMStream
- abc.ABC
Inherited members
class STT (*, language: str = 'en', detect_language: bool = False, model: WhisperModels | str = 'whisper-1', base_url: str | None = None, api_key: str | None = None, client: openai.AsyncClient | None = None)
-
Helper class that provides a standard way to create an ABC using inheritance.
Create a new instance of OpenAI STT.
api_key
must be set to your OpenAI API key, either using the argument or by setting theOPENAI_API_KEY
environmental variable.Expand source code
class STT(stt.STT): def __init__( self, *, language: str = "en", detect_language: bool = False, model: WhisperModels | str = "whisper-1", base_url: str | None = None, api_key: str | None = None, client: openai.AsyncClient | None = None, ): """ Create a new instance of OpenAI STT. ``api_key`` must be set to your OpenAI API key, either using the argument or by setting the ``OPENAI_API_KEY`` environmental variable. """ super().__init__( capabilities=stt.STTCapabilities(streaming=False, interim_results=False) ) if detect_language: language = "" self._opts = _STTOptions( language=language, detect_language=detect_language, model=model, ) self._client = client or openai.AsyncClient( api_key=api_key, base_url=base_url, http_client=httpx.AsyncClient( timeout=httpx.Timeout(connect=15.0, read=5.0, write=5.0, pool=5.0), follow_redirects=True, limits=httpx.Limits( max_connections=50, max_keepalive_connections=50, keepalive_expiry=120, ), ), ) @staticmethod def with_groq( *, model: GroqAudioModels | str = "whisper-large-v3-turbo", api_key: str | None = None, base_url: str | None = "https://api.groq.com/openai/v1", client: openai.AsyncClient | None = None, language: str = "en", detect_language: bool = False, ) -> STT: """ Create a new instance of Groq STT. ``api_key`` must be set to your Groq API key, either using the argument or by setting the ``GROQ_API_KEY`` environmental variable. """ # Use environment variable if API key is not provided api_key = api_key or os.environ.get("GROQ_API_KEY") if api_key is None: raise ValueError("Groq API key is required") # Instantiate and return a configured STT instance return STT( model=model, api_key=api_key, base_url=base_url, client=client, language=language, detect_language=detect_language, ) def _sanitize_options(self, *, language: str | None = None) -> _STTOptions: config = dataclasses.replace(self._opts) config.language = language or config.language return config async def _recognize_impl( self, buffer: AudioBuffer, *, language: str | None = None ) -> stt.SpeechEvent: try: config = self._sanitize_options(language=language) buffer = utils.merge_frames(buffer) io_buffer = io.BytesIO() with wave.open(io_buffer, "wb") as wav: wav.setnchannels(buffer.num_channels) wav.setsampwidth(2) # 16-bit wav.setframerate(buffer.sample_rate) wav.writeframes(buffer.data) resp = await self._client.audio.transcriptions.create( file=("file.wav", io_buffer.getvalue(), "audio/wav"), model=self._opts.model, language=config.language, response_format="json", ) return stt.SpeechEvent( type=stt.SpeechEventType.FINAL_TRANSCRIPT, alternatives=[ stt.SpeechData(text=resp.text or "", language=language or "") ], ) except openai.APITimeoutError: raise APITimeoutError() except openai.APIStatusError as e: raise APIStatusError( e.message, status_code=e.status_code, request_id=e.request_id, body=e.body, ) except Exception as e: raise APIConnectionError() from e
Ancestors
- STT
- abc.ABC
- EventEmitter
- typing.Generic
Static methods
def with_groq(*, model: GroqAudioModels | str = 'whisper-large-v3-turbo', api_key: str | None = None, base_url: str | None = 'https://api.groq.com/openai/v1', client: openai.AsyncClient | None = None, language: str = 'en', detect_language: bool = False) ‑> livekit.plugins.openai.stt.STT
-
Create a new instance of Groq STT.
api_key
must be set to your Groq API key, either using the argument or by setting theGROQ_API_KEY
environmental variable.
Inherited members
class TTS (*, model: TTSModels | str = 'tts-1', voice: TTSVoices | str = 'alloy', speed: float = 1.0, base_url: str | None = None, api_key: str | None = None, client: openai.AsyncClient | None = None)
-
Helper class that provides a standard way to create an ABC using inheritance.
Create a new instance of OpenAI TTS.
api_key
must be set to your OpenAI API key, either using the argument or by setting theOPENAI_API_KEY
environmental variable.Expand source code
class TTS(tts.TTS): def __init__( self, *, model: TTSModels | str = "tts-1", voice: TTSVoices | str = "alloy", speed: float = 1.0, base_url: str | None = None, api_key: str | None = None, client: openai.AsyncClient | None = None, ) -> None: """ Create a new instance of OpenAI TTS. ``api_key`` must be set to your OpenAI API key, either using the argument or by setting the ``OPENAI_API_KEY`` environmental variable. """ super().__init__( capabilities=tts.TTSCapabilities( streaming=False, ), sample_rate=OPENAI_TTS_SAMPLE_RATE, num_channels=OPENAI_TTS_CHANNELS, ) self._opts = _TTSOptions( model=model, voice=voice, speed=speed, ) self._client = client or openai.AsyncClient( api_key=api_key, base_url=base_url, http_client=httpx.AsyncClient( timeout=httpx.Timeout(connect=15.0, read=5.0, write=5.0, pool=5.0), follow_redirects=True, limits=httpx.Limits( max_connections=50, max_keepalive_connections=50, keepalive_expiry=120, ), ), ) def update_options( self, *, model: TTSModels | None, voice: TTSVoices | None, speed: float | None ) -> None: self._opts.model = model or self._opts.model self._opts.voice = voice or self._opts.voice self._opts.speed = speed or self._opts.speed @staticmethod def create_azure_client( *, model: TTSModels = "tts-1", voice: TTSVoices = "alloy", speed: float = 1.0, azure_endpoint: str | None = None, azure_deployment: str | None = None, api_version: str | None = None, api_key: str | None = None, azure_ad_token: str | None = None, azure_ad_token_provider: AsyncAzureADTokenProvider | None = None, organization: str | None = None, project: str | None = None, base_url: str | None = None, ) -> TTS: """ This automatically infers the following arguments from their corresponding environment variables if they are not provided: - `api_key` from `AZURE_OPENAI_API_KEY` - `organization` from `OPENAI_ORG_ID` - `project` from `OPENAI_PROJECT_ID` - `azure_ad_token` from `AZURE_OPENAI_AD_TOKEN` - `api_version` from `OPENAI_API_VERSION` - `azure_endpoint` from `AZURE_OPENAI_ENDPOINT` """ azure_client = openai.AsyncAzureOpenAI( azure_endpoint=azure_endpoint, azure_deployment=azure_deployment, api_version=api_version, api_key=api_key, azure_ad_token=azure_ad_token, azure_ad_token_provider=azure_ad_token_provider, organization=organization, project=project, base_url=base_url, ) # type: ignore return TTS(model=model, voice=voice, speed=speed, client=azure_client) def synthesize(self, text: str) -> "ChunkedStream": return ChunkedStream( self, text, self._client.audio.speech.with_streaming_response.create( input=text, model=self._opts.model, voice=self._opts.voice, # type: ignore response_format="mp3", speed=self._opts.speed, ), )
Ancestors
- TTS
- abc.ABC
- EventEmitter
- typing.Generic
Static methods
def create_azure_client(*, model: TTSModels = 'tts-1', voice: TTSVoices = 'alloy', speed: float = 1.0, azure_endpoint: str | None = None, azure_deployment: str | None = None, api_version: str | None = None, api_key: str | None = None, azure_ad_token: str | None = None, azure_ad_token_provider: AsyncAzureADTokenProvider | None = None, organization: str | None = None, project: str | None = None, base_url: str | None = None) ‑> livekit.plugins.openai.tts.TTS
-
This automatically infers the following arguments from their corresponding environment variables if they are not provided: -
api_key
fromAZURE_OPENAI_API_KEY
-organization
fromOPENAI_ORG_ID
-project
fromOPENAI_PROJECT_ID
-azure_ad_token
fromAZURE_OPENAI_AD_TOKEN
-api_version
fromOPENAI_API_VERSION
-azure_endpoint
fromAZURE_OPENAI_ENDPOINT
Methods
def synthesize(self, text: str) ‑> livekit.plugins.openai.tts.ChunkedStream
def update_options(self, *, model: TTSModels | None, voice: TTSVoices | None, speed: float | None) ‑> None
Inherited members