diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/InteractiveServiceImpl.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/InteractiveServiceImpl.java index f0bf166..7498bd0 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/InteractiveServiceImpl.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/InteractiveServiceImpl.java @@ -30,10 +30,10 @@ import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.util.Assert; -import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; +import java.util.*; +import static com.hnac.hzims.bigmodel.interactive.constants.FunctionConstants.TypeEnum; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -49,15 +49,25 @@ import static com.hnac.hzims.bigmodel.interactive.constants.FunctionConstants.*; @RequiredArgsConstructor public class InteractiveServiceImpl implements IInteractiveService { + private final BladeLogger logger; + private final IJumpPageService jumpPageService; + private final IFunctionService functionService; + private final BigModelInvokeUrl bigModelInvokeUrl; - private final BladeLogger logger; + private final IDeptClient deptClient; + private final IUserClient userClient; + + private final RedisTemplate redisTemplate; + @Value("${fdp.host}") private String fdpHost; + private final static String HZIMS_BIGMODEL_ASK_KEY = "hzims:bigmodel:ask:key"; + @Override public R resolve(ModelFunctionReq req) { logger.info("interactive:resolve","开始解析大模型函数,函数内容为:" + JSON.toJSONString(req)); @@ -80,7 +90,7 @@ public class InteractiveServiceImpl implements IInteractiveService { public R ask(String question,String sessionId,String userId) { Map params = new HashMap<>(); params.put("id",sessionId); - params.put("userid",userId.toString()); + params.put("userid", userId); params.put("query",question); params.put("stationids",this.getStationPermissionsById(userId).stream().map(DeptStationDTO::getStationId).filter(StringUtil::isNotBlank).filter(Func::isNotEmpty).toArray()); params.put("projectids",this.getStationPermissionsById(userId).stream().map(DeptStationDTO::getDeptId).filter(Func::isNotEmpty).toArray()); @@ -90,6 +100,14 @@ public class InteractiveServiceImpl implements IInteractiveService { log.error("远程调用大模型【发起问答】接口失败!"); return R.fail("远程调用大模型【发起问答】接口失败!"); } + // 添加redis问题会话 + List asks = (List) redisTemplate.opsForValue().get(HZIMS_BIGMODEL_ASK_KEY); + if(CollectionUtil.isEmpty(asks)){ + asks = Collections.singletonList(sessionId); + }else{ + asks.add(sessionId); + } + redisTemplate.opsForValue().set(HZIMS_BIGMODEL_ASK_KEY,asks); return R.success("消息发送成功"); } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/schedule/InteractiveSchedule.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/schedule/InteractiveSchedule.java index 56af36e..13b6344 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/schedule/InteractiveSchedule.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/schedule/InteractiveSchedule.java @@ -3,31 +3,23 @@ package com.hnac.hzims.bigmodel.schedule; import com.alibaba.fastjson.JSON; import com.hnac.hzims.bigmodel.interactive.service.IInteractiveService; import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO; -import com.hnac.hzims.bigmodel.websocket.server.InteractiveWsServer; import com.hnac.hzims.bigmodel.websocket.service.InteractiveWsService; import com.hnac.hzims.bigmodel.websocket.sessionManager.InteractiveSessionManager; import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.handler.annotation.XxlJob; import com.xxl.job.core.log.XxlJobLogger; import lombok.AllArgsConstructor; -import org.springblade.core.log.exception.ServiceException; -import org.springblade.core.tool.utils.Func; -import org.springblade.system.cache.ParamCache; +import org.springblade.core.tool.utils.CollectionUtil; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; -import org.springframework.util.Assert; import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession; -import java.io.IOException; -import java.util.Enumeration; import java.util.List; -import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ThreadPoolExecutor; -import java.util.stream.Collectors; -import static com.hnac.hzims.bigmodel.schedule.XxlJobHandlerConstant.*; +import static com.hnac.hzims.bigmodel.schedule.XxlJobHandlerConstant.GET_INTERACTIVE_RESULT; /** * @Author: huangxing @@ -37,11 +29,16 @@ import static com.hnac.hzims.bigmodel.schedule.XxlJobHandlerConstant.*; @AllArgsConstructor public class InteractiveSchedule { - private final RedisTemplate redisTemplate; private final InteractiveWsService wsService; + private final IInteractiveService interactiveService; + private final ThreadPoolExecutor getAnswerPoolExecutor; + private final RedisTemplate redisTemplate; + + private final static String HZIMS_BIGMODEL_ASK_KEY = "hzims:bigmodel:ask:key"; + // @XxlJob(GET_INTERACTIVE_RESULT) // public ReturnT execute(String params) { // String resultKey = ParamCache.getValue(GET_INTERACTIVE_RESULT); @@ -68,17 +65,27 @@ public class InteractiveSchedule { @XxlJob(GET_INTERACTIVE_RESULT) public ReturnT execute(String params) { List sessionIds = InteractiveSessionManager.getSessionIds(); - XxlJobLogger.log("开始获取到sessionId:"+sessionIds.stream().collect(Collectors.joining(","))); - List AnswerList = interactiveService.getAnswerBySessionIds(sessionIds.stream().collect(Collectors.joining(","))); - XxlJobLogger.log("获取到答案:"+JSON.toJSONString(AnswerList)); - AnswerList.parallelStream().forEach(answerVO -> { - CompletableFuture.runAsync(() -> { - XxlJobLogger.log("开始获取"+answerVO.getSessionId()+"答案!"); - WebSocketSession session = InteractiveSessionManager.get(answerVO.getSessionId()); - TextMessage message = InteractiveSessionManager.getTextMessage("1",JSON.toJSONString(answerVO)); - wsService.sendMessage(session,message); - }, getAnswerPoolExecutor); - }); + XxlJobLogger.log("开始获取到sessionId:"+ String.join(",", sessionIds)); + List answerList = interactiveService.getAnswerBySessionIds(String.join(",", sessionIds)); + XxlJobLogger.log("获取到答案:"+JSON.toJSONString(answerList)); + answerList.parallelStream().forEach(answerVO -> CompletableFuture.runAsync(() -> { + XxlJobLogger.log("开始获取"+answerVO.getSessionId()+"答案!"); + List asks = (List) redisTemplate.opsForValue().get(HZIMS_BIGMODEL_ASK_KEY); + // 问题回答完成,去除redis问题标记 + if(answerVO.getStatus() == 0){ + if(CollectionUtil.isNotEmpty(asks) && asks.contains(answerVO.getSessionId())){ + WebSocketSession session = InteractiveSessionManager.get(answerVO.getSessionId()); + TextMessage message = InteractiveSessionManager.getTextMessage("1",JSON.toJSONString(answerVO)); + wsService.sendMessage(session,message); + } + asks.remove(answerVO.getSessionId()); + redisTemplate.opsForValue().set(HZIMS_BIGMODEL_ASK_KEY,asks); + return; + } + WebSocketSession session = InteractiveSessionManager.get(answerVO.getSessionId()); + TextMessage message = InteractiveSessionManager.getTextMessage("1",JSON.toJSONString(answerVO)); + wsService.sendMessage(session,message); + }, getAnswerPoolExecutor)); return ReturnT.SUCCESS; } } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/handler/InteractiveHandler.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/handler/InteractiveHandler.java index a2dc3c6..6c030fb 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/handler/InteractiveHandler.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/handler/InteractiveHandler.java @@ -56,8 +56,7 @@ public class InteractiveHandler extends TextWebSocketHandler { } catch (IOException e) { throw new RuntimeException(e); } - } - else { + }else { // 发送问题 String context = messageJSON.getString("context"); String userId = messageJSON.getString("userId"); diff --git a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/fill/service/impl/GenerateServiceImpl.java b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/fill/service/impl/GenerateServiceImpl.java index 2e170cd..bc33c70 100644 --- a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/fill/service/impl/GenerateServiceImpl.java +++ b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/fill/service/impl/GenerateServiceImpl.java @@ -60,7 +60,7 @@ public class GenerateServiceImpl extends BaseServiceImpl