From d2c15e7806aa9e7609be3b175e3164e4955e56f8 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Wed, 8 May 2024 20:14:12 +0800 Subject: [PATCH 1/2] Refactor stream_response function to handle error responses and raise an exception --- devchat/assistant.py | 5 ++++- devchat/openai/http_openai.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/devchat/assistant.py b/devchat/assistant.py index 10a94f00..ec20e55a 100755 --- a/devchat/assistant.py +++ b/devchat/assistant.py @@ -104,7 +104,10 @@ def iterate_response(self) -> Iterator[str]: if self._chat.config.stream: created_time = int(time.time()) config_params = self._chat.config.dict(exclude_unset=True) - for chunk in self._chat.stream_response(self._prompt): + stream_responses = self._chat.stream_response(self._prompt) + if not stream_responses: + raise RuntimeError("No response returned from the chat API") + for chunk in stream_responses: try: if hasattr(chunk, "dict"): chunk = chunk.dict() diff --git a/devchat/openai/http_openai.py b/devchat/openai/http_openai.py index e5ebdacd..196d048f 100755 --- a/devchat/openai/http_openai.py +++ b/devchat/openai/http_openai.py @@ -36,7 +36,7 @@ def stream_response(connection: http.client.HTTPSConnection, data, headers): response = connection.getresponse() if response.status != 200: - print(f"Error: {response.status} - {response.reason}") + print(f"Error: {response.status} - {response.reason} {response.read()}", end="\n\n", file=sys.stderr) return None return LineReader(response=response) From ae9193310b0a140e8cd0393bb285d1a995759193 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Wed, 8 May 2024 20:17:08 +0800 Subject: [PATCH 2/2] Refactor error handling in stream_response function --- devchat/openai/http_openai.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/devchat/openai/http_openai.py b/devchat/openai/http_openai.py index 196d048f..c405b315 100755 --- a/devchat/openai/http_openai.py +++ b/devchat/openai/http_openai.py @@ -36,7 +36,8 @@ def stream_response(connection: http.client.HTTPSConnection, data, headers): response = connection.getresponse() if response.status != 200: - print(f"Error: {response.status} - {response.reason} {response.read()}", end="\n\n", file=sys.stderr) + print(f"Error: {response.status} - {response.reason} {response.read()}", + end="\n\n", file=sys.stderr) return None return LineReader(response=response)