mirror of
https://github.com/xtekky/gpt4free.git
synced 2025-09-27 04:36:17 +08:00
Enhance OpenaiTemplate and Nvidia providers with support for text/plain responses and improved error handling
This commit is contained in:
@@ -13,4 +13,5 @@ class Nvidia(OpenaiTemplate):
|
|||||||
needs_auth = True
|
needs_auth = True
|
||||||
models_needs_auth = True
|
models_needs_auth = True
|
||||||
default_model = DEFAULT_MODEL
|
default_model = DEFAULT_MODEL
|
||||||
add_user = False
|
add_user = False
|
||||||
|
supports_text_plain = False
|
@@ -135,7 +135,7 @@ class SharedTokenManager(AuthFileMixin):
|
|||||||
data = json.load(fs)
|
data = json.load(fs)
|
||||||
credentials = self.validateCredentials(data)
|
credentials = self.validateCredentials(data)
|
||||||
self.memory_cache["credentials"] = credentials
|
self.memory_cache["credentials"] = credentials
|
||||||
except FileNotFoundError:
|
except FileNotFoundError as e:
|
||||||
self.memory_cache["credentials"] = None
|
self.memory_cache["credentials"] = None
|
||||||
raise TokenManagerError(TokenError.FILE_ACCESS_ERROR, "Credentials file not found", e) from e
|
raise TokenManagerError(TokenError.FILE_ACCESS_ERROR, "Credentials file not found", e) from e
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
|
@@ -29,6 +29,7 @@ class OpenaiTemplate(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin
|
|||||||
add_user = True
|
add_user = True
|
||||||
use_image_size = False
|
use_image_size = False
|
||||||
max_tokens: int = None
|
max_tokens: int = None
|
||||||
|
supports_text_plain = True # Whether the provider can handle text/plain responses
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_models(cls, api_key: str = None, api_base: str = None) -> list[str]:
|
def get_models(cls, api_key: str = None, api_base: str = None) -> list[str]:
|
||||||
@@ -61,8 +62,10 @@ class OpenaiTemplate(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin
|
|||||||
except MissingAuthError:
|
except MissingAuthError:
|
||||||
raise
|
raise
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
debug.error(e)
|
if cls.fallback_models:
|
||||||
return cls.fallback_models
|
debug.error(e)
|
||||||
|
return cls.fallback_models
|
||||||
|
raise e
|
||||||
return cls.models
|
return cls.models
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -145,7 +148,7 @@ class OpenaiTemplate(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin
|
|||||||
if api_endpoint is None:
|
if api_endpoint is None:
|
||||||
api_endpoint = cls.api_endpoint
|
api_endpoint = cls.api_endpoint
|
||||||
async with session.post(api_endpoint, json=data, ssl=cls.ssl) as response:
|
async with session.post(api_endpoint, json=data, ssl=cls.ssl) as response:
|
||||||
async for chunk in read_response(response, stream, prompt, cls.get_dict(), download_media):
|
async for chunk in read_response(response, stream, prompt, cls.get_dict(), download_media, cls.supports_text_plain):
|
||||||
yield chunk
|
yield chunk
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -160,9 +163,9 @@ class OpenaiTemplate(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin
|
|||||||
**({} if headers is None else headers)
|
**({} if headers is None else headers)
|
||||||
}
|
}
|
||||||
|
|
||||||
async def read_response(response: StreamResponse, stream: bool, prompt: str, provider_info: dict, download_media: bool):
|
async def read_response(response: StreamResponse, stream: bool, prompt: str, provider_info: dict, download_media: bool, supports_text_plain: bool = True) -> AsyncResult:
|
||||||
content_type = response.headers.get("content-type", "text/event-stream" if stream else "application/json")
|
content_type = response.headers.get("content-type", "text/event-stream" if stream else "application/json")
|
||||||
if content_type.startswith("text/plain"):
|
if supports_text_plain and content_type.startswith("text/plain"):
|
||||||
yield await response.text()
|
yield await response.text()
|
||||||
elif content_type.startswith("application/json"):
|
elif content_type.startswith("application/json"):
|
||||||
data = await response.json()
|
data = await response.json()
|
||||||
|
Reference in New Issue
Block a user