package com.bwie.controller; import cn.hutool.core.util.StrUtil; import com.bwie.common.MsgDTO; import com.bwie.component.XfXhStreamClient; import com.bwie.config.XfXhConfig; import com.bwie.listener.XfXhWebSocketListener; import lombok.extern.slf4j.Slf4j; import org.java_websocket.WebSocket; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.Collections; import java.util.UUID; /** * @author 冯凯 * @version 1.0 * @description: TODO * @date 2023/9/29 14:06 */ @RestController @RequestMapping("/test") @Slf4j public class ChatController { @Resource private XfXhStreamClient xfXhStreamClient; @Resource private XfXhConfig xfXhConfig; /** * 发送问题 * * @param question 问题 * @return 星火大模型的回答 */ @GetMapping("/sendQuestion") public String sendQuestion(@RequestParam("question") String question) { // 如果是无效字符串,则不对大模型进行请求 if (StrUtil.isBlank(question)) { return "无效问题,请重新输入"; } // 获取连接令牌 if (!xfXhStreamClient.operateToken(XfXhStreamClient.GET_TOKEN_STATUS)) { return "当前大模型连接数过多,请稍后再试"; } // 创建消息对象 MsgDTO msgDTO = MsgDTO.createUserMsg(question); // 创建监听器 XfXhWebSocketListener listener = new XfXhWebSocketListener(); // 发送问题给大模型,生成 websocket 连接 WebSocket webSocket = xfXhStreamClient.sendMsg(UUID.randomUUID().toString().substring(0, 10), Collections.singletonList(msgDTO), listener); if (webSocket == null) { // 归还令牌 xfXhStreamClient.operateToken(XfXhStreamClient.BACK_TOKEN_STATUS); return "系统内部错误,请联系管理员"; } try { int count = 0; // 为了避免死循环,设置循环次数来定义超时时长 int maxCount = xfXhConfig.getMaxResponseTime() * 5; while (count <= maxCount) { Thread.sleep(200); if (listener.isWsCloseFlag()) { break; } count++; } if (count > maxCount) { return "大模型响应超时,请联系管理员"; } // 响应大模型的答案 System.out.println(listener.getAnswer().toString()); return listener.getAnswer().toString(); } catch (InterruptedException e) { log.error("错误:" + e.getMessage()); return "系统内部错误,请联系管理员"; } finally { // 关闭 websocket 连接 webSocket.close(1000, ""); // 归还令牌 xfXhStreamClient.operateToken(XfXhStreamClient.BACK_TOKEN_STATUS); } } }