From 5580958d49e5aab48908000614e47ecb75ff4797 Mon Sep 17 00:00:00 2001 From: zhaoqingang <zhaoqg0118@163.com> Date: 星期四, 28 十一月 2024 19:14:26 +0800 Subject: [PATCH] 智能数据问题优化 --- app/api/agent.py | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 45 insertions(+), 1 deletions(-) diff --git a/app/api/agent.py b/app/api/agent.py index 5842350..698ca0d 100644 --- a/app/api/agent.py +++ b/app/api/agent.py @@ -53,12 +53,18 @@ bisheng_service = BishengService(base_url=settings.sgb_base_url) try: token = get_bisheng_token(db, current_user.id) - result = await bisheng_service.get_chat_sessions(token, page, limit) + result = await bisheng_service.get_chat_sessions(token, agent_id, page, limit) except Exception as e: raise HTTPException(status_code=500, detail=str(e)) return ResponseList(code=200, msg="", data=result) elif agent.agent_type == AgentType.BASIC: + offset = (page - 1) * limit + records = db.query(SessionModel).filter(SessionModel.agent_id == agent_id, SessionModel.tenant_id==current_user.id).order_by(SessionModel.create_date.desc()).offset(offset).limit(limit).all() + result = [item.to_dict() for item in records] + return ResponseList(code=200, msg="", data=result) + + elif agent.agent_type == AgentType.DIFY: offset = (page - 1) * limit records = db.query(SessionModel).filter(SessionModel.agent_id == agent_id, SessionModel.tenant_id==current_user.id).order_by(SessionModel.create_date.desc()).offset(offset).limit(limit).all() result = [item.to_dict() for item in records] @@ -148,10 +154,47 @@ if i.get("role") == "user": tmp_data["question"]=i.get("content") elif i.get("role") == "assistant": + if isinstance(i.get("content"), dict): tmp_data["answer"] = i.get("content", {}).get("message") if "file_name" in i.get("content", {}): tmp_data["files"] = [{"file_name":i.get("content", {}).get("file_name"), "file_url":i.get("content", {}).get("file_url")}] + else: + tmp_data["answer"] = i.get("content") + + if "excel_url" in i: + tmp_data["excel_url"] = i.get("excel_url") + if "image_url" in i: + tmp_data["image_url"] = i.get("image_url") + if "sql" in i: + tmp_data["sql"] = i.get("sql") + if "code" in i: + tmp_data["code"] = i.get("code") + if "e" in i: + tmp_data["e"] = i.get("e") + data.append(tmp_data) + tmp_data = {} + + if tmp_data: + data.append(tmp_data) + + return JSONResponse(status_code=200, content={"code": 200, "data": data}) + elif agent.agent_type == AgentType.DIFY: + data = [] + session = db.query(SessionModel).filter(SessionModel.id == conversation_id).first() + if session: + tmp_data = {} + for i in session.log_to_json().get("message", []): + if i.get("role") == "user": + tmp_data["question"] = i.get("content") + elif i.get("role") == "assistant": + if isinstance(i.get("content"), dict): + 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"), + "file_url": i.get("content", {}).get("file_url")}] + if "images" in i.get("content", {}): + tmp_data["images"] = i.get("content", {}).get("images") else: tmp_data["answer"] = i.get("content") data.append(tmp_data) @@ -161,6 +204,7 @@ data.append(tmp_data) return JSONResponse(status_code=200, content={"code": 200, "data": data}) + else: return JSONResponse(status_code=200, content={"code": 200, "log": "Unsupported agent type"}) -- Gitblit v1.8.0