diff options
author | H Lohaus <hlohaus@users.noreply.github.com> | 2024-04-18 21:13:26 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-18 21:13:26 +0200 |
commit | 718ea7c187b0852aacc18bf890fcc105dfff2665 (patch) | |
tree | 84f59d4ea5a679382df3f71ff3de027aad2a8fd4 /g4f/Provider/needs_auth/Openai.py | |
parent | Merge pull request #1848 from hlohaus/nem (diff) | |
parent | Add Ecosia Provider, Add OpenaiAccount alias (diff) | |
download | gpt4free-718ea7c187b0852aacc18bf890fcc105dfff2665.tar gpt4free-718ea7c187b0852aacc18bf890fcc105dfff2665.tar.gz gpt4free-718ea7c187b0852aacc18bf890fcc105dfff2665.tar.bz2 gpt4free-718ea7c187b0852aacc18bf890fcc105dfff2665.tar.lz gpt4free-718ea7c187b0852aacc18bf890fcc105dfff2665.tar.xz gpt4free-718ea7c187b0852aacc18bf890fcc105dfff2665.tar.zst gpt4free-718ea7c187b0852aacc18bf890fcc105dfff2665.zip |
Diffstat (limited to '')
-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 { |