XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

Enhance OpenaiTemplate and Nvidia providers with support for text/plain responses and improved error handling

217c1e85
hlohaus <983577+hlohaus@users.noreply.github.com>
提交于

代码差异

3 个文件 +11 -7
Modified g4f/Provider/needs_auth/Nvidia.py +2 -1
@@ -13,4 +13,5 @@ class Nvidia(OpenaiTemplate):
13 13 needs_auth = True
14 14 models_needs_auth = True
15 15 default_model = DEFAULT_MODEL
16 add_user = False
16 add_user = False
17 supports_text_plain = False
Modified g4f/Provider/qwen/sharedTokenManager.py +1 -1
@@ -135,7 +135,7 @@ class SharedTokenManager(AuthFileMixin):
135 135 data = json.load(fs)
136 136 credentials = self.validateCredentials(data)
137 137 self.memory_cache["credentials"] = credentials
138 except FileNotFoundError:
138 except FileNotFoundError as e:
139 139 self.memory_cache["credentials"] = None
140 140 raise TokenManagerError(TokenError.FILE_ACCESS_ERROR, "Credentials file not found", e) from e
141 141 except json.JSONDecodeError as e:
Modified g4f/Provider/template/OpenaiTemplate.py +8 -5
@@ -29,6 +29,7 @@ class OpenaiTemplate(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin
29 29 add_user = True
30 30 use_image_size = False
31 31 max_tokens: int = None
32 supports_text_plain = True # Whether the provider can handle text/plain responses
32 33
33 34 @classmethod
34 35 def get_models(cls, api_key: str = None, api_base: str = None) -> list[str]:
@@ -61,8 +62,10 @@ class OpenaiTemplate(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin
61 62 except MissingAuthError:
62 63 raise
63 64 except Exception as e:
64 debug.error(e)
65 return cls.fallback_models
65 if cls.fallback_models:
66 debug.error(e)
67 return cls.fallback_models
68 raise e
66 69 return cls.models
67 70
68 71 @classmethod
@@ -145,7 +148,7 @@ class OpenaiTemplate(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin
145 148 if api_endpoint is None:
146 149 api_endpoint = cls.api_endpoint
147 150 async with session.post(api_endpoint, json=data, ssl=cls.ssl) as response:
148 async for chunk in read_response(response, stream, prompt, cls.get_dict(), download_media):
151 async for chunk in read_response(response, stream, prompt, cls.get_dict(), download_media, cls.supports_text_plain):
149 152 yield chunk
150 153
151 154 @classmethod
@@ -160,9 +163,9 @@ class OpenaiTemplate(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin
160 163 **({} if headers is None else headers)
161 164 }
162 165
163 async def read_response(response: StreamResponse, stream: bool, prompt: str, provider_info: dict, download_media: bool):
166 async def read_response(response: StreamResponse, stream: bool, prompt: str, provider_info: dict, download_media: bool, supports_text_plain: bool = True) -> AsyncResult:
164 167 content_type = response.headers.get("content-type", "text/event-stream" if stream else "application/json")
165 if content_type.startswith("text/plain"):
168 if supports_text_plain and content_type.startswith("text/plain"):
166 169 yield await response.text()
167 170 elif content_type.startswith("application/json"):
168 171 data = await response.json()