250522
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
package com.gxwebsoft.law.controller;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.toolkit.sql.StringEscape;
|
||||
import com.gxwebsoft.common.core.utils.JSONUtil;
|
||||
import com.gxwebsoft.common.core.web.ApiResult;
|
||||
import com.gxwebsoft.common.core.web.BaseController;
|
||||
import com.gxwebsoft.law.config.WebSocketServer;
|
||||
import com.gxwebsoft.law.entity.ChatMessage;
|
||||
import com.gxwebsoft.law.entity.ChatResponse;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
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 javax.annotation.Resource;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Api(tags = "AI")
|
||||
@RestController
|
||||
@RequestMapping("/api/chat")
|
||||
public class AiController extends BaseController {
|
||||
@Resource
|
||||
private WebSocketServer webSocketServer;
|
||||
|
||||
@PostMapping("/message")
|
||||
public ApiResult<?> message(@RequestBody ChatMessage message) throws IOException {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("query", message.getQuery());
|
||||
params.put("user", getLoginUserId());
|
||||
params.put("response_mode", "streaming");
|
||||
String token = "Bearer app-UxV82WXIRrScpf53exkJ7dIw";
|
||||
if (message.getType() != null) {
|
||||
token = "Bearer app-7AFseF5UTEJpZGkW93S0wybh";
|
||||
}
|
||||
if (message.getInputs() != null) {
|
||||
Map<String, Object> inputs = new HashMap<>();
|
||||
inputs.put("request_type", message.getRequestType());
|
||||
inputs.put("request_json", message.getInputs());
|
||||
params.put("inputs", inputs);
|
||||
} else {
|
||||
params.put("inputs", new HashMap<>());
|
||||
}
|
||||
// 使用 Java 自带的 HttpURLConnection 发送流式请求
|
||||
try {
|
||||
URL url = new URL("http://workflow.gxshucheng.com:8010/v1/chat-messages");
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Authorization", token);
|
||||
connection.setRequestProperty("Content-Type", "application/json");
|
||||
connection.setDoOutput(true);
|
||||
connection.setConnectTimeout(600000);
|
||||
connection.setReadTimeout(600000);
|
||||
|
||||
// 写入请求体
|
||||
try (OutputStream os = connection.getOutputStream()) {
|
||||
os.write(JSONUtil.toJSONString(params).getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
StringBuilder responseStr = new StringBuilder();
|
||||
// 读取响应流
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
System.out.println("Received chunk: " + line); // 打印接收到的每一部分数据
|
||||
// 这里可以对每一部分数据进行处理,例如解析或发送给前端
|
||||
if (!line.isEmpty()) {
|
||||
String[] dataList = line.split("data: ");
|
||||
if (dataList.length == 2) {
|
||||
// System.out.println(dataList[1]);
|
||||
Map data = JSONUtil.parseObject(dataList[1], Map.class);
|
||||
if (data.get("event") != null && data.get("event").equals("message")) {
|
||||
String answer = (String) data.get("answer");
|
||||
String task_id = (String) data.get("task_id");
|
||||
if (answer != null && !answer.isEmpty()) {
|
||||
HashMap<String, String> answerData = new HashMap<>();
|
||||
answerData.put("answer", answer);
|
||||
answerData.put("taskId", task_id);
|
||||
webSocketServer.sendMessage(message.getUser(), JSONUtil.toJSONString(answerData));
|
||||
}
|
||||
System.out.println(answer);
|
||||
responseStr.append(answer);
|
||||
}else if (data.get("event") != null && data.get("event").equals("message_end")) {
|
||||
String task_id = (String) data.get("task_id");
|
||||
HashMap<String, String> answerData = new HashMap<>();
|
||||
answerData.put("answer", "__END__");
|
||||
answerData.put("taskId", task_id);
|
||||
webSocketServer.sendMessage(message.getUser(), JSONUtil.toJSONString(answerData));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
for (StackTraceElement stackTraceElement : e.getStackTrace()) {
|
||||
System.out.println(stackTraceElement);
|
||||
}
|
||||
webSocketServer.sendMessage(message.getUser(), "出错了,请晚点再来提问吧~");
|
||||
return fail("出错了,请晚点再来提问吧~");
|
||||
}
|
||||
|
||||
// 返回成功响应
|
||||
return success("Stream processing completed");
|
||||
}
|
||||
|
||||
@PostMapping("/messageStop")
|
||||
public ApiResult<?> stop(@RequestBody Map<String, Object> data) {
|
||||
if (data.get("taskId") == null) return success();
|
||||
String taskId = data.get("taskId").toString();
|
||||
Map<String, Integer> postData = new HashMap<>();
|
||||
postData.put("user", getLoginUserId());
|
||||
String token = "Bearer app-UxV82WXIRrScpf53exkJ7dIw";
|
||||
if (data.get("type") != null) {
|
||||
token = "Bearer app-7AFseF5UTEJpZGkW93S0wybh";
|
||||
}
|
||||
String res = HttpRequest.post("http://workflow.gxshucheng.com:8010/v1/chat-messages/" + taskId + "/stop")
|
||||
.header("Authorization", token)
|
||||
.header("Content-Type", "application/json")
|
||||
.body(JSONObject.toJSONString(postData))
|
||||
.execute().body();
|
||||
System.out.println("stop res:" + res);
|
||||
return success();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user