From 5a11a870e2abb5201f62c253ca811e52035864ed Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期五, 10 一月 2025 18:25:13 +0800
Subject: [PATCH] 知识库选择返回自己创建的
---
app/api/report.py | 121 ++++++++++++++++++++++++++++++++-------
1 files changed, 98 insertions(+), 23 deletions(-)
diff --git a/app/api/report.py b/app/api/report.py
index 386dcf1..00f80d9 100644
--- a/app/api/report.py
+++ b/app/api/report.py
@@ -4,13 +4,16 @@
import asyncio
import websockets
from sqlalchemy.orm import Session
-from app.api import get_current_user_websocket, ResponseList, get_current_user
+
+from Log import logger
+from app.api import get_current_user_websocket, ResponseList, get_current_user, format_file_url, process_files
from app.config.config import settings
+from app.models import MenuCapacityModel
from app.models.agent_model import AgentModel, AgentType
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()
@@ -21,11 +24,18 @@
chat_id: str,
current_user: UserModel = Depends(get_current_user_websocket),
db: Session = Depends(get_db)):
- agent = db.query(AgentModel).filter(AgentModel.id == agent_id).first()
+ agent = db.query(MenuCapacityModel).filter(MenuCapacityModel.chat_id == agent_id).first()
+ if not agent:
+ agent = db.query(AgentModel).filter(AgentModel.id == agent_id).first()
+ agent_type = agent.agent_type
+ chat_type = agent.type
+ else:
+ agent_type = agent.capacity_type
+ chat_type = agent.chat_type
if not agent:
ret = {"message": "Agent not found", "type": "close"}
return websocket.send_json(ret)
- agent_type = agent.agent_type
+ # agent_type = agent.agent_type
if chat_id == "" or chat_id == "0":
ret = {"message": "Chat ID not found", "type": "close"}
return websocket.send_json(ret)
@@ -34,8 +44,8 @@
ret = {"message": "Agent error", "type": "close"}
return websocket.send_json(ret)
- token = get_bisheng_token(db, current_user.id)
- service_uri = f"{settings.bisheng_websocket_url}/api/v1/chat/{agent_id}?type=L1&t=&chat_id={chat_id}"
+ token = await get_bisheng_token(db, current_user.id)
+ service_uri = f"{settings.sgb_websocket_url}/api/v1/chat/{agent_id}?type=L1&t=&chat_id={chat_id}"
headers = {'cookie': f"access_token_cookie={token};"}
await websocket.accept()
@@ -57,20 +67,70 @@
# 鐩戝惉姣曟槆鍙戞潵鐨勬秷鎭苟杞彂缁欏鎴风
async def forward_to_client():
+ is_answer = False
while True:
- message = await service_websocket.recv()
- print(f"Received from bisheng: {message}")
- data = json.loads(message)
- files = data.get("files", [])
- steps = data.get("intermediate_steps", "")
- if len(files) != 0 or steps != "" or data["type"] == "close":
- if data["type"] == "close":
- t = "close"
+ try:
+ message = await service_websocket.recv()
+ # print(f"Received from bisheng: {message}")
+ data = json.loads(message)
+ files = data.get("files", [])
+ steps = data.get("intermediate_steps", "")
+ msg = data.get("message", "")
+ category = data.get("category", "")
+ process_files(files, agent_id)
+ if category == "question" and steps:
+ is_answer = False
+ if not steps:
+ steps = "\n"
+ else:
+ steps = steps + "\n"
+ result = {"message": steps, "type": "stream", "files": files}
+ await websocket.send_json(result)
+ if category == "answer" and not is_answer:
+ if not steps.endswith("\n"):
+ steps += "\n\n"
+ result = {"message": steps, "type": "stream", "files": files}
+ await websocket.send_json(result)
+ if category == "answer" and is_answer:
+ # process_files(files, agent_id)
+ result = {"message": "\n", "type": "stream", "files": files}
+ await websocket.send_json(result)
+ elif data["type"] == "close":
+ # process_files(files, agent_id)
+ result = {"message": "", "type": "close", "files": files}
+ await websocket.send_json(result)
+ elif category == "processing":
+ # process_files(files, agent_id)
+ is_answer = True
+ result = {"message": msg, "type": "stream", "files": files}
+ await websocket.send_json(result)
+ elif files:
+ # process_files(files, agent_id)
+ result = {"message": "", "type": "stream", "files": files}
+ await websocket.send_json(result)
+
+ elif category == "system" and steps:
+ result = {"message": steps, "type": "stream", "files": files}
+ await websocket.send_json(result)
else:
- t = "stream"
- result = {"step_message": steps, "type": t, "files": files}
- await websocket.send_json(result)
- print(f"Forwarded to client, {chat_id}: {result}")
+ logger.error("-------------------11111111111111--------------------------")
+ logger.error(data)
+ except Exception as e:
+ logger.error(e)
+ await websocket.send_json({"message": "杩炴帴寮傚父锛�", "type": "close", "files": []})
+ # 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 = {"message": msg, "type": t, "files": files}
+ # await websocket.send_json(result)
+ # elif steps and last_message == "step":
+ # result = {"step_message": steps, "type": "stream", "files": files}
+ # await websocket.send_json(result)
+
+ # last_message = "message" if msg else "step"
# 鍚姩涓や釜浠诲姟锛屽垎鍒鐞嗗鎴风鍜屾湇鍔$鐨勬秷鎭�
tasks = [
@@ -84,11 +144,26 @@
task.cancel()
try:
await task
- except asyncio.CancelledError:
+ except asyncio.CancelledError as e:
+ print(f"asyncio CancelledError: {e}")
pass
- except WebSocketDisconnect:
- print(f"Client {chat_id} disconnected")
+ except WebSocketDisconnect as e:
+ print(f"WebSocket connection closed with code {e.code}: {e.reason}")
+ await websocket.close()
+ await service_websocket.close()
+ except Exception as e:
+ print(f"Exception occurred: {e}")
+ finally:
+ print("Cleaning up resources of bisheng report")
+ # 鍙栨秷鎵�鏈変换鍔�
+ for task in tasks:
+ if not task.done():
+ task.cancel()
+ try:
+ await task
+ except asyncio.CancelledError:
+ pass
@router.get("/variables/list", response_model=ResponseList)
@@ -96,9 +171,9 @@
agent = db.query(AgentModel).filter(AgentModel.id == agent_id).first()
if not agent:
return ResponseList(code=404, msg="Agent not found")
- bisheng_service = BishengService(base_url=settings.bisheng_base_url)
+ bisheng_service = BishengService(base_url=settings.sgb_base_url)
try:
- token = get_bisheng_token(db, current_user.id)
+ token = await get_bisheng_token(db, current_user.id)
result = await bisheng_service.variable_list(token, agent_id)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
--
Gitblit v1.8.0