zhaoqingang
2024-11-25 1ab90de913405b45050e0f732a03004087134fda
app/api/agent.py
@@ -60,7 +60,13 @@
    elif agent.agent_type == AgentType.BASIC:
        offset = (page - 1) * limit
        records = db.query(SessionModel).filter(SessionModel.agent_id == agent_id).offset(offset).limit(limit).all()
        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]
        return ResponseList(code=200, msg="", data=result)
@@ -105,7 +111,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))
    if agent.agent_type == AgentType.BISHENG:
    elif agent.agent_type == AgentType.BISHENG:
        bisheng_service = BishengService(base_url=settings.sgb_base_url)
        try:
            token = get_bisheng_token(db, current_user.id)
@@ -139,6 +145,52 @@
            return JSONResponse(status_code=200, content={"code": 200, "data": combined_logs})
        except Exception as e:
            raise HTTPException(status_code=500, detail=str(e))
    elif agent.agent_type == AgentType.BASIC:
        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("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")
                    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")}]
                    else:
                        tmp_data["answer"] = i.get("content")
                    data.append(tmp_data)
                    tmp_data = {}
            if tmp_data:
                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"})