This commit is contained in:
Nicolas Mowen 2026-05-19 10:35:34 -06:00
parent ea2e423e11
commit bd0ce61ab9
2 changed files with 16 additions and 17 deletions

View File

@ -7,14 +7,15 @@ overrides what is genuinely Azure-specific:
- Client construction: parses ``api-version`` out of the configured - Client construction: parses ``api-version`` out of the configured
``base_url`` query string and instantiates :class:`openai.AzureOpenAI` ``base_url`` query string and instantiates :class:`openai.AzureOpenAI`
with ``azure_endpoint`` instead of ``base_url``. with ``azure_endpoint`` instead of ``base_url``. Raises if the URL is
malformed; :class:`GenAIClientManager` catches the exception and
disables the provider.
- Context size: Azure does not expose a per-model ``max_model_len`` field - Context size: Azure does not expose a per-model ``max_model_len`` field
reliably, so we keep the historical 128K default rather than the reliably, so we keep the historical 128K default rather than the
model-name heuristic used by OpenAI. model-name heuristic used by OpenAI.
""" """
import logging import logging
from typing import Optional
from urllib.parse import parse_qs, urlparse from urllib.parse import parse_qs, urlparse
from openai import AzureOpenAI from openai import AzureOpenAI
@ -30,22 +31,16 @@ logger = logging.getLogger(__name__)
class AzureOpenAIClient(OpenAIClient): class AzureOpenAIClient(OpenAIClient):
"""Generative AI client for Frigate using Azure OpenAI.""" """Generative AI client for Frigate using Azure OpenAI."""
provider: AzureOpenAI # type: ignore[assignment] def _init_provider(self) -> AzureOpenAI:
def _init_provider(self) -> Optional[AzureOpenAI]:
"""Initialize the AzureOpenAI client from the configured base_url.""" """Initialize the AzureOpenAI client from the configured base_url."""
try: parsed_url = urlparse(self.genai_config.base_url or "")
parsed_url = urlparse(self.genai_config.base_url or "") query_params = parse_qs(parsed_url.query)
query_params = parse_qs(parsed_url.query) api_version = query_params.get("api-version", [None])[0]
api_version = query_params.get("api-version", [None])[0]
azure_endpoint = f"{parsed_url.scheme}://{parsed_url.netloc}/"
if not api_version: if not api_version:
logger.warning("Azure OpenAI url is missing API version.") raise ValueError("Azure OpenAI base_url is missing api-version.")
return None
except Exception as e: azure_endpoint = f"{parsed_url.scheme}://{parsed_url.netloc}/"
logger.warning("Error parsing Azure OpenAI url: %s", str(e))
return None
return AzureOpenAI( return AzureOpenAI(
api_key=self.genai_config.api_key, api_key=self.genai_config.api_key,

View File

@ -38,7 +38,11 @@ class OpenAIClient(GenAIClient):
context_size: Optional[int] = None context_size: Optional[int] = None
def _init_provider(self) -> OpenAI: def _init_provider(self) -> OpenAI:
"""Initialize the client.""" """Initialize the client.
Subclasses (e.g. Azure) should raise on configuration errors; the
manager catches construction failures and disables the provider.
"""
# Extract context_size from provider_options as it's not a valid OpenAI client parameter # Extract context_size from provider_options as it's not a valid OpenAI client parameter
# It will be used in get_context_size() instead # It will be used in get_context_size() instead
provider_opts = { provider_opts = {