From 5d535b127cffb34affcb5d2a15fb3ddd4aa7e5ba Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期一, 14 十月 2024 11:27:06 +0800
Subject: [PATCH] 处理异常返回错误

---
 app/api/chat.py |   12 +++++++++---
 1 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/app/api/chat.py b/app/api/chat.py
index 471a4e6..9bd22b9 100644
--- a/app/api/chat.py
+++ b/app/api/chat.py
@@ -51,14 +51,20 @@
                             print(f"Received from ragflow: {rag_response}")
                             json_str = rag_response[5:].strip()
                             json_data = json.loads(json_str)
-                            if json_data.get("data") is not True:
+                            data = json_data.get("data")
+                            if data is True:  # 瀹屾垚杈撳嚭
+                                result = {"message": "", "type": "close"}
+                            elif data is None:  # 鍙戠敓閿欒
+                                answer = json_data.get("retmsg", json_data.get("retcode"))
+                                result = {"message": "鍐呴儴閿欒锛�" + answer, "type": "stream"}
+                            else:  # 姝e父杈撳嚭
                                 answer = json_data.get("data", {}).get("answer", "")
                                 result = {"message": answer, "type": "stream"}
-                            else:
-                                result = {"message": "", "type": "close"}
                             await websocket.send_json(result)
                             print(f"Forwarded to client {chat_id}: {result}")
                         except Exception as e:
+                            result = {"message": f"鍐呴儴閿欒锛� {e}", "type": "close"}
+                            await websocket.send_json(result)
                             print(f"Error forwarding message to ragflow: {e}")
             # 鍚姩浠诲姟澶勭悊瀹㈡埛绔秷鎭�
             tasks = [

--
Gitblit v1.8.0