From 7e543f4747914f280454f09b070ead500fb72277 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Thu, 18 Apr 2024 20:18:51 +0200 Subject: Add Ecosia Provider, Add OpenaiAccount alias Use AsyncClient in API, add web_search parameter in API Improve error messages in Openai --- g4f/Provider/needs_auth/Openai.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'g4f/Provider/needs_auth/Openai.py') 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 { -- cgit v1.2.3