suerprisePlus
2024-12-17 d4e4ea6405756d495806a609e24ae85ebd7345ef
src/main/java/com/yb/controller/agentController.java
@@ -2,14 +2,17 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonParser;
import com.yb.config.R;
import com.yb.config.XzConfig;
import com.yb.service.AgentService;
import com.yb.service.XzService;
import com.yb.service.IntentionService;
import com.yb.util.EntityHttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
@@ -20,7 +23,9 @@
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Tag(name = "Agent意图")
@@ -33,9 +38,11 @@
    @Autowired
    private XzConfig xzConfig;
    @Autowired
    private XzService xzService;
    @Autowired
    private AgentService agentService;
    @Autowired
    private IntentionService intentionService;
    @Autowired
    private EntityHttpUtil entityHttpUtil;
    @ApiOperation("意图识别")
    @PostMapping("/intention")
@@ -43,15 +50,12 @@
    public R getIntention(@RequestParam Map<String, String> allParams) throws Exception {
        System.out.println("rec getMessage:" + allParams);
        String msg = allParams.get("message");
        String sessionId = allParams.get("session_id");
        String libId = allParams.get("lib_id");
        String url = xzConfig.agentUrl;
        String intent = xzConfig.agentIntent;
        String rag = xzConfig.agentRag;
        String chart = xzConfig.agentChat;
        String sessionid = allParams.get("session_id");
        String libid = allParams.get("lib_id");
//       判断msg是否为空
        if (StringUtils.isEmpty(msg)) {
            String info = agentService.getAgentIntention(url, intent, "''");
            String info = agentService.getAgentIntention(xzConfig.agentUrl, xzConfig.agentIntent, "''",sessionid,libid);
            JSONObject obj = JSONObject.parseObject(info);
            String cont = obj.getString("content");
            System.out.println("rec isEmpty:" + cont);
@@ -63,11 +67,11 @@
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        if (isMap) {
            hashMap.put("type", "Map");
            HashMap<String, Object> mapObj = new HashMap<String, Object>();
            String funcInfo = agentService.getAgentIntention(url, intent, msg);
            hashMap.put("session_id", allParams.get("session_id"));
            String funcInfo = agentService.getAgentIntention(xzConfig.agentUrl, xzConfig.agentIntent, msg,sessionid,libid);
            JSONObject jsonObject = JSON.parseObject(funcInfo);
            String orderString = jsonObject.getString("order");
            if(orderString == null){
            if (orderString == null) {
                hashMap.put("type", "text");
                JSONObject errInfo = JSON.parseObject(funcInfo);
                String erroContent = errInfo.getString("content");
@@ -75,75 +79,95 @@
                return R.error(erroContent);
            }
            JSONObject orderObject = JSON.parseObject(orderString);
            String func = orderObject.getString("name");
            JSONObject argsObject = JSON.parseObject(orderObject.getString("args"));
            String place = argsObject.getString("place");
            String type = argsObject.getString("attris");
            String dis = argsObject.getString("redius");
            int radius = 5000;
            if (dis != null) {
                dis = dis.replace("m", "");
                radius = Integer.parseInt(dis);
            String report =jsonObject.getString("report");
//          意图函数匹配
            HashMap<String, Object> mapObj = intentionService.getFunctionMatch(orderString, allParams,report);
//            hashMap.put("place",place)
            String args = JSONObject.parseObject(orderString).getString("args");
            if (args != null) {
                String place = JSONObject.parseObject(args).getString("place");
                mapObj.put("place", place);
            }
            String queryFiled = "enti_name";
            String typeFiled = "sjnr";
            String func1 = "poiMap";
            String func2 = "aroundPoi";
            String func3 = "queryMeta";
            if (func.equals(func1)) {
                String poiMap = agentService.getAgentPoiMap(place, queryFiled);
                System.out.println("poiMap: " + poiMap);
                JSONObject poiObj = JSONObject.parseObject(poiMap);
                System.out.println("poiMap: " + poiObj);
                mapObj.put("func", func1);
                mapObj.put("data", poiObj);
            } else if (func.equals(func2)) {
                mapObj.put("func", func2);
                String aroundPoi = agentService.getAgentAroundPoi(place, type, radius, queryFiled, typeFiled);
                JSONArray dataArray = JSONArray.parseArray(aroundPoi);
                System.out.println("aroundPoi: " + dataArray);
                mapObj.put("data", dataArray);
            } else if (func.equals(func3)) {
                mapObj.put("func", func3);
                String filedString= xzService.getQueryMeta(xzConfig.layerId, xzConfig.dbid,typeFiled,xzConfig.queryEntity);
                JSONObject metaObject = JSON.parseObject(filedString);
                JSONArray dataArray = metaObject.getJSONArray("data");
                String markDown = "| 数据类型 | 数据量 |\\n|------|------|\\n";
                for (int i = 0; i < dataArray.size(); i++) {
                    JSONObject dataObj = dataArray.getJSONObject(i);
                    System.out.println("rec queryMeta:" + dataObj);
                    String[] filedValue = new String[2];
                    for (String key : dataObj.keySet()) {
                        if (key == typeFiled) {
                            filedValue[0] = dataObj.getString(key);
                        } else {
                            filedValue[1] = dataObj.getString(key);
                        }
                    }
                    markDown += "| " + filedValue[0] + " | " + filedValue[1] + " |\n";
                }
                mapObj.put("data", markDown);
            }
            hashMap.put("content", mapObj);
            return R.ok(hashMap);
        } else {
            String ragInfo = agentService.getAgentIntention(url, chart, msg);
            JSONObject jsonObject = JSON.parseObject(ragInfo);
            String answer = jsonObject.getString("content");
            System.out.println("Answer: " + answer);
            hashMap.put("content", answer);
            hashMap.put("type", "Rag");
            return R.ok(hashMap);
            String ragInfo1 = agentService.getAgentRag(xzConfig.agentUrl, xzConfig.agentRag, allParams.get("session_id"), allParams.get("lib_id"), msg);
            System.out.println("rec rag:" + ragInfo1);
            if (ragInfo1.equals("")) {
                String ragInfo = agentService.getAgentIntention(xzConfig.agentUrl, xzConfig.agentChat, msg,sessionid,libid);
                JSONObject jsonObject = JSON.parseObject(ragInfo);
                String answer = jsonObject.getString("content");
                System.out.println("Answer: " + answer);
                hashMap.put("content", answer);
                hashMap.put("type", "Rag");
                return R.ok(hashMap);
            } else {
                JSONObject jsonObject1 = JSON.parseObject(ragInfo1);
                String answer1 = jsonObject1.getString("content");
                System.out.println("Answer: " + answer1);
                String jso = JSONObject.parseObject(answer1).getString("answer");
                String context = JSONObject.parseObject(answer1).getString("context");
                if (context != "") {
                    hashMap.put("fileSrource", JSON.parseArray(context));
                }
                hashMap.put("content", jso);
                hashMap.put("type", "Rag");
                return R.ok(hashMap);
            }
        }
//        return R.error("未查询到相关信息");
    }
    @ApiOperation("获取图层列表")
    @GetMapping("/layerList")
    @Operation(summary = "获取图层列表")
    public R getLayerList() throws Exception {
        HashMap<String, String> formData = new HashMap<>();
        formData.put("token", xzConfig.token);
        formData.put("dbid", xzConfig.dbid);
        String obj = entityHttpUtil.getPostMessage(xzConfig.permissionLayer, formData);
        System.out.println("layerList: " + JSON.parseObject(JSONObject.parseObject(obj).getString("data")));
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        String layers = JSON.parseObject(JSONObject.parseObject(obj).getString("data")).getString("layers");
        JSONArray dataArray = JSONArray.parseArray(layers);
        ArrayList<HashMap> list = new ArrayList<>();
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject itmObj = JSONObject.parseObject(dataArray.get(i).toString());
            String item = itmObj.getString("dataformat");
            String id = itmObj.getString("id");
            if (item.equals("entity")) {
                HashMap<String, Object> itmHash = new HashMap<>();
                String name = itmObj.getString("name");
                String url = itmObj.getString("lod_url");
                if (url != null) {
                    String url3d = JSONObject.parseObject(url).getString("3d");
                    if (url3d != null) {
                        String l1 = JSONObject.parseObject(url3d).getString("l0");
                        itmHash.put("url", l1);
                        itmHash.put("sourceType", "Tileset");
                        itmHash.put("name", name);
                        itmHash.put("id", id);
                        list.add(itmHash);
                    }
                }
            }
        }
//            System.out.println("layerList: " + list);
        hashMap.put("data", list);
        return R.ok(hashMap);
    }
}