79 lines
2.8 KiB
Python
79 lines
2.8 KiB
Python
import httpx
|
|
from openai import AsyncAzureOpenAI
|
|
from openai.types import Reasoning
|
|
from openai.types.shared_params import ResponsesModel
|
|
|
|
from livekit.agents.llm import ToolChoice
|
|
from livekit.agents.types import (
|
|
NOT_GIVEN,
|
|
NotGivenOr,
|
|
)
|
|
from livekit.plugins import openai
|
|
from livekit.plugins.openai.utils import AsyncAzureADTokenProvider
|
|
|
|
|
|
class LLM(openai.responses.LLM):
|
|
def __init__(
|
|
self,
|
|
*,
|
|
model: str | ResponsesModel = "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: NotGivenOr[str] = NOT_GIVEN,
|
|
temperature: NotGivenOr[float] = NOT_GIVEN,
|
|
parallel_tool_calls: NotGivenOr[bool] = NOT_GIVEN,
|
|
tool_choice: NotGivenOr[ToolChoice] = NOT_GIVEN,
|
|
timeout: httpx.Timeout | None = None,
|
|
reasoning: NotGivenOr[Reasoning] = NOT_GIVEN,
|
|
max_output_tokens: NotGivenOr[int] = NOT_GIVEN,
|
|
) -> None:
|
|
"""
|
|
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`
|
|
""" # noqa: E501
|
|
|
|
azure_client = AsyncAzureOpenAI(
|
|
max_retries=0,
|
|
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,
|
|
timeout=timeout
|
|
if timeout
|
|
else httpx.Timeout(connect=15.0, read=5.0, write=5.0, pool=5.0),
|
|
) # type: ignore
|
|
|
|
super().__init__(
|
|
model=model,
|
|
client=azure_client,
|
|
use_websocket=False,
|
|
user=user,
|
|
temperature=temperature,
|
|
parallel_tool_calls=parallel_tool_calls,
|
|
tool_choice=tool_choice,
|
|
reasoning=reasoning,
|
|
max_output_tokens=max_output_tokens,
|
|
)
|
|
self._azure_client = azure_client
|
|
|
|
async def aclose(self) -> None:
|
|
await super().aclose()
|
|
await self._azure_client.close()
|