广州市综治平台后端
xusd
1 days ago 3feed5879e5a9b794862bf46d38c7f66cfe41d50
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
package cn.huge.module.ai;
 
import cn.huge.base.common.utils.HttpClientUtils;
import cn.huge.base.common.utils.ReturnFailUtils;
import cn.huge.base.common.utils.ReturnSucUtils;
import cn.huge.module.ai.domain.AiBodyDto;
import cn.huge.module.ai.domain.AiRequestVo;
import cn.huge.module.sy.domain.po.SyTimeLimit;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.net.http.HttpClient;
import java.util.HashMap;
import java.util.Map;
 
@RestController
@RequestMapping("/api/ai/chat")
public class AiChatController {
 
    private String url = "http://10.202.1.102:9002/";
 
    @PostMapping("/risk")
    public String risk(@RequestBody AiRequestVo aiRequestVo) {
        String postUrl = url + "v1/workflows/run";
        initHead("Bearer app-8ylvZOHf2fOgTGfe28MYtHDD");
        String s = "";
        try {
            s = HttpClientUtils.httpPostRaw(postUrl, JSON.toJSONString(aiRequestVo), initHead("Bearer app-8ylvZOHf2fOgTGfe28MYtHDD"), "utf-8");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        JSONObject object = JSONObject.parseObject(s);
        if (object != null) {
            JSONObject data = object.getJSONObject("data");
            if (data != null) {
                JSONObject outputs = data.getJSONObject("outputs");
                if (outputs != null) {
                    JSONArray result = outputs.getJSONArray("result");
                    if (result != null && result.size() > 0) {
                        return result.getJSONObject(0).getString("is_risk_ai");
                    }
                }
            }
        }
        return "0";
    }
 
    @PostMapping("/chat")
    public AiBodyDto chat(@RequestBody AiRequestVo aiRequestVo) {
        String postUrl = url + "v1/chat-messages";
        String s = "";
        AiBodyDto aiBodyDto = new AiBodyDto();
        try {
            s = HttpClientUtils.httpPostRaw(postUrl, JSON.toJSONString(aiRequestVo), initHead("Bearer app-8naZjGyj8a129EuUP2Jb6n7T"), "utf-8");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        JSONObject object = JSONObject.parseObject(s);
        if (object != null) {
            String conversation_id = object.getString("conversation_id");
            String answer = object.getString("answer");
            aiBodyDto.setAnswer(answer);
            aiBodyDto.setConversationId(conversation_id);
        }
        return aiBodyDto;
    }
 
    @PostMapping("/strategy")
    public AiBodyDto strategy(@RequestBody AiRequestVo aiRequestVo) {
        String postUrl = url + "v1/workflows/run";
        String s = "";
        AiBodyDto aiBodyDto = new AiBodyDto();
        try {
            s = HttpClientUtils.httpPostRaw(postUrl, JSON.toJSONString(aiRequestVo), initHead("Bearer app-WMriHFsAozx0FECkopW2Z6X1"), "utf-8");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        JSONObject object = JSONObject.parseObject(s);
        if (object != null) {
            JSONObject data = object.getJSONObject("data");
            if (data != null) {
                JSONObject outputs = data.getJSONObject("outputs");
                if (outputs != null) {
                    String text = outputs.getString("text");
                    aiBodyDto.setAnswer(text);
                }
            }
        }
        return aiBodyDto;
    }
 
    private Map<String, String> initHead(String token) {
        Map<String, String> head = new HashMap<>();
        head.put("Content-Type", "application/json");
        head.put("Authorization", token);
        return head;
    }
}