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 initHead(String token) { Map head = new HashMap<>(); head.put("Content-Type", "application/json"); head.put("Authorization", token); return head; } }