summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/needs_auth/Openai.py
diff options
context:
space:
mode:
authorHeiner Lohaus <hlohaus@users.noreply.github.com>2024-04-18 20:18:51 +0200
committerHeiner Lohaus <hlohaus@users.noreply.github.com>2024-04-18 20:18:51 +0200
commit7e543f4747914f280454f09b070ead500fb72277 (patch)
tree94e4680f009d25d2437895262e50316c2020b80e /g4f/Provider/needs_auth/Openai.py
parentAdd missing styling on phone (diff)
downloadgpt4free-7e543f4747914f280454f09b070ead500fb72277.tar
gpt4free-7e543f4747914f280454f09b070ead500fb72277.tar.gz
gpt4free-7e543f4747914f280454f09b070ead500fb72277.tar.bz2
gpt4free-7e543f4747914f280454f09b070ead500fb72277.tar.lz
gpt4free-7e543f4747914f280454f09b070ead500fb72277.tar.xz
gpt4free-7e543f4747914f280454f09b070ead500fb72277.tar.zst
gpt4free-7e543f4747914f280454f09b070ead500fb72277.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 {