From 8c10231f0dd96175da799b1fe5efbfe25e81e563 Mon Sep 17 00:00:00 2001 From: yang_shj <1069818635@QQ.com> Date: Wed, 8 May 2024 13:40:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?#=E6=B0=B4=E7=94=B5=E7=AB=99=E5=8F=91?= =?UTF-8?q?=E7=94=B5=E9=87=8F=E5=A1=AB=E8=A1=A5=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hnac/hzims/operational/fill/service/impl/GenerateServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 Date: Wed, 8 May 2024 14:44:57 +0800 Subject: [PATCH 2/2] =?UTF-8?q?#=E5=A4=A7=E6=A8=A1=E5=9E=8B=E9=99=90?= =?UTF-8?q?=E5=88=B6=E9=97=AE=E9=A2=98=E5=AE=8C=E6=88=90=E4=B8=8D=E5=9C=A8?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E6=B6=88=E6=81=AF=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/InteractiveServiceImpl.java | 54 +++++++++++++--------- .../bigmodel/schedule/InteractiveSchedule.java | 51 +++++++++++--------- .../websocket/handler/InteractiveHandler.java | 3 +- 3 files changed, 61 insertions(+), 47 deletions(-) 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 d9e2efb..d3af689 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 @@ -4,20 +4,17 @@ import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; import com.hnac.hzims.bigmodel.configuration.BigModelInvokeUrl; import com.hnac.hzims.bigmodel.entity.FunctionEntity; +import com.hnac.hzims.bigmodel.function.service.IFunctionService; import com.hnac.hzims.bigmodel.interactive.req.ModelFunctionReq; import com.hnac.hzims.bigmodel.interactive.service.IInteractiveService; import com.hnac.hzims.bigmodel.interactive.service.IJumpPageService; -import com.hnac.hzims.bigmodel.function.service.IFunctionService; import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO; -import lombok.AllArgsConstructor; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springblade.core.log.exception.ServiceException; import org.springblade.core.log.logger.BladeLogger; -import org.springblade.core.secure.utils.AuthUtil; import org.springblade.core.tool.api.R; import org.springblade.core.tool.utils.CollectionUtil; import org.springblade.core.tool.utils.Func; @@ -29,15 +26,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 java.util.HashMap; -import java.util.List; -import java.util.Map; - -import static com.hnac.hzims.bigmodel.interactive.constants.FunctionConstants.*; +import static com.hnac.hzims.bigmodel.interactive.constants.FunctionConstants.TypeEnum; /** * @Author: huangxing * @Date: 2024/04/26 14:51 @@ -47,31 +39,39 @@ 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)); FunctionEntity function = functionService.getFunctionByCode(req.getFunctionName()); TypeEnum typeEnum = TypeEnum.getTypeEnumByType(function.getType()); - switch (typeEnum) { - // 页面跳转 - case JUMP: - String extra = jumpPageService.dealJumpTypeFunction(function, req.getFunctionArgs()); - Assert.isTrue(StringUtil.isNotBlank(extra) && Func.isNotEmpty(extra), () -> { - throw new ServiceException("解析" + function.getName() + "函数失败!"); - }); - return R.data(extra); - default: - throw new ServiceException("函数解析失败!"); + // 页面跳转 + if (Objects.requireNonNull(typeEnum) == TypeEnum.JUMP) { + String extra = jumpPageService.dealJumpTypeFunction(function, req.getFunctionArgs()); + Assert.isTrue(StringUtil.isNotBlank(extra) && Func.isNotEmpty(extra), () -> { + throw new ServiceException("解析" + function.getName() + "函数失败!"); + }); + return R.data(extra); } + throw new ServiceException("函数解析失败!"); } @Override @@ -79,7 +79,7 @@ public class InteractiveServiceImpl implements IInteractiveService { //TODO 保存问题 Map params = new HashMap<>(); params.put("id",sessionId); - params.put("userid",userId.toString()); + params.put("userid", userId); params.put("query",question); HttpResponse response = HttpRequest.post(fdpHost + bigModelInvokeUrl.getAssistantAsk()) .body(JSON.toJSONString(params)).execute(); @@ -87,6 +87,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");