From c4338024c90cd36a330d42194bb553525828fa3b Mon Sep 17 00:00:00 2001 From: zhaoqingang <zhaoqg0118@163.com> Date: 星期五, 14 二月 2025 16:28:10 +0800 Subject: [PATCH] 问题优化 --- app/api/agent.py | 49 +++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 43 insertions(+), 6 deletions(-) diff --git a/app/api/agent.py b/app/api/agent.py index fb1ad87..0a6429e 100644 --- a/app/api/agent.py +++ b/app/api/agent.py @@ -81,11 +81,15 @@ @router.get("/{agent_id}/{conversation_id}/session_log") async def session_log(agent_id: str, conversation_id: str, db: Session = Depends(get_db), current_user: UserModel = Depends(get_current_user)): - agent = db.query(AgentModel).filter(AgentModel.id == agent_id).first() + # agent = db.query(AgentModel).filter(AgentModel.id == agent_id).first() + # if not agent: + # return Response(code=404, msg="Agent not found") + agent = db.query(MenuCapacityModel).filter(MenuCapacityModel.chat_id == agent_id).first() if not agent: - return Response(code=404, msg="Agent not found") + return ResponseList(code=404, msg="Agent not found") + agent_type = int(agent.capacity_type) - if agent.agent_type == AgentType.RAGFLOW: + if agent_type == AgentType.RAGFLOW: ragflow_service = RagflowService(base_url=settings.fwr_base_url) try: token = await get_ragflow_token(db, current_user.id) @@ -116,7 +120,7 @@ return JSONResponse(status_code=200, content={"code": 400, "message": "Invalid result structure"}) except Exception as e: raise HTTPException(status_code=500, detail=str(e)) - elif agent.agent_type == AgentType.BISHENG: + elif agent_type == AgentType.BISHENG: is_join = False if agent.name == "鎶ュ憡鐢熸垚": is_join = True @@ -180,7 +184,7 @@ 'answer': answer_str, 'files': files}]}) except Exception as e: raise HTTPException(status_code=500, detail=str(e)) - elif agent.agent_type == AgentType.BASIC: + elif agent_type == AgentType.BASIC: data = [] session = db.query(SessionModel).filter(SessionModel.id == conversation_id).first() if session: @@ -219,7 +223,7 @@ data.append(tmp_data) return JSONResponse(status_code=200, content={"code": 200, "data": data}) - elif agent.agent_type == AgentType.DIFY: + elif agent_type == AgentType.DIFY: data = [] session = db.query(SessionModel).filter(SessionModel.id == conversation_id).first() if session: @@ -227,8 +231,11 @@ for i in session.log_to_json().get("message", []): if i.get("role") == "user": tmp_data["question"] = i.get("content") + if "upload_filenames" in i: + tmp_data["upload_filenames"] = i.get("upload_filenames") elif i.get("role") == "assistant": if isinstance(i.get("content"), dict): + content = i.get("content", {}) tmp_data["answer"] = i.get("content", {}).get("answer") if "file_name" in i.get("content", {}): tmp_data["files"] = [{"file_name": i.get("content", {}).get("file_name"), @@ -238,6 +245,36 @@ if "download_url" in i.get("content", {}): tmp_data["download_url"] = i.get("content", {}).get("download_url") + if "node_list" in content: + node_dict = { + "node_data": [], + # {"title": "鍘婚櫎鍐椾綑", # 鑺傜偣鍚嶇О "status": "succeeded", # 鑺傜偣鐘舵��"created_at": 1735817337, # 寮�濮嬫椂闂�"finished_at": 1735817337, # 缁撴潫鏃堕棿"error": "" # 閿欒鏃ュ織} + "total_tokens": 0, # 鑺辫垂token鏁� + "created_at": 0, # 寮�濮嬫椂闂� + "finished_at": 0, # 缁撴潫鏃堕棿 + "elapsed_time": 0, # 缁撴潫鏃堕棿 + "status": "succeeded", # 宸ヤ綔娴佺姸鎬� + "error": "", # 閿欒鏃ュ織 + } + for node in content["node_list"]: + if node.get("event") == "node_finished": + node_dict["node_data"].append({ + "title": node.get("data", {}).get("title", ""), + "status": node.get("data", {}).get("status", ""), + "created_at": node.get("data", {}).get("created_at", 0), + "finished_at": node.get("data", {}).get("finished_at", 0), + "node_type": node.get("data", {}).get("node_type", 0), + "elapsed_time": node.get("data", {}).get("elapsed_time", 0), + "error": node.get("data", {}).get("error", ""), + }) + elif node.get("event") == "workflow_finished": + node_dict["total_tokens"] = node.get("data", {}).get("total_tokens", 0) + node_dict["created_at"] = node.get("data", {}).get("created_at", 0) + node_dict["finished_at"] = node.get("data", {}).get("finished_at", 0) + node_dict["status"] = node.get("data", {}).get("status", "") + node_dict["error"] = node.get("data", {}).get("error", "") + node_dict["elapsed_time"] = node.get("data", {}).get("elapsed_time", 0) + tmp_data["workflow"] = node_dict else: tmp_data["answer"] = i.get("content") data.append(tmp_data) -- Gitblit v1.8.0