-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
breaking: Use AI Factory SDK and LLM tracing
- Loading branch information
Showing
13 changed files
with
329 additions
and
520 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,40 @@ | ||
from abc import abstractmethod | ||
from enum import Enum | ||
from typing import Any | ||
|
||
from openai import AsyncAzureOpenAI, AsyncOpenAI | ||
from pydantic import BaseModel, Field, SecretStr, ValidationInfo, field_validator | ||
from azure.ai.inference.aio import ChatCompletionsClient | ||
from pydantic import BaseModel | ||
|
||
from app.helpers.cache import async_lru_cache | ||
from app.helpers.identity import token | ||
|
||
|
||
class ModeEnum(str, Enum): | ||
AZURE_OPENAI = "azure_openai" | ||
"""Use Azure OpenAI.""" | ||
OPENAI = "openai" | ||
"""Use OpenAI.""" | ||
from app.helpers.http import azure_transport | ||
from app.helpers.identity import credential | ||
|
||
|
||
class AbstractPlatformModel(BaseModel, frozen=True): | ||
_client_kwargs: dict[str, Any] = { | ||
# Reliability | ||
"max_retries": 0, # Retries are managed manually | ||
"timeout": 60, | ||
} | ||
class DeploymentModel(BaseModel, frozen=True): | ||
api_version: str = "2024-10-21" # See: https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#api-specs | ||
context: int | ||
endpoint: str | ||
model: str | ||
seed: int = 42 # Reproducible results | ||
streaming: bool | ||
temperature: float = 0.0 # Most focused and deterministic | ||
|
||
@abstractmethod | ||
async def instance( | ||
self, | ||
) -> tuple[AsyncAzureOpenAI | AsyncOpenAI, "AbstractPlatformModel"]: | ||
pass | ||
|
||
|
||
class AzureOpenaiPlatformModel(AbstractPlatformModel, frozen=True): | ||
api_version: str = "2024-06-01" | ||
deployment: str | ||
|
||
@async_lru_cache() | ||
async def instance(self) -> tuple[AsyncAzureOpenAI, AbstractPlatformModel]: | ||
return AsyncAzureOpenAI( | ||
**self._client_kwargs, | ||
async def instance(self) -> tuple[ChatCompletionsClient, "DeploymentModel"]: | ||
return ChatCompletionsClient( | ||
# Reliability | ||
seed=self.seed, | ||
temperature=self.temperature, | ||
# Deployment | ||
api_version=self.api_version, | ||
azure_deployment=self.deployment, | ||
azure_endpoint=self.endpoint, | ||
endpoint=self.endpoint, | ||
model=self.model, | ||
# Performance | ||
transport=await azure_transport(), | ||
# Authentication | ||
azure_ad_token_provider=await token( | ||
"https://cognitiveservices.azure.com/.default" | ||
), | ||
credential_scopes=["https://cognitiveservices.azure.com/.default"], | ||
credential=await credential(), | ||
), self | ||
|
||
|
||
class OpenaiPlatformModel(AbstractPlatformModel, frozen=True): | ||
api_key: SecretStr | ||
|
||
@async_lru_cache() | ||
async def instance(self) -> tuple[AsyncOpenAI, AbstractPlatformModel]: | ||
return AsyncOpenAI( | ||
**self._client_kwargs, | ||
# API root URL | ||
base_url=self.endpoint, | ||
# Authentication | ||
api_key=self.api_key.get_secret_value(), | ||
), self | ||
|
||
|
||
class SelectedPlatformModel(BaseModel): | ||
azure_openai: AzureOpenaiPlatformModel | None = None | ||
mode: ModeEnum | ||
openai: OpenaiPlatformModel | None = None | ||
|
||
@field_validator("azure_openai") | ||
@classmethod | ||
def _validate_azure_openai( | ||
cls, | ||
azure_openai: AzureOpenaiPlatformModel | None, | ||
info: ValidationInfo, | ||
) -> AzureOpenaiPlatformModel | None: | ||
if not azure_openai and info.data.get("mode", None) == ModeEnum.AZURE_OPENAI: | ||
raise ValueError("Azure OpenAI config required") | ||
return azure_openai | ||
|
||
@field_validator("openai") | ||
@classmethod | ||
def _validate_openai( | ||
cls, | ||
openai: OpenaiPlatformModel | None, | ||
info: ValidationInfo, | ||
) -> OpenaiPlatformModel | None: | ||
if not openai and info.data.get("mode", None) == ModeEnum.OPENAI: | ||
raise ValueError("OpenAI config required") | ||
return openai | ||
|
||
def selected(self) -> AzureOpenaiPlatformModel | OpenaiPlatformModel: | ||
platform = ( | ||
self.azure_openai if self.mode == ModeEnum.AZURE_OPENAI else self.openai | ||
) | ||
assert platform | ||
return platform | ||
|
||
|
||
class LlmModel(BaseModel): | ||
fast: SelectedPlatformModel = Field( | ||
serialization_alias="backup", # Backwards compatibility with v6 | ||
) | ||
slow: SelectedPlatformModel = Field( | ||
serialization_alias="primary", # Backwards compatibility with v6 | ||
) | ||
fast: DeploymentModel | ||
slow: DeploymentModel | ||
|
||
def selected(self, is_fast: bool) -> AzureOpenaiPlatformModel | OpenaiPlatformModel: | ||
platform = self.fast if is_fast else self.slow | ||
return platform.selected() | ||
def selected(self, is_fast: bool) -> DeploymentModel: | ||
return self.fast if is_fast else self.slow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.