diff options
Diffstat (limited to 'g4f/Provider/needs_auth/Openai.py')
-rw-r--r-- | g4f/Provider/needs_auth/Openai.py | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/g4f/Provider/needs_auth/Openai.py b/g4f/Provider/needs_auth/Openai.py index 80318f6d..f73c1011 100644 --- a/g4f/Provider/needs_auth/Openai.py +++ b/g4f/Provider/needs_auth/Openai.py @@ -56,6 +56,7 @@ class Openai(AsyncGeneratorProvider, ProviderModelMixin): await raise_for_status(response) if not stream: data = await response.json() + cls.raise_error(data) choice = data["choices"][0] if "content" in choice["message"]: yield choice["message"]["content"].strip() @@ -70,8 +71,7 @@ class Openai(AsyncGeneratorProvider, ProviderModelMixin): if chunk == b"[DONE]": break data = json.loads(chunk) - if "error_message" in data: - raise ResponseError(data["error_message"]) + cls.raise_error(data) choice = data["choices"][0] if "content" in choice["delta"] and choice["delta"]["content"]: delta = choice["delta"]["content"] @@ -89,6 +89,13 @@ class Openai(AsyncGeneratorProvider, ProviderModelMixin): if "finish_reason" in choice and choice["finish_reason"] is not None: return FinishReason(choice["finish_reason"]) + @staticmethod + def raise_error(data: dict): + if "error_message" in data: + raise ResponseError(data["error_message"]) + elif "error" in data: + raise ResponseError(f'Error {data["error"]["code"]}: {data["error"]["message"]}') + @classmethod def get_headers(cls, stream: bool, api_key: str = None, headers: dict = None) -> dict: return { |