广州市综治平台后端
xusd
10 days ago e4420b4ce607a043fd53af34966d86c39157d1b6
dyh-service/dyh-sys/src/main/java/cn/huge/module/ai/AiChatController.java
New file
@@ -0,0 +1,109 @@
package cn.huge.module.ai;
import cn.huge.base.common.bo.R;
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 lombok.extern.slf4j.Slf4j;
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/web/ai/chat")
@Slf4j
public class AiChatController {
    private String url = "http://10.202.1.102:9002/";
    @PostMapping("/risk")
    public R<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) {
                    JSONObject body = outputs.getJSONObject("body");
                    if (body != null) {
                        return R.ok(body.getJSONObject("data").getString("is_risk_ai"));
                    }
                }
            }
        }
        log.info("xsd:{}", object);
        return R.ok("0");
    }
    @PostMapping("/chat")
    public R<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);
        }
        log.info("xsd:{}", aiBodyDto);
        return R.ok(aiBodyDto);
    }
    @PostMapping("/strategy")
    public R<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);
                }
            }
        }
        log.info("xsd:{}", aiBodyDto);
        return R.ok(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;
    }
}