Browse Source

#大模型限制问题完成不在进行消息推送

zhongwei
yang_shj 7 months ago
parent
commit
3b76a522a3
  1. 54
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/InteractiveServiceImpl.java
  2. 51
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/schedule/InteractiveSchedule.java
  3. 3
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/handler/InteractiveHandler.java

54
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 cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hnac.hzims.bigmodel.configuration.BigModelInvokeUrl; import com.hnac.hzims.bigmodel.configuration.BigModelInvokeUrl;
import com.hnac.hzims.bigmodel.entity.FunctionEntity; 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.req.ModelFunctionReq;
import com.hnac.hzims.bigmodel.interactive.service.IInteractiveService; import com.hnac.hzims.bigmodel.interactive.service.IInteractiveService;
import com.hnac.hzims.bigmodel.interactive.service.IJumpPageService; 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 com.hnac.hzims.bigmodel.interactive.vo.AnswerVO;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.exception.ServiceException; import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.log.logger.BladeLogger; 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.api.R;
import org.springblade.core.tool.utils.CollectionUtil; import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func; 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.stereotype.Service;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import java.util.*;
import java.util.HashMap; import static com.hnac.hzims.bigmodel.interactive.constants.FunctionConstants.TypeEnum;
import java.util.List;
import java.util.Map;
import static com.hnac.hzims.bigmodel.interactive.constants.FunctionConstants.*;
/** /**
* @Author: huangxing * @Author: huangxing
* @Date: 2024/04/26 14:51 * @Date: 2024/04/26 14:51
@ -47,31 +39,39 @@ import static com.hnac.hzims.bigmodel.interactive.constants.FunctionConstants.*;
@RequiredArgsConstructor @RequiredArgsConstructor
public class InteractiveServiceImpl implements IInteractiveService { public class InteractiveServiceImpl implements IInteractiveService {
private final BladeLogger logger;
private final IJumpPageService jumpPageService; private final IJumpPageService jumpPageService;
private final IFunctionService functionService; private final IFunctionService functionService;
private final BigModelInvokeUrl bigModelInvokeUrl; private final BigModelInvokeUrl bigModelInvokeUrl;
private final BladeLogger logger;
private final IDeptClient deptClient; private final IDeptClient deptClient;
private final IUserClient userClient; private final IUserClient userClient;
private final RedisTemplate redisTemplate;
@Value("${fdp.host}") @Value("${fdp.host}")
private String fdpHost; private String fdpHost;
private final static String HZIMS_BIGMODEL_ASK_KEY = "hzims:bigmodel:ask:key";
@Override @Override
public R resolve(ModelFunctionReq req) { public R resolve(ModelFunctionReq req) {
logger.info("interactive:resolve","开始解析大模型函数,函数内容为:" + JSON.toJSONString(req)); logger.info("interactive:resolve","开始解析大模型函数,函数内容为:" + JSON.toJSONString(req));
FunctionEntity function = functionService.getFunctionByCode(req.getFunctionName()); FunctionEntity function = functionService.getFunctionByCode(req.getFunctionName());
TypeEnum typeEnum = TypeEnum.getTypeEnumByType(function.getType()); TypeEnum typeEnum = TypeEnum.getTypeEnumByType(function.getType());
switch (typeEnum) { // 页面跳转
// 页面跳转 if (Objects.requireNonNull(typeEnum) == TypeEnum.JUMP) {
case JUMP: String extra = jumpPageService.dealJumpTypeFunction(function, req.getFunctionArgs());
String extra = jumpPageService.dealJumpTypeFunction(function, req.getFunctionArgs()); Assert.isTrue(StringUtil.isNotBlank(extra) && Func.isNotEmpty(extra), () -> {
Assert.isTrue(StringUtil.isNotBlank(extra) && Func.isNotEmpty(extra), () -> { throw new ServiceException("解析" + function.getName() + "函数失败!");
throw new ServiceException("解析" + function.getName() + "函数失败!"); });
}); return R.data(extra);
return R.data(extra);
default:
throw new ServiceException("函数解析失败!");
} }
throw new ServiceException("函数解析失败!");
} }
@Override @Override
@ -79,7 +79,7 @@ public class InteractiveServiceImpl implements IInteractiveService {
//TODO 保存问题 //TODO 保存问题
Map<String,String> params = new HashMap<>(); Map<String,String> params = new HashMap<>();
params.put("id",sessionId); params.put("id",sessionId);
params.put("userid",userId.toString()); params.put("userid", userId);
params.put("query",question); params.put("query",question);
HttpResponse response = HttpRequest.post(fdpHost + bigModelInvokeUrl.getAssistantAsk()) HttpResponse response = HttpRequest.post(fdpHost + bigModelInvokeUrl.getAssistantAsk())
.body(JSON.toJSONString(params)).execute(); .body(JSON.toJSONString(params)).execute();
@ -87,6 +87,14 @@ public class InteractiveServiceImpl implements IInteractiveService {
log.error("远程调用大模型【发起问答】接口失败!"); log.error("远程调用大模型【发起问答】接口失败!");
return R.fail("远程调用大模型【发起问答】接口失败!"); return R.fail("远程调用大模型【发起问答】接口失败!");
} }
// 添加redis问题会话
List<String> asks = (List<String>) 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("消息发送成功"); return R.success("消息发送成功");
} }

51
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.alibaba.fastjson.JSON;
import com.hnac.hzims.bigmodel.interactive.service.IInteractiveService; import com.hnac.hzims.bigmodel.interactive.service.IInteractiveService;
import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO; 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.service.InteractiveWsService;
import com.hnac.hzims.bigmodel.websocket.sessionManager.InteractiveSessionManager; import com.hnac.hzims.bigmodel.websocket.sessionManager.InteractiveSessionManager;
import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob; import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger; import com.xxl.job.core.log.XxlJobLogger;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springblade.core.log.exception.ServiceException; import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.cache.ParamCache;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.WebSocketSession;
import java.io.IOException;
import java.util.Enumeration;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor; 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 * @Author: huangxing
@ -37,11 +29,16 @@ import static com.hnac.hzims.bigmodel.schedule.XxlJobHandlerConstant.*;
@AllArgsConstructor @AllArgsConstructor
public class InteractiveSchedule { public class InteractiveSchedule {
private final RedisTemplate redisTemplate;
private final InteractiveWsService wsService; private final InteractiveWsService wsService;
private final IInteractiveService interactiveService; private final IInteractiveService interactiveService;
private final ThreadPoolExecutor getAnswerPoolExecutor; private final ThreadPoolExecutor getAnswerPoolExecutor;
private final RedisTemplate redisTemplate;
private final static String HZIMS_BIGMODEL_ASK_KEY = "hzims:bigmodel:ask:key";
// @XxlJob(GET_INTERACTIVE_RESULT) // @XxlJob(GET_INTERACTIVE_RESULT)
// public ReturnT execute(String params) { // public ReturnT execute(String params) {
// String resultKey = ParamCache.getValue(GET_INTERACTIVE_RESULT); // String resultKey = ParamCache.getValue(GET_INTERACTIVE_RESULT);
@ -68,17 +65,27 @@ public class InteractiveSchedule {
@XxlJob(GET_INTERACTIVE_RESULT) @XxlJob(GET_INTERACTIVE_RESULT)
public ReturnT execute(String params) { public ReturnT execute(String params) {
List<String> sessionIds = InteractiveSessionManager.getSessionIds(); List<String> sessionIds = InteractiveSessionManager.getSessionIds();
XxlJobLogger.log("开始获取到sessionId:"+sessionIds.stream().collect(Collectors.joining(","))); XxlJobLogger.log("开始获取到sessionId:"+ String.join(",", sessionIds));
List<AnswerVO> AnswerList = interactiveService.getAnswerBySessionIds(sessionIds.stream().collect(Collectors.joining(","))); List<AnswerVO> answerList = interactiveService.getAnswerBySessionIds(String.join(",", sessionIds));
XxlJobLogger.log("获取到答案:"+JSON.toJSONString(AnswerList)); XxlJobLogger.log("获取到答案:"+JSON.toJSONString(answerList));
AnswerList.parallelStream().forEach(answerVO -> { answerList.parallelStream().forEach(answerVO -> CompletableFuture.runAsync(() -> {
CompletableFuture.runAsync(() -> { XxlJobLogger.log("开始获取"+answerVO.getSessionId()+"答案!");
XxlJobLogger.log("开始获取"+answerVO.getSessionId()+"答案!"); List<String> asks = (List<String>) redisTemplate.opsForValue().get(HZIMS_BIGMODEL_ASK_KEY);
WebSocketSession session = InteractiveSessionManager.get(answerVO.getSessionId()); // 问题回答完成,去除redis问题标记
TextMessage message = InteractiveSessionManager.getTextMessage("1",JSON.toJSONString(answerVO)); if(answerVO.getStatus() == 0){
wsService.sendMessage(session,message); if(CollectionUtil.isNotEmpty(asks) && asks.contains(answerVO.getSessionId())){
}, getAnswerPoolExecutor); 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; return ReturnT.SUCCESS;
} }
} }

3
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) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }else {
else {
// 发送问题 // 发送问题
String context = messageJSON.getString("context"); String context = messageJSON.getString("context");
String userId = messageJSON.getString("userId"); String userId = messageJSON.getString("userId");

Loading…
Cancel
Save