Enhance Azure provider error handling and update environment variables

This commit is contained in:
hlohaus 2025-09-22 22:21:03 +02:00
parent cb0136fdcf
commit 391e1f463e
4 changed files with 9 additions and 8 deletions

View file

@ -5,6 +5,8 @@ G4F_API_KEY=
G4F_PROXY= G4F_PROXY=
G4F_TIMEOUT= G4F_TIMEOUT=
G4F_STREAM_TIMEOUT= G4F_STREAM_TIMEOUT=
G4F_BROWSER_PORT=
G4F_BROWSER_HOST=
HUGGINGFACE_API_KEY= HUGGINGFACE_API_KEY=
POLLINATIONS_API_KEY= POLLINATIONS_API_KEY=

View file

@ -132,9 +132,7 @@ class Azure(OpenaiTemplate):
for key, value in cls.model_extra_body[model].items(): for key, value in cls.model_extra_body[model].items():
kwargs.setdefault(key, value) kwargs.setdefault(key, value)
stream = False stream = False
if stream: if cls.failed.get(model + api_key, 0) >= 3:
kwargs.setdefault("stream_options", {"include_usage": True})
if cls.failed.get(api_key, 0) >= 3:
raise MissingAuthError(f"API key has failed too many times.") raise MissingAuthError(f"API key has failed too many times.")
try: try:
async for chunk in super().create_async_generator( async for chunk in super().create_async_generator(
@ -148,5 +146,5 @@ class Azure(OpenaiTemplate):
): ):
yield chunk yield chunk
except MissingAuthError as e: except MissingAuthError as e:
cls.failed[api_key] = cls.failed.get(api_key, 0) + 1 cls.failed[model + api_key] = cls.failed.get(model + api_key, 0) + 1
raise MissingAuthError(f"{e}. Ask for help in the {cls.login_url} Discord server.") from e raise MissingAuthError(f"{e}. Ask for help in the {cls.login_url} Discord server.") from e

View file

@ -89,7 +89,7 @@ class OpenaiTemplate(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin
headers: dict = None, headers: dict = None,
impersonate: str = None, impersonate: str = None,
download_media: bool = True, download_media: bool = True,
extra_parameters: list[str] = ["tools", "parallel_tool_calls", "tool_choice", "reasoning_effort", "logit_bias", "modalities", "audio", "stream_options"], extra_parameters: list[str] = ["tools", "parallel_tool_calls", "tool_choice", "reasoning_effort", "logit_bias", "modalities", "audio", "stream_options", "include_reasoning", "response_format", "max_completion_tokens", "reasoning_effort", "search_settings"],
extra_body: dict = None, extra_body: dict = None,
**kwargs **kwargs
) -> AsyncResult: ) -> AsyncResult:
@ -127,6 +127,8 @@ class OpenaiTemplate(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin
yield ImageResponse([image["url"] for image in data["data"]], prompt) yield ImageResponse([image["url"] for image in data["data"]], prompt)
return return
if stream:
kwargs.setdefault("stream_options", {"include_usage": True})
extra_parameters = {key: kwargs[key] for key in extra_parameters if key in kwargs} extra_parameters = {key: kwargs[key] for key in extra_parameters if key in kwargs}
if extra_body is None: if extra_body is None:
extra_body = {} extra_body = {}

View file

@ -11,7 +11,6 @@ import time
from email.utils import formatdate from email.utils import formatdate
import os.path import os.path
import hashlib import hashlib
import asyncio
import base64 import base64
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from urllib.parse import quote_plus from urllib.parse import quote_plus
@ -109,7 +108,7 @@ async def lifespan(app: FastAPI):
try: try:
os.remove(lock_file) os.remove(lock_file)
except Exception as e: except Exception as e:
debug.error(f"Failed to remove lock file {lock_file}:" ,e) debug.error(f"Failed to remove lock file {lock_file}:", e)
def create_app(): def create_app():
app = FastAPI(lifespan=lifespan) app = FastAPI(lifespan=lifespan)
@ -270,7 +269,7 @@ class Api:
user = "admin" user = "admin"
path = request.url.path path = request.url.path
if path.startswith("/v1") or path.startswith("/api/") or (AppConfig.demo and path == '/backend-api/v2/upload_cookies'): if path.startswith("/v1") or path.startswith("/api/") or (AppConfig.demo and path == '/backend-api/v2/upload_cookies'):
if request.method != "OPTIONS": if request.method != "OPTIONS" and not path.endswith("/models"):
if user_g4f_api_key is None: if user_g4f_api_key is None:
return ErrorResponse.from_message("G4F API key required", HTTP_401_UNAUTHORIZED) return ErrorResponse.from_message("G4F API key required", HTTP_401_UNAUTHORIZED)
if AppConfig.g4f_api_key is None and user is None: if AppConfig.g4f_api_key is None and user is None: