Add azure provider

This commit is contained in:
hlohaus 2025-07-11 05:57:04 +02:00
parent 7965487830
commit 210dfdc537
5 changed files with 46 additions and 9 deletions

View file

@ -0,0 +1,36 @@
from __future__ import annotations
import os
from ...typing import Messages, AsyncResult
from ..template import OpenaiTemplate
class Azure(OpenaiTemplate):
working = True
needs_auth = True
@classmethod
async def create_async_generator(
cls,
model: str,
messages: Messages,
api_key: str = None,
api_endpoint: str = None,
**kwargs
) -> AsyncResult:
if not model:
model = os.environ.get("AZURE_DEFAULT_MODEL", cls.default_model)
if not api_key:
raise ValueError("API key is required for Azure provider")
if not api_endpoint:
api_endpoint = os.environ.get("AZURE_API_ENDPOINT")
if not api_endpoint:
raise ValueError("API endpoint is required for Azure provider")
async for chunk in super().create_async_generator(
model=model,
messages=messages,
api_key=api_key,
api_endpoint=api_endpoint,
**kwargs
):
yield chunk

View file

@ -1,4 +1,5 @@
from .Anthropic import Anthropic
from .Azure import Azure
from .BingCreateImages import BingCreateImages
from .BlackboxPro import BlackboxPro
from .CablyAI import CablyAI

View file

@ -135,12 +135,12 @@ class OpenaiTemplate(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin
if "usage" in data:
yield Usage(**data["usage"])
if "choices" in data:
choice = data["choices"][0]
if "content" in choice["message"] and choice["message"]["content"]:
choice = next(iter(data["choices"]), None)
if choice and "content" in choice["message"] and choice["message"]["content"]:
yield choice["message"]["content"].strip()
if "tool_calls" in choice["message"]:
yield ToolCalls(choice["message"]["tool_calls"])
if "finish_reason" in choice and choice["finish_reason"] is not None:
if choice and "finish_reason" in choice and choice["finish_reason"] is not None:
yield FinishReason(choice["finish_reason"])
return
elif content_type.startswith("text/event-stream"):
@ -153,8 +153,8 @@ class OpenaiTemplate(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin
if not model_returned and model:
yield ProviderInfo(**cls.get_dict(), model=model)
model_returned = True
choice = data["choices"][0]
if "content" in choice["delta"] and choice["delta"]["content"]:
choice = next(iter(data["choices"]), None)
if choice and "content" in choice["delta"] and choice["delta"]["content"]:
delta = choice["delta"]["content"]
if first:
delta = delta.lstrip()
@ -163,7 +163,7 @@ class OpenaiTemplate(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin
yield delta
if "usage" in data and data["usage"]:
yield Usage(**data["usage"])
if "finish_reason" in choice and choice["finish_reason"] is not None:
if choice and "finish_reason" in choice and choice["finish_reason"] is not None:
yield FinishReason(choice["finish_reason"])
break
else:

View file

@ -408,8 +408,8 @@ class RaiseErrorMixin():
raise ResponseError(data["error"]["message"])
else:
raise ResponseError(data["error"])
elif ("choices" not in data or not data["choices"]) and "data" not in data:
raise ResponseError(f"Invalid response: {json.dumps(data)}")
#elif ("choices" not in data or not data["choices"]) and "data" not in data:
# raise ResponseError(f"Invalid response: {json.dumps(data)}")
class AuthFileMixin():

View file

@ -10,7 +10,7 @@ from ..typing import Messages
from ..image import is_data_an_media, to_input_audio, is_valid_media, is_valid_audio, to_data_uri
from .files import get_bucket_dir, read_bucket
def render_media(bucket_id: str, name: str, url: str, as_path: bool = False, as_base64: bool = False) -> Union[str, Path]:
def render_media(bucket_id: str, name: str, url: str, as_path: bool = False, as_base64: bool = False, **kwargs) -> Union[str, Path]:
if (as_base64 or as_path or url.startswith("/")):
file = Path(get_bucket_dir(bucket_id, "thumbnail", name))
if not file.exists():