From c9b88e4c1f9b2dae3548c1b8509c82452ac137ed Mon Sep 17 00:00:00 2001 From: zjzj1996 Date: Tue, 7 Mar 2023 10:32:32 +0800 Subject: [PATCH 1/8] fix:fix bug --- openai/api_requestor.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/openai/api_requestor.py b/openai/api_requestor.py index 64e55e82ef..3b3c99260c 100644 --- a/openai/api_requestor.py +++ b/openai/api_requestor.py @@ -666,7 +666,10 @@ def _interpret_response_line( headers=rheaders, ) try: - data = json.loads(rbody) + if rheaders.get('Content-Type') == 'application/json': + data = json.loads(rbody) + else: + data = rbody except (JSONDecodeError, UnicodeDecodeError) as e: raise error.APIError( f"HTTP code {rcode} from API ({rbody})", rbody, rcode, headers=rheaders From 78515ab731c5608c6fa33ee1247a8989adb07b42 Mon Sep 17 00:00:00 2001 From: zjzj1996 Date: Tue, 7 Mar 2023 11:15:55 +0800 Subject: [PATCH 2/8] fix:fix response_format bug --- openai/api_requestor.py | 7 ++++--- openai/api_resources/audio.py | 1 + openai/util.py | 22 +++++++++++++++------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/openai/api_requestor.py b/openai/api_requestor.py index 3b3c99260c..e785b3e02f 100644 --- a/openai/api_requestor.py +++ b/openai/api_requestor.py @@ -29,7 +29,7 @@ import openai from openai import error, util, version from openai.openai_response import OpenAIResponse -from openai.util import ApiType +from openai.util import ApiType, ResponseFormat TIMEOUT_SECS = 600 MAX_CONNECTION_RETRIES = 2 @@ -125,6 +125,7 @@ def __init__( api_type=None, api_version=None, organization=None, + response_format=None ): self.api_base = api_base or openai.api_base self.api_key = key or util.default_api_key() @@ -135,7 +136,7 @@ def __init__( ) self.api_version = api_version or openai.api_version self.organization = organization or openai.organization - + self.response_format = response_format @classmethod def format_app_info(cls, info): str = info["name"] @@ -666,7 +667,7 @@ def _interpret_response_line( headers=rheaders, ) try: - if rheaders.get('Content-Type') == 'application/json': + if self.response_format == ResponseFormat.JSON or self.response_format == ResponseFormat.VERBOSE_JSON: data = json.loads(rbody) else: data = rbody diff --git a/openai/api_resources/audio.py b/openai/api_resources/audio.py index 8ad6705680..1c253ecc88 100644 --- a/openai/api_resources/audio.py +++ b/openai/api_resources/audio.py @@ -31,6 +31,7 @@ def _prepare_request( api_type=api_type, api_version=api_version, organization=organization, + response_format=params["response_format"] ) files: List[Any] = [] data = { diff --git a/openai/util.py b/openai/util.py index 56dfc2e677..923f2323b1 100644 --- a/openai/util.py +++ b/openai/util.py @@ -99,12 +99,12 @@ def get_object_classes(): def convert_to_openai_object( - resp, - api_key=None, - api_version=None, - organization=None, - engine=None, - plain_old_data=False, + resp, + api_key=None, + api_version=None, + organization=None, + engine=None, + plain_old_data=False, ): # If we get a OpenAIResponse, we'll want to return a OpenAIObject. @@ -124,7 +124,7 @@ def convert_to_openai_object( for i in resp ] elif isinstance(resp, dict) and not isinstance( - resp, openai.openai_object.OpenAIObject + resp, openai.openai_object.OpenAIObject ): resp = resp.copy() klass_name = resp.get("object") @@ -186,3 +186,11 @@ def default_api_key() -> str: raise openai.error.AuthenticationError( "No API key provided. You can set your API key in code using 'openai.api_key = ', or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = '. You can generate API keys in the OpenAI web interface. See https://onboard.openai.com for details, or email support@openai.com if you have any questions." ) + + +class ResponseFormat(Enum): + JSON = "json" + TEXT = "text" + SRT = "srt" + VERBOSE_JSON = "verbose_json" + VTT = "vtt" From f13a697356c133f1e9adcee41338284f2dbd19a7 Mon Sep 17 00:00:00 2001 From: zjzj1996 Date: Tue, 7 Mar 2023 11:41:07 +0800 Subject: [PATCH 3/8] fix:fix response_format bug --- openai/api_requestor.py | 8 +++----- openai/api_resources/audio.py | 1 - openai/util.py | 7 +------ 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/openai/api_requestor.py b/openai/api_requestor.py index e785b3e02f..c364a379b9 100644 --- a/openai/api_requestor.py +++ b/openai/api_requestor.py @@ -125,7 +125,6 @@ def __init__( api_type=None, api_version=None, organization=None, - response_format=None ): self.api_base = api_base or openai.api_base self.api_key = key or util.default_api_key() @@ -136,7 +135,6 @@ def __init__( ) self.api_version = api_version or openai.api_version self.organization = organization or openai.organization - self.response_format = response_format @classmethod def format_app_info(cls, info): str = info["name"] @@ -667,10 +665,10 @@ def _interpret_response_line( headers=rheaders, ) try: - if self.response_format == ResponseFormat.JSON or self.response_format == ResponseFormat.VERBOSE_JSON: - data = json.loads(rbody) - else: + if 'text/plain' in rheaders.get('Content-Type'): data = rbody + else: + data = json.loads(rbody) except (JSONDecodeError, UnicodeDecodeError) as e: raise error.APIError( f"HTTP code {rcode} from API ({rbody})", rbody, rcode, headers=rheaders diff --git a/openai/api_resources/audio.py b/openai/api_resources/audio.py index 1c253ecc88..8ad6705680 100644 --- a/openai/api_resources/audio.py +++ b/openai/api_resources/audio.py @@ -31,7 +31,6 @@ def _prepare_request( api_type=api_type, api_version=api_version, organization=organization, - response_format=params["response_format"] ) files: List[Any] = [] data = { diff --git a/openai/util.py b/openai/util.py index 923f2323b1..30f946fcb2 100644 --- a/openai/util.py +++ b/openai/util.py @@ -188,9 +188,4 @@ def default_api_key() -> str: ) -class ResponseFormat(Enum): - JSON = "json" - TEXT = "text" - SRT = "srt" - VERBOSE_JSON = "verbose_json" - VTT = "vtt" + From b0b5c7086149b3d56d4ea0422475d05f372c1685 Mon Sep 17 00:00:00 2001 From: zjzj1996 Date: Tue, 7 Mar 2023 11:41:54 +0800 Subject: [PATCH 4/8] fix:fix response_format bug --- openai/api_requestor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openai/api_requestor.py b/openai/api_requestor.py index c364a379b9..93df803316 100644 --- a/openai/api_requestor.py +++ b/openai/api_requestor.py @@ -29,7 +29,7 @@ import openai from openai import error, util, version from openai.openai_response import OpenAIResponse -from openai.util import ApiType, ResponseFormat +from openai.util import ApiType TIMEOUT_SECS = 600 MAX_CONNECTION_RETRIES = 2 From 6e1b40885f0c2467cb98083077da7bcbfee06983 Mon Sep 17 00:00:00 2001 From: zjzj1996 Date: Tue, 7 Mar 2023 11:48:49 +0800 Subject: [PATCH 5/8] fix:fix response_format bug --- openai/util.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/openai/util.py b/openai/util.py index 30f946fcb2..75f1c4a793 100644 --- a/openai/util.py +++ b/openai/util.py @@ -99,12 +99,12 @@ def get_object_classes(): def convert_to_openai_object( - resp, - api_key=None, - api_version=None, - organization=None, - engine=None, - plain_old_data=False, + resp, + api_key=None, + api_version=None, + organization=None, + engine=None, + plain_old_data=False, ): # If we get a OpenAIResponse, we'll want to return a OpenAIObject. @@ -124,7 +124,7 @@ def convert_to_openai_object( for i in resp ] elif isinstance(resp, dict) and not isinstance( - resp, openai.openai_object.OpenAIObject + resp, openai.openai_object.OpenAIObject ): resp = resp.copy() klass_name = resp.get("object") @@ -185,7 +185,4 @@ def default_api_key() -> str: else: raise openai.error.AuthenticationError( "No API key provided. You can set your API key in code using 'openai.api_key = ', or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = '. You can generate API keys in the OpenAI web interface. See https://onboard.openai.com for details, or email support@openai.com if you have any questions." - ) - - - + ) \ No newline at end of file From 3c8dd356d493c2c426e0d9ea32093b66bce25707 Mon Sep 17 00:00:00 2001 From: zjzj1996 Date: Tue, 7 Mar 2023 12:25:28 +0800 Subject: [PATCH 6/8] fix:fix response_format bug --- openai/api_requestor.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/openai/api_requestor.py b/openai/api_requestor.py index 93df803316..43e66ec349 100644 --- a/openai/api_requestor.py +++ b/openai/api_requestor.py @@ -135,6 +135,7 @@ def __init__( ) self.api_version = api_version or openai.api_version self.organization = organization or openai.organization + @classmethod def format_app_info(cls, info): str = info["name"] @@ -691,4 +692,4 @@ async def aiohttp_session() -> AsyncIterator[aiohttp.ClientSession]: yield user_set_session else: async with aiohttp.ClientSession() as session: - yield session + yield session \ No newline at end of file From 03c65d6220d485cac99db05e831d538beb62ade0 Mon Sep 17 00:00:00 2001 From: zjzj1996 Date: Tue, 7 Mar 2023 12:38:36 +0800 Subject: [PATCH 7/8] fix:fix response_format bug --- openai/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openai/util.py b/openai/util.py index 75f1c4a793..56dfc2e677 100644 --- a/openai/util.py +++ b/openai/util.py @@ -185,4 +185,4 @@ def default_api_key() -> str: else: raise openai.error.AuthenticationError( "No API key provided. You can set your API key in code using 'openai.api_key = ', or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = '. You can generate API keys in the OpenAI web interface. See https://onboard.openai.com for details, or email support@openai.com if you have any questions." - ) \ No newline at end of file + ) From 047e7ff2ded9469375aee3b3a418834dd34ae00d Mon Sep 17 00:00:00 2001 From: zjzj1996 Date: Tue, 7 Mar 2023 12:39:43 +0800 Subject: [PATCH 8/8] fix:fix response_format bug --- openai/api_requestor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openai/api_requestor.py b/openai/api_requestor.py index 43e66ec349..8a0e6cabc2 100644 --- a/openai/api_requestor.py +++ b/openai/api_requestor.py @@ -692,4 +692,4 @@ async def aiohttp_session() -> AsyncIterator[aiohttp.ClientSession]: yield user_set_session else: async with aiohttp.ClientSession() as session: - yield session \ No newline at end of file + yield session