From dc478b065693dd24e4cae719186d6aafb2d24f6d Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期一, 25 十一月 2024 10:05:10 +0800
Subject: [PATCH] difyq 接入
---
app/api/report.py | 25 +++++++++++++++++++------
1 files changed, 19 insertions(+), 6 deletions(-)
diff --git a/app/api/report.py b/app/api/report.py
index 254695d..f8d32fa 100644
--- a/app/api/report.py
+++ b/app/api/report.py
@@ -10,7 +10,7 @@
from app.models.base_model import get_db
from app.models.user_model import UserModel
from app.service.bisheng import BishengService
-from app.service.token import get_bisheng_token
+from app.service.service_token import get_bisheng_token
router = APIRouter()
@@ -65,24 +65,37 @@
files = data.get("files", [])
steps = data.get("intermediate_steps", "")
msg = data.get("message", "")
+ category = data.get("category", "")
- if len(files) != 0 or (steps and last_message == "step") or msg or data["type"] == "close":
+ if len(files) != 0 or (msg and category != "answer") or data["type"] == "close":
if data["type"] == "close":
t = "close"
else:
t = "stream"
process_files(files, agent_id)
- result = {"step_message": steps, "message": msg, "type": t, "files": files}
+ result = {"message": msg, "type": t, "files": files}
await websocket.send_json(result)
- print(f"Forwarded to client, {chat_id}: {result}")
+ elif steps and last_message == "step":
+ result = {"step_message": steps, "type": "stream", "files": files}
+ await websocket.send_json(result)
- last_message = "step" if steps else "message"
+ last_message = "message" if msg else "step"
# 鍚姩涓や釜浠诲姟锛屽垎鍒鐞嗗鎴风鍜屾湇鍔$鐨勬秷鎭�
tasks = [
asyncio.create_task(forward_to_service()),
asyncio.create_task(forward_to_client())
]
+ done, pending = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
+
+ # 鍙栨秷鏈畬鎴愮殑浠诲姟
+ for task in pending:
+ task.cancel()
+ try:
+ await task
+ except asyncio.CancelledError as e:
+ print(f"asyncio CancelledError: {e}")
+ pass
except WebSocketDisconnect as e:
print(f"WebSocket connection closed with code {e.code}: {e.reason}")
@@ -91,7 +104,7 @@
except Exception as e:
print(f"Exception occurred: {e}")
finally:
- print("Cleaning up resources")
+ print("Cleaning up resources of bisheng report")
# 鍙栨秷鎵�鏈変换鍔�
for task in tasks:
if not task.done():
--
Gitblit v1.8.0