suerprisePlus
2024-09-09 623cee8be4846e5762ff2949e519335ef8dee2bb
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
package com.yb.controller;
 
 
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.yb.config.R;
import com.yb.config.XzConfig;
import com.yb.service.AgentService;
import com.yb.service.IntentionService;
import com.yb.service.XzService;
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.HashMap;
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;
 
    @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");
//       判断msg是否为空
        if (StringUtils.isEmpty(msg)) {
            String info = agentService.getAgentIntention(xzConfig.agentUrl, xzConfig.agentIntent, "''");
            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");
            String funcInfo = agentService.getAgentIntention(xzConfig.agentUrl, xzConfig.agentIntent, msg);
            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);
 
            }
//          意图函数匹配
            HashMap<String, Object> mapObj = intentionService.getFunctionMatch(orderString);
            hashMap.put("content", mapObj);
            return R.ok(hashMap);
 
        } else {
            String ragInfo = agentService.getAgentIntention(xzConfig.agentUrl, xzConfig.agentChat, 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);
        }
 
 
    }
 
 
}