From 612d51a591abf0745d8186516f322a8944449ba7 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期六, 12 十月 2024 20:46:56 +0800
Subject: [PATCH] 处理异常
---
app/service/ragflow.py | 9 ++++++---
app/api/chat.py | 24 +++++++++++++-----------
2 files changed, 19 insertions(+), 14 deletions(-)
diff --git a/app/api/chat.py b/app/api/chat.py
index 1fe7ad6..471a4e6 100644
--- a/app/api/chat.py
+++ b/app/api/chat.py
@@ -47,17 +47,19 @@
message = await websocket.receive_json()
print(f"Received from client {chat_id}: {message}")
async for rag_response in ragflow_service.chat(token, chat_id, message["chatHistory"]):
- 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:
- 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}")
-
+ try:
+ 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:
+ 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:
+ print(f"Error forwarding message to ragflow: {e}")
# 鍚姩浠诲姟澶勭悊瀹㈡埛绔秷鎭�
tasks = [
asyncio.create_task(forward_to_ragflow())
diff --git a/app/service/ragflow.py b/app/service/ragflow.py
index 5fbe175..df131f1 100644
--- a/app/service/ragflow.py
+++ b/app/service/ragflow.py
@@ -51,8 +51,11 @@
# 妫�鏌ュ搷搴旂姸鎬佺爜
if response.status_code == 200:
# 娴佸紡璇诲彇鍝嶅簲
- async for answer in response.aiter_text():
- yield answer
+ try:
+ async for answer in response.aiter_text():
+ yield answer
+ except GeneratorExit as e:
+ print(e)
+ return
else:
yield f"Error: {response.status_code}"
-
--
Gitblit v1.8.0