summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/needs_auth/Openai.py
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2024-04-18 21:13:26 +0200
committerGitHub <noreply@github.com>2024-04-18 21:13:26 +0200
commit718ea7c187b0852aacc18bf890fcc105dfff2665 (patch)
tree84f59d4ea5a679382df3f71ff3de027aad2a8fd4 /g4f/Provider/needs_auth/Openai.py
parentMerge pull request #1848 from hlohaus/nem (diff)
parentAdd Ecosia Provider, Add OpenaiAccount alias (diff)
downloadgpt4free-0.3.0.0.tar
gpt4free-0.3.0.0.tar.gz
gpt4free-0.3.0.0.tar.bz2
gpt4free-0.3.0.0.tar.lz
gpt4free-0.3.0.0.tar.xz
gpt4free-0.3.0.0.tar.zst
gpt4free-0.3.0.0.zip
Diffstat (limited to 'g4f/Provider/needs_auth/Openai.py')
-rw-r--r--g4f/Provider/needs_auth/Openai.py11
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 {