suerprisePlus
2024-10-18 1efa47bc58fe0673a231233f644d3a5f8277e42c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.yb.controller;
 
 
import com.alibaba.fastjson.JSON;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.IntentionService;
 
import com.yb.util.EntityHttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
 
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意图")
@RestController
@RequestMapping("/api/agent")
@Api(tags = "Agent意图")
@Component
public class agentController {
 
    @Autowired
    private XzConfig xzConfig;
    @Autowired
    private AgentService agentService;
    @Autowired
    private IntentionService intentionService;
    @Autowired
    private EntityHttpUtil entityHttpUtil;
 
    @ApiOperation("意图识别")
    @PostMapping("/intention")
    @Operation(summary = "意图识别")
    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");
//       判断msg是否为空
        if (StringUtils.isEmpty(msg)) {
            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);
            return R.error(cont);
//
        }
//       判断是否调用地图
        boolean isMap = msg.matches("(?i).*@map.*");
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        if (isMap) {
            hashMap.put("type", "Map");
            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) {
                hashMap.put("type", "text");
                JSONObject errInfo = JSON.parseObject(funcInfo);
                String erroContent = errInfo.getString("content");
                hashMap.put("content", erroContent);
                return R.error(erroContent);
 
            }
            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);
            }
            hashMap.put("content", mapObj);
            return R.ok(hashMap);
 
        } else {
 
            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);
            }
 
        }
 
 
    }
 
    @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: " + obj);
        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);
    }
 
 
}