mirror of
https://github.com/xtekky/gpt4free.git
synced 2025-09-26 20:31:14 +08:00
Enhance Azure provider error handling and update environment variables
This commit is contained in:
@@ -5,6 +5,8 @@ G4F_API_KEY=
|
||||
G4F_PROXY=
|
||||
G4F_TIMEOUT=
|
||||
G4F_STREAM_TIMEOUT=
|
||||
G4F_BROWSER_PORT=
|
||||
G4F_BROWSER_HOST=
|
||||
|
||||
HUGGINGFACE_API_KEY=
|
||||
POLLINATIONS_API_KEY=
|
||||
|
@@ -132,9 +132,7 @@ class Azure(OpenaiTemplate):
|
||||
for key, value in cls.model_extra_body[model].items():
|
||||
kwargs.setdefault(key, value)
|
||||
stream = False
|
||||
if stream:
|
||||
kwargs.setdefault("stream_options", {"include_usage": True})
|
||||
if cls.failed.get(api_key, 0) >= 3:
|
||||
if cls.failed.get(model + api_key, 0) >= 3:
|
||||
raise MissingAuthError(f"API key has failed too many times.")
|
||||
try:
|
||||
async for chunk in super().create_async_generator(
|
||||
@@ -148,5 +146,5 @@ class Azure(OpenaiTemplate):
|
||||
):
|
||||
yield chunk
|
||||
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
|
@@ -89,7 +89,7 @@ class OpenaiTemplate(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin
|
||||
headers: dict = None,
|
||||
impersonate: str = None,
|
||||
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,
|
||||
**kwargs
|
||||
) -> AsyncResult:
|
||||
@@ -127,6 +127,8 @@ class OpenaiTemplate(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin
|
||||
yield ImageResponse([image["url"] for image in data["data"]], prompt)
|
||||
return
|
||||
|
||||
if stream:
|
||||
kwargs.setdefault("stream_options", {"include_usage": True})
|
||||
extra_parameters = {key: kwargs[key] for key in extra_parameters if key in kwargs}
|
||||
if extra_body is None:
|
||||
extra_body = {}
|
||||
|
@@ -11,7 +11,6 @@ import time
|
||||
from email.utils import formatdate
|
||||
import os.path
|
||||
import hashlib
|
||||
import asyncio
|
||||
import base64
|
||||
from contextlib import asynccontextmanager
|
||||
from urllib.parse import quote_plus
|
||||
@@ -109,7 +108,7 @@ async def lifespan(app: FastAPI):
|
||||
try:
|
||||
os.remove(lock_file)
|
||||
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():
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
@@ -270,7 +269,7 @@ class Api:
|
||||
user = "admin"
|
||||
path = request.url.path
|
||||
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:
|
||||
return ErrorResponse.from_message("G4F API key required", HTTP_401_UNAUTHORIZED)
|
||||
if AppConfig.g4f_api_key is None and user is None:
|
||||
|
Reference in New Issue
Block a user