Browse Source

handle writing responses errors

Alex Cheema 10 months ago
parent
commit
b181f8aa82
1 changed files with 5 additions and 1 deletions
  1. 5 1
      exo/api/chatgpt_api.py

+ 5 - 1
exo/api/chatgpt_api.py

@@ -345,7 +345,11 @@ class ChatGPTAPI:
             "chat.completion",
           )
           if DEBUG >= 2: print(f"Streaming completion: {completion}")
-          await response.write(f"data: {json.dumps(completion)}\n\n".encode())
+          try:
+            await response.write(f"data: {json.dumps(completion)}\n\n".encode())
+          except Exception as e:
+            if DEBUG >= 2: print(f"Error streaming completion: {e}")
+            if DEBUG >= 2: traceback.print_exc()
 
         def on_result(_request_id: str, tokens: List[int], is_finished: bool):
           self.stream_tasks[request_id] = asyncio.create_task(stream_result(request_id, tokens, is_finished))