瀏覽代碼

handle writing responses errors

Alex Cheema 1 年之前
父節點
當前提交
b181f8aa82
共有 1 個文件被更改,包括 5 次插入1 次删除
  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))