Browse Source

fix:大模型代码架构优化

zhongwei
haungxing 7 months ago
parent
commit
3acfb76154
  1. 37
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/data/controller/RemoteController.java
  2. 97
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/data/service/HistoryDataService.java
  3. 31
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/data/service/RemoteService.java
  4. 1
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/AnalyseDataController.java
  5. 55
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/FontEndInteractiveController.java
  6. 13
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/HznlmInteractiveController.java
  7. 3
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/InteractiveController.java
  8. 10
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/factory/AnswerResolveFactory.java
  9. 14
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IAnswerResolveService.java
  10. 71
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/AnalyseDataServiceImpl.java
  11. 8
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/ChoiceAnswerResolveServiceImpl.java
  12. 29
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/DiagnoseAnswerResolveServiceImpl.java
  13. 8
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/ExtraResolveStrategyService.java
  14. 15
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/HznlmInvokeServiceImpl.java
  15. 87
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/ParamAnswerResolveServiceImpl.java
  16. 38
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/RemoteAnswerResolveServiceImpl.java
  17. 2
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/vo/AnswerVO.java
  18. 6
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/manager/SessionRedisManager.java
  19. 85
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/schedule/FrontEndInteractiveSchedule.java
  20. 23
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/schedule/InteractiveSchedule.java
  21. 5
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/utils/RequestClientUtil.java
  22. 2
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/config/WebSocketConfig.java
  23. 6
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/service/InteractiveWsService.java

37
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/data/controller/RemoteController.java

@ -0,0 +1,37 @@
package com.hnac.hzims.bigmodel.data.controller;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.hnac.hzims.bigmodel.BigModelConstants;
import com.hnac.hzims.bigmodel.data.service.RemoteService;
import com.hnac.hzinfo.log.annotation.Business;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.api.R;
import org.springblade.system.dto.ControlDTO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: huangxing
* @Date: 2024/06/24 14:42
*/
@AllArgsConstructor
@RequestMapping("/remote")
@RestController
@Api(value = "数据平台遥控指令管理",tags = "数据平台遥控指令管理")
@Business(module = BigModelConstants.APP_NAME,value = "数据平台遥控指令管理")
public class RemoteController {
private final RemoteService remoteService;
@ApiOperation("下发遥控指令")
@ApiOperationSupport(order = 1)
@PostMapping("/sendRemoteControl")
public R<Object> sendRemoteControl(ControlDTO controlDTO) {
return remoteService.sendRemoteControl(controlDTO);
}
}

97
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/data/service/HistoryDataService.java

@ -0,0 +1,97 @@
package com.hnac.hzims.bigmodel.data.service;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.hnac.hzims.bigmodel.interactive.constants.DataMethodEnum;
import com.hnac.hzims.bigmodel.interactive.constants.DateEnum;
import com.hnac.hzims.bigmodel.interactive.vo.HistoryDataSearchVO;
import com.hnac.hzinfo.sdk.core.response.Result;
import com.hnac.hzinfo.sdk.v5.device.DeviceDataClient;
import com.hnac.hzinfo.sdk.v5.device.dto.ReductionAttrDataDTO;
import com.hnac.hzinfo.sdk.v5.device.dto.ReductionDataDTO;
import com.hnac.hzinfo.sdk.v5.device.vo.ReductionDataVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.log.logger.BladeLogger;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
/**
* @Author: huangxing
* @Date: 2024/06/24 14:57
*/
@Service
@Slf4j
@AllArgsConstructor
public class HistoryDataService {
private final DeviceDataClient deviceDataClient;
private final BladeLogger logger;
public static final int DATA_COUNT_MAX = 8000;
public Result<ReductionDataVO> getPolymerizationData(HistoryDataSearchVO searchVO) {
DataMethodEnum enumByMethod = DataMethodEnum.getEnumByMethod(searchVO.getMethod());
Assert.isTrue(Func.isNotEmpty(enumByMethod),() -> {
throw new ServiceException("数据查询聚合方式传参有误,查询失败!");
});
// 聚合数据方式处理
DateEnum dateEnum = DateEnum.getDateEnumByType(searchVO.getDataType());
ReductionDataDTO dataDTO = new ReductionDataDTO();
ReductionAttrDataDTO reductionAttrData = new ReductionAttrDataDTO();
reductionAttrData.setSignage(searchVO.getSignage());
reductionAttrData.setAccessRules(enumByMethod.getAccessRule());
reductionAttrData.setKeepFigures(2);
dataDTO.setBeginTime(LocalDateTime.parse(searchVO.getStartTime(), DateUtil.DATETIME_FORMATTER));
dataDTO.setEndTime(LocalDateTime.parse(searchVO.getEndTime(),DateUtil.DATETIME_FORMATTER));
dataDTO.setDeviceCode(searchVO.getDeviceCode());
dataDTO.setNeedPage(false);
dataDTO.setSaveTimeType(dateEnum.getSaveTimeType());
dataDTO.setTimeInterval(1);
dataDTO.setDtos(Lists.newArrayList(reductionAttrData));
logger.info("interactive:getPolymerizationData","config传参为:" + JSON.toJSONString(dataDTO));
return deviceDataClient.pageDeviceCodeAndSignages(dataDTO);
}
public DateEnum getDateEnumByDuration(String startTime,String endTime,DateEnum dateEnum) {
LocalDateTime start = LocalDateTime.parse(startTime, DateUtil.DATETIME_FORMATTER);
LocalDateTime end = LocalDateTime.parse(endTime, DateUtil.DATETIME_FORMATTER);
Duration duration = Duration.between(start, end);
int count;
switch(dateEnum) {
case YEAR:
count = Long.valueOf(ChronoUnit.YEARS.between(start.toLocalDate(), end.toLocalDate())).intValue();
break;
case MONTH:
count = Long.valueOf(ChronoUnit.MONTHS.between(start.toLocalDate(), end.toLocalDate())).intValue();
break;
case DAY:
count = Long.valueOf(ChronoUnit.DAYS.between(start.toLocalDate(), end.toLocalDate())).intValue();
break;
case HOUR:
count = Long.valueOf(duration.toHours()).intValue();
break;
case MINUTE:
count = Long.valueOf(duration.toMinutes()).intValue();
break;
case SECOND:
count = Long.valueOf(duration.getSeconds()).intValue();
break;
default:
throw new ServiceException("未找到相关时间类型,查询失败!");
}
if(count <= DATA_COUNT_MAX) {
return dateEnum;
}
else {
return getDateEnumByDuration(startTime, endTime, DateEnum.getDateEnumByOrder(dateEnum.getOrder() + 1));
}
}
}

31
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/data/service/RemoteService.java

@ -0,0 +1,31 @@
package com.hnac.hzims.bigmodel.data.service;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.api.R;
import org.springblade.system.dto.ControlDTO;
import org.springblade.system.feign.IRemoteClient;
import org.springframework.stereotype.Service;
/**
* @Author: huangxing
* @Date: 2024/06/24 14:36
* @Descirbe: 数据平台 - 遥控服务类
*/
@Service
@Slf4j
@AllArgsConstructor
public class RemoteService {
private final IRemoteClient remoteClient;
/**
* 发送遥控指令
* @param controlDTO 遥控指令
* @return 结果
*/
public R<Object> sendRemoteControl(ControlDTO controlDTO) {
return remoteClient.sendCtrl(controlDTO);
}
}

1
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/AnalyseDataController.java

@ -1,6 +1,5 @@
package com.hnac.hzims.bigmodel.interactive.controller;
import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.hnac.hzims.bigmodel.interactive.service.IAnalyseDataService;
import com.hnac.hzims.bigmodel.interactive.vo.HistoryDataSearchVO;

55
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/FontEndInteractiveController.java

@ -0,0 +1,55 @@
package com.hnac.hzims.bigmodel.interactive.controller;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.google.common.collect.Lists;
import com.hnac.hzims.bigmodel.BigModelConstants;
import com.hnac.hzims.bigmodel.interactive.service.IHznlmInvokeService;
import com.hnac.hzinfo.log.annotation.Business;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
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 java.util.List;
/**
* @Author: huangxing
* @Date: 2024/06/24 14:19
*/
@RestController
@RequestMapping("/fontEnd/interactive")
@Api(value = "前端交互控制层",tags = "前端交互控制层")
@AllArgsConstructor
@Business(module = BigModelConstants.APP_NAME,value = "前端交互层管理",ignore = false)
public class FontEndInteractiveController {
private final IHznlmInvokeService hznlmInvokeService;
@ApiOperation("提问")
@ApiOperationSupport(order = 1)
@GetMapping("/ask")
public R ask(@RequestParam @ApiParam("用户提出问题") String question, @RequestParam @ApiParam("问答sessionId") String sessionId, @RequestParam @ApiParam("用户Id") String userId) {
hznlmInvokeService.ask(question, sessionId, userId);
return R.success("操作成功!");
}
@ApiOperation("删除对话sessionId")
@ApiOperationSupport(order = 2)
@GetMapping("/removeSessionId")
public R<Boolean> removeSessionId(@RequestParam(value = "id") String sessionId) {
hznlmInvokeService.removeSessionId(sessionId);
return R.success("操作成功!");
}
@ApiOperation("获取热点问题")
@ApiOperationSupport(order = 3)
@GetMapping("/hotQuestions")
public R<List<String>> hotQuestions() {
return R.data(hznlmInvokeService.hotQuestions());
}
}

13
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/HznlmInteractiveController.java

@ -3,17 +3,16 @@ package com.hnac.hzims.bigmodel.interactive.controller;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.hnac.hzims.bigmodel.BigModelConstants;
import com.hnac.hzims.bigmodel.interactive.dto.AuthDataDTO;
import com.hnac.hzims.bigmodel.interactive.req.ModelFunctionReq;
import com.hnac.hzims.bigmodel.interactive.service.IHznlmInteractiveService;
import com.hnac.hzims.bigmodel.interactive.vo.AuthDataVO;
import com.hnac.hzims.bigmodel.interactive.vo.ResolveResultVO;
import com.hnac.hzinfo.log.annotation.Business;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
@ -38,4 +37,10 @@ public class HznlmInteractiveController {
return R.data(interactiveService.getAuthData(req));
}
@ApiOperation("解析大模型函数")
@ApiOperationSupport(order = 2)
@PostMapping("/resolve")
public R<ResolveResultVO> resolve(@RequestBody @Valid ModelFunctionReq req) {
return R.data(interactiveService.resolve(req));
}
}

3
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/InteractiveController.java

@ -42,6 +42,7 @@ public class InteractiveController {
@ApiOperation("提问")
@ApiOperationSupport(order = 2)
@GetMapping("/ask")
@Deprecated
public R ask(@RequestParam @ApiParam("用户提出问题") String question,@RequestParam @ApiParam("问答sessionId") String sessionId,@RequestParam @ApiParam("用户Id") String userId) {
return interactiveService.ask(question, sessionId, userId);
}
@ -67,6 +68,7 @@ public class InteractiveController {
@ApiOperation("删除对话sessionId")
@ApiOperationSupport(order = 5)
@GetMapping("/removeSessionId")
@Deprecated
public R<Boolean> removeSessionId(@RequestParam(value = "id") String sessionId) {
return R.data(interactiveService.removeSessionId(sessionId));
}
@ -99,6 +101,7 @@ public class InteractiveController {
@ApiOperation("获取热点问题")
@ApiOperationSupport(order = 8)
@GetMapping("/hotQuestions")
@Deprecated
public R hotQuestions() {
try{
List questions = interactiveService.hotQuestions();

10
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/factory/AnswerResolveFactory.java

@ -1,6 +1,9 @@
package com.hnac.hzims.bigmodel.interactive.factory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hnac.hzims.bigmodel.interactive.constants.FuncRouteEnum;
import com.hnac.hzims.bigmodel.interactive.service.IAnswerResolveService;
import com.hnac.hzims.bigmodel.interactive.service.IResolveService;
import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO;
import org.springblade.core.log.exception.ServiceException;
@ -19,7 +22,12 @@ public class AnswerResolveFactory {
public static final String REMOTE_ANSWER_SERVICE = "remoteAnswerResolveService";
public static final String PARAM_ANSWER_SERVICE = "paramAnswerResolveService";
public static IResolveService getResolveService(String funcCode) {
public static IAnswerResolveService getResolveService(Object extra) {
String funcCode = JSONObject.parseObject(JSON.toJSONString(extra)).getString("func");
return AnswerResolveFactory.getResolveService(funcCode);
}
public static IAnswerResolveService getResolveService(String funcCode) {
FuncRouteEnum funcRouteEnum = FuncRouteEnum.getEnumByFuncCode(funcCode);
if(Func.isNotEmpty(funcRouteEnum)) {
switch (funcRouteEnum) {

14
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IAnswerResolveService.java

@ -3,6 +3,7 @@ package com.hnac.hzims.bigmodel.interactive.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO;
import com.hnac.hzims.bigmodel.interactive.vo.ExtraVO;
import java.util.Arrays;
import java.util.stream.Stream;
@ -15,8 +16,19 @@ public interface IAnswerResolveService {
AnswerVO resolve(AnswerVO answer);
/**
* 解析 extra
* @param originExtra 大模型传入 extra
* @return 解析后的 extra
*/
ExtraVO getExtra(JSONObject originExtra);
/***
*
* @param answer
* @return
*/
default Stream<JSONObject> extraStream(AnswerVO answer) {
return Arrays.stream(answer.getExtras()).map(JSON::toJSONString).map(JSONObject::parseObject);
}
}

71
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/AnalyseDataServiceImpl.java

@ -1,14 +1,12 @@
package com.hnac.hzims.bigmodel.interactive.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.hnac.hzims.bigmodel.data.service.HistoryDataService;
import com.hnac.hzims.bigmodel.interactive.constants.DataMethodEnum;
import com.hnac.hzims.bigmodel.interactive.constants.DateEnum;
import com.hnac.hzims.bigmodel.interactive.constants.FuncRouteEnum;
import com.hnac.hzims.bigmodel.interactive.constants.FunctionConstants;
import com.hnac.hzims.bigmodel.interactive.service.IAnalyseDataService;
import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO;
import com.hnac.hzims.bigmodel.interactive.vo.ExtraVO;
import com.hnac.hzims.bigmodel.interactive.vo.HistoryDataSearchVO;
import com.hnac.hzinfo.sdk.core.response.Result;
@ -28,7 +26,6 @@ import org.springframework.util.Assert;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.List;
@ -43,9 +40,7 @@ import java.util.stream.Collectors;
@AllArgsConstructor
public class AnalyseDataServiceImpl implements IAnalyseDataService {
private final DeviceDataClient deviceDataClient;
private final BladeLogger logger;
public static final int DATA_COUNT_MAX = 8000;
private final HistoryDataService historyDataService;
@Override
public ExtraVO getHistoryData(HistoryDataSearchVO searchVO) {
@ -57,14 +52,14 @@ public class AnalyseDataServiceImpl implements IAnalyseDataService {
DateEnum dateEnum = DateEnum.getDateEnumByType(searchVO.getDataType());
searchVO.setDataType(dateEnum.getDateType());
// 获取最适配时间段
DateEnum adapterDate = this.getDateEnumByDuration(searchVO.getStartTime(), searchVO.getEndTime(), dateEnum);
DateEnum adapterDate = historyDataService.getDateEnumByDuration(searchVO.getStartTime(), searchVO.getEndTime(), dateEnum);
String note = adapterDate.getDateType().equals(searchVO.getDataType()) ? "" : "(由于数据量过大,只能帮您" + adapterDate.getDateType() + "查询)";
searchVO.setDataType(adapterDate.getDateType());
ExtraVO extraVO = new ExtraVO();
extraVO.setType(FuncRouteEnum.HISTORY_DATA.getType().getType());
extraVO.setFuncCode(FuncRouteEnum.HISTORY_DATA.getFuncCode());
Map<String,Object> params = new HashMap<>(1);
Result<ReductionDataVO> reductionDataVOR = this.getPolymerizationData(searchVO);
Result<ReductionDataVO> reductionDataVOR = historyDataService.getPolymerizationData(searchVO);
String label;
if(!reductionDataVOR.isSuccess()) {
params.put("errorInfo",reductionDataVOR.getMsg());
@ -102,62 +97,4 @@ public class AnalyseDataServiceImpl implements IAnalyseDataService {
extraVO.setParams(params);
return extraVO;
}
private Result<ReductionDataVO> getPolymerizationData(HistoryDataSearchVO searchVO) {
DataMethodEnum enumByMethod = DataMethodEnum.getEnumByMethod(searchVO.getMethod());
Assert.isTrue(Func.isNotEmpty(enumByMethod),() -> {
throw new ServiceException("数据查询聚合方式传参有误,查询失败!");
});
// 聚合数据方式处理
DateEnum dateEnum = DateEnum.getDateEnumByType(searchVO.getDataType());
ReductionDataDTO dataDTO = new ReductionDataDTO();
ReductionAttrDataDTO reductionAttrData = new ReductionAttrDataDTO();
reductionAttrData.setSignage(searchVO.getSignage());
reductionAttrData.setAccessRules(enumByMethod.getAccessRule());
reductionAttrData.setKeepFigures(2);
dataDTO.setBeginTime(LocalDateTime.parse(searchVO.getStartTime(),DateUtil.DATETIME_FORMATTER));
dataDTO.setEndTime(LocalDateTime.parse(searchVO.getEndTime(),DateUtil.DATETIME_FORMATTER));
dataDTO.setDeviceCode(searchVO.getDeviceCode());
dataDTO.setNeedPage(false);
dataDTO.setSaveTimeType(dateEnum.getSaveTimeType());
dataDTO.setTimeInterval(1);
dataDTO.setDtos(Lists.newArrayList(reductionAttrData));
logger.info("interactive:getPolymerizationData","config传参为:" + JSON.toJSONString(dataDTO));
return deviceDataClient.pageDeviceCodeAndSignages(dataDTO);
}
private DateEnum getDateEnumByDuration(String startTime,String endTime,DateEnum dateEnum) {
LocalDateTime start = LocalDateTime.parse(startTime, DateUtil.DATETIME_FORMATTER);
LocalDateTime end = LocalDateTime.parse(endTime, DateUtil.DATETIME_FORMATTER);
Duration duration = Duration.between(start, end);
int count;
switch(dateEnum) {
case YEAR:
count = Long.valueOf(ChronoUnit.YEARS.between(start.toLocalDate(), end.toLocalDate())).intValue();
break;
case MONTH:
count = Long.valueOf(ChronoUnit.MONTHS.between(start.toLocalDate(), end.toLocalDate())).intValue();
break;
case DAY:
count = Long.valueOf(ChronoUnit.DAYS.between(start.toLocalDate(), end.toLocalDate())).intValue();
break;
case HOUR:
count = Long.valueOf(duration.toHours()).intValue();
break;
case MINUTE:
count = Long.valueOf(duration.toMinutes()).intValue();
break;
case SECOND:
count = Long.valueOf(duration.getSeconds()).intValue();
break;
default:
throw new ServiceException("未找到相关时间类型,查询失败!");
}
if(count <= DATA_COUNT_MAX) {
return dateEnum;
}
else {
return getDateEnumByDuration(startTime, endTime, DateEnum.getDateEnumByOrder(dateEnum.getOrder() + 1));
}
}
}

8
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/ChoiceAnswerResolveServiceImpl.java

@ -28,13 +28,13 @@ public class ChoiceAnswerResolveServiceImpl implements IAnswerResolveService {
@Override
public AnswerVO resolve(AnswerVO answer) {
List<ExtraVO> extraList = this.extraStream(answer).map(this::getExtra).collect(Collectors.toList());
answer.setExtras(extraList.toArray());
Object[] extraArray = this.extraStream(answer).map(this::getExtra).toArray();
answer.setExtras(extraArray);
return answer;
}
private ExtraVO getExtra(JSONObject originExtra) {
@Override
public ExtraVO getExtra(JSONObject originExtra) {
ExtraVO result = new ExtraVO();
JSONArray selections = JSONArray.parseArray(JSON.toJSONString(originExtra.get("data")));
result.setSelection(selections);

29
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/DiagnoseAnswerResolveServiceImpl.java

@ -1,12 +1,20 @@
package com.hnac.hzims.bigmodel.interactive.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hnac.hzims.bigmodel.interactive.constants.FuncRouteEnum;
import com.hnac.hzims.bigmodel.interactive.constants.FunctionConstants;
import com.hnac.hzims.bigmodel.interactive.factory.AnswerResolveFactory;
import com.hnac.hzims.bigmodel.interactive.service.IAnswerResolveService;
import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO;
import com.hnac.hzims.bigmodel.interactive.vo.ExtraVO;
import groovy.util.logging.Slf4j;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* @Author: huangxing
* @Date: 2024/06/21 18:59
@ -18,6 +26,25 @@ public class DiagnoseAnswerResolveServiceImpl implements IAnswerResolveService {
@Override
public AnswerVO resolve(AnswerVO answer) {
return null;
Object[] extraArray = this.extraStream(answer).map(this::getExtra).toArray();
answer.setExtras(extraArray);
return answer;
}
@Override
public ExtraVO getExtra(JSONObject originExtra) {
ExtraVO result = new ExtraVO();
JSONObject data = JSONObject.parseObject(JSON.toJSONString(originExtra.get("data")));
Map<String,Object> params = new HashMap<>(5);
params.put("faultId",data.getString("fault_id"));
params.put("name",data.getString("fault_name"));
params.put("station",data.getString("station_id"));
params.put("fdpDeviceName",data.getString("device_name"));
params.put("fdpOrd",data.getString("ord"));
result.setParams(params);
String funcCode = originExtra.getString("func");
result.setFuncCode(funcCode);
result.setType(FuncRouteEnum.getEnumByFuncCode(funcCode).getType().getType());
return result;
}
}

8
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/ExtraResolveStrategyService.java

@ -8,22 +8,15 @@ import com.hnac.hzims.bigmodel.interactive.constants.FunctionConstants;
import com.hnac.hzims.bigmodel.interactive.constants.SearchTypeEnum;
import com.hnac.hzims.bigmodel.interactive.service.IAnalyseDataService;
import com.hnac.hzims.bigmodel.interactive.vo.*;
import com.hnac.hzims.bigmodel.websocket.constants.RedisKeyConstants;
import com.hnac.hzims.operational.station.entity.StationEntity;
import com.hnac.hzims.operational.station.entity.StationVideoTypeEntity;
import com.hnac.hzims.operational.station.feign.IStationClient;
import com.hnac.hzims.operational.station.feign.IStationVideoTypeClient;
import com.hnac.hzinfo.sdk.v5.device.client.DeviceClient;
import com.hnac.hzinfo.sdk.v5.device.vo.DeviceInstanceAttrVO;
import com.hnac.hzinfo.sdk.v5.device.vo.DeviceInstanceFuncVO;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.AllArgsConstructor;
import org.springblade.core.log.logger.BladeLogger;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.*;
import org.springblade.system.dto.ControlDTO;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.time.format.DateTimeFormatter;
@ -147,6 +140,7 @@ public class ExtraResolveStrategyService {
return result;
}
@Deprecated
private ExtraVO resolveConfirmRemote(Map<String,Object> extra) {
ExtraVO result = new ExtraVO();
RemoteParamVO remoteParam = JSONObject.parseObject(JSON.toJSONString(extra.get("data")), RemoteParamVO.class);

15
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/HznlmInvokeServiceImpl.java

@ -4,15 +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.TypeReference;
import com.google.common.collect.Lists;
import com.hnac.hzims.bigmodel.configuration.BigModelInvokeUrl;
import com.hnac.hzims.bigmodel.interactive.service.IHznlmInvokeService;
import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO;
import com.hnac.hzims.bigmodel.manager.SessionRedisManager;
import com.hnac.hzims.bigmodel.utils.RequestClientUtil;
import com.xxl.job.core.log.XxlJobLogger;
import groovy.util.logging.Slf4j;
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.tool.api.R;
@ -94,15 +96,20 @@ public class HznlmInvokeServiceImpl implements IHznlmInvokeService {
@Override
public List hotQuestions() {
return RequestClientUtil.postCall(fdpHost + bigModelInvokeUrl.getHotQuestion(), null, List.class);
try {
return RequestClientUtil.postCall(fdpHost + bigModelInvokeUrl.getHotQuestion(), null, new TypeReference<List<String>>(){});
}
catch (Exception e) {
log.error("An error occurred",e);
return Lists.newArrayList();
}
}
@Override
public List<AnswerVO> getAnswerBySessionIds(String sessionIds) {
Map<String,Object> params = new HashMap<>();
params.put("ids",Func.toStrList(",",sessionIds).toArray());
List<AnswerVO> answerVOList = RequestClientUtil.postCall(fdpHost + bigModelInvokeUrl.getAssistantStatus(), params, List.class);
//TODO 解析答案
List<AnswerVO> answerVOList = RequestClientUtil.postCall(fdpHost + bigModelInvokeUrl.getAssistantStatus(), params, new TypeReference<List<AnswerVO>>(){});
return answerVOList;
}

87
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/ParamAnswerResolveServiceImpl.java

@ -1,11 +1,26 @@
package com.hnac.hzims.bigmodel.interactive.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hnac.hzims.bigmodel.interactive.constants.FuncRouteEnum;
import com.hnac.hzims.bigmodel.interactive.constants.SearchTypeEnum;
import com.hnac.hzims.bigmodel.interactive.factory.AnswerResolveFactory;
import com.hnac.hzims.bigmodel.interactive.service.IAnalyseDataService;
import com.hnac.hzims.bigmodel.interactive.service.IAnswerResolveService;
import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO;
import com.hnac.hzims.bigmodel.interactive.vo.*;
import com.xxl.job.core.log.XxlJobLogger;
import groovy.util.logging.Slf4j;
import lombok.AllArgsConstructor;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @Author: huangxing
@ -16,8 +31,76 @@ import org.springframework.stereotype.Service;
@AllArgsConstructor
public class ParamAnswerResolveServiceImpl implements IAnswerResolveService {
private final IAnalyseDataService analyseDataService;
public static final String PATTERN_DATETIME = "yyyy-MM-dd HH:mm:ss.SSS";
@Override
public AnswerVO resolve(AnswerVO answer) {
return null;
Object[] extraArray = this.extraStream(answer).map(this::getExtra).toArray();
answer.setExtras(extraArray);
return answer;
}
@Override
public ExtraVO getExtra(JSONObject originExtra) {
DataVO data = JSONObject.parseObject(JSON.toJSONString(originExtra.get("data")),DataVO.class);
Assert.isTrue(Func.isNotEmpty(data),() -> {
XxlJobLogger.log("解析参数答案失败,extra不能为空!");
throw new ServiceException("解析参数答案失败,extra不能为空!");
});
if(Func.isNotEmpty(data.getReal())) {
// 处理查询实时数据
RealDataVO real = data.getReal();
Date date = DateUtil.parse(real.getTime(), PATTERN_DATETIME);
real.setTime(DateUtil.format(date,DateUtil.PATTERN_DATETIME));
}
else if(StringUtil.isNotBlank(data.getType()) && SearchTypeEnum.HISTORY.getSearchType().equals(data.getType())) {
// 处理查询历史数据
this.getHistoryData(data);
}
String funcCode = originExtra.getString("func");
ExtraVO result = new ExtraVO();
result.setFuncCode(funcCode);
result.setType(FuncRouteEnum.getEnumByFuncCode(funcCode).getType().getType());
Map<String,Object> param = new HashMap(1);
param.put("data", data);
result.setParams(param);
return result;
}
/**
* 查询历史数据
* @param data 大模型识别数据信息
* @return 历史数据
*/
private ExtraVO getHistoryData(DataVO data) {
// 确认查询聚合方式传入
Assert.isTrue(Func.isNotEmpty(data.getItem()) && StringUtil.isNotBlank(data.getMethod()),() -> {
XxlJobLogger.log("历史数据查询方式为空,查询失败!");
throw new ServiceException("历史数据查询方式为空,查询失败!");
});
return analyseDataService.getHistoryData(this.convertHistoryDataSearch(data));
}
/**
* 获取历史数据查询对象
* @param data 大模型解析点位信息
* @return 历史数据查询对象
*/
private HistoryDataSearchVO convertHistoryDataSearch(DataVO data) {
HistoryDataSearchVO searchVO = new HistoryDataSearchVO();
DataItemVO item = data.getItem();
searchVO.setDataType(data.getDataType());
searchVO.setAttrName(item.getAttrName());
searchVO.setDeviceCode(item.getDeviceId());
searchVO.setDeviceName(item.getDeviceName());
searchVO.setStationName(item.getProjectName());
searchVO.setMethod(data.getMethod());
searchVO.setSignage(item.getSignage());
searchVO.setStartTime(data.getStartTime());
searchVO.setEndTime(data.getEndTime());
return searchVO;
}
}

38
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/RemoteAnswerResolveServiceImpl.java

@ -1,12 +1,24 @@
package com.hnac.hzims.bigmodel.interactive.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hnac.hzims.bigmodel.interactive.constants.FuncRouteEnum;
import com.hnac.hzims.bigmodel.interactive.factory.AnswerResolveFactory;
import com.hnac.hzims.bigmodel.interactive.service.IAnswerResolveService;
import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO;
import com.hnac.hzims.bigmodel.interactive.vo.ExtraVO;
import com.hnac.hzims.bigmodel.interactive.vo.RemoteParamVO;
import com.hnac.hzinfo.sdk.v5.device.client.DeviceClient;
import com.hnac.hzinfo.sdk.v5.device.vo.DeviceInstanceFuncVO;
import groovy.util.logging.Slf4j;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* @Author: huangxing
* @Date: 2024/06/21 19:02
@ -16,8 +28,32 @@ import org.springframework.stereotype.Service;
@AllArgsConstructor
public class RemoteAnswerResolveServiceImpl implements IAnswerResolveService {
private final DeviceClient deviceClient;
@Override
public AnswerVO resolve(AnswerVO answer) {
return null;
Object[] extraArray = this.extraStream(answer).map(this::getExtra).toArray();
answer.setExtras(extraArray);
return answer;
}
@Override
public ExtraVO getExtra(JSONObject originExtra) {
ExtraVO result = new ExtraVO();
RemoteParamVO remoteParam = JSONObject.parseObject(JSON.toJSONString(originExtra.get("data")), RemoteParamVO.class);
String funcCode = originExtra.getString("func");
result.setFuncCode(funcCode);
result.setType(FuncRouteEnum.getEnumByFuncCode(funcCode).getType().getType());
R<DeviceInstanceFuncVO> funcVOR = deviceClient.getFuncById(remoteParam.getFuncId());
if(funcVOR.isSuccess()) {
Map<String,Object> param = new HashMap();
param.put("control",funcVOR.getData());
param.put("deviceName",remoteParam.getDeviceName());
param.put("projectName",remoteParam.getProjectName());
param.put("deviceCode",remoteParam.getDeviceId());
param.put("value",remoteParam.getValue());
result.setParams(param);
}
return result;
}
}

2
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/vo/AnswerVO.java

@ -7,6 +7,7 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springblade.core.tool.utils.BeanUtil;
import java.io.Serializable;
@ -19,6 +20,7 @@ import java.io.Serializable;
@ApiModel("HZLLM答案VO对象")
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
public class AnswerVO implements Serializable {
@ApiModelProperty("发起问答时的随机ID")

6
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/manager/SessionRedisManager.java

@ -1,10 +1,13 @@
package com.hnac.hzims.bigmodel.manager;
import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
import static com.hnac.hzims.bigmodel.schedule.XxlJobHandlerConstant.HZIMS_BIGMODEL_ASK_KEY;
/**
@ -25,4 +28,7 @@ public class SessionRedisManager {
redisTemplate.opsForList().remove(HZIMS_BIGMODEL_ASK_KEY,1,sessionId);
}
public List<String> getSessionIds() {
return redisTemplate.opsForList().range(HZIMS_BIGMODEL_ASK_KEY, 0, -1);
}
}

85
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/schedule/FrontEndInteractiveSchedule.java

@ -0,0 +1,85 @@
package com.hnac.hzims.bigmodel.schedule;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hnac.hzims.bigmodel.interactive.factory.AnswerResolveFactory;
import com.hnac.hzims.bigmodel.interactive.service.IAnswerResolveService;
import com.hnac.hzims.bigmodel.interactive.service.IHznlmInteractiveService;
import com.hnac.hzims.bigmodel.interactive.service.IHznlmInvokeService;
import com.hnac.hzims.bigmodel.interactive.service.IResolveService;
import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO;
import com.hnac.hzims.bigmodel.manager.SessionRedisManager;
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 lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.logger.BladeLogger;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;
import static com.hnac.hzims.bigmodel.schedule.XxlJobHandlerConstant.*;
/**
* @Author: huangxing
* @Date: 2024/06/24 09:53
*/
@Component
@AllArgsConstructor
@Slf4j
public class FrontEndInteractiveSchedule {
private final SessionRedisManager sessionRedisManager;
private final InteractiveWsService wsService;
private final ThreadPoolExecutor getAnswerPoolExecutor;
private final IHznlmInvokeService hznlmInvokeService;
private final BladeLogger logger;
@XxlJob(GET_INTERACTIVE_RESULT)
public ReturnT getInteractiveResult(String params) {
List<String> sessionIds = sessionRedisManager.getSessionIds();
if(CollectionUtil.isEmpty(sessionIds)){
XxlJobLogger.log("问题都已经回答完毕!");
return ReturnT.SUCCESS;
}
List<AnswerVO> answerList = hznlmInvokeService.getAnswerBySessionIds(String.join(",", sessionIds));
answerList.stream().parallel().forEach(answerVO -> CompletableFuture.runAsync(() -> {
// 如果已经获取到答案 则删除缓存
if(answerVO.getRunning() == 0) {
logger.info("hzims:bigmodel:getInteractiveResult:answer","获取大模型解析答案:" + JSON.toJSONString(answerVO));
sessionRedisManager.removeSessionId(answerVO.getSessionId());
}
Object[] extras = answerVO.getExtras();
if(ObjectUtil.isNotEmpty(answerVO.getExtras()) && extras.length > 0){
try {
// 解析答案
Object[] resolveExtras = Arrays.stream(extras).map(extra -> {
IAnswerResolveService answerResolveService = AnswerResolveFactory.getResolveService(extra);
JSONObject extraObject = JSONObject.parseObject(JSON.toJSONString(extra));
return answerResolveService.getExtra(extraObject);
}).toArray();
answerVO.setExtras(resolveExtras);
}
catch(Exception e) {
log.error("An error occurred",e);
AnswerVO.error(answerVO.getSessionId(), answerVO.getUserId(), answerVO.getQuery());
}
}
TextMessage message = InteractiveSessionManager.getTextMessage("1",JSON.toJSONString(answerVO));
wsService.sendMessage(answerVO.getSessionId(),message);
}, getAnswerPoolExecutor));
return ReturnT.SUCCESS;
}
}

23
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/schedule/InteractiveSchedule.java

@ -67,29 +67,6 @@ public class InteractiveSchedule {
private final ExtraResolveStrategyService extraResolveStrategyService;
// @XxlJob(GET_INTERACTIVE_RESULT)
// public ReturnT execute(String params) {
// String resultKey = ParamCache.getValue(GET_INTERACTIVE_RESULT);
// Set<String> keySet = redisTemplate.keys(resultKey + "*");
// keySet.parallelStream().forEach(key -> {
// // 根据Key获取sessionId
// List<String> keySplits = Func.toStrList(":", key);
// String sessionId = keySplits.get(2);
// // 查询websocket是否存在连接session
// WebSocketSession session = InteractiveSessionManager.get(sessionId);
// if(session == null) {
// return;
// }
// TextMessage message = new TextMessage(JSON.toJSONString(redisTemplate.opsForValue().get(key)));
// Boolean sendResult = wsService.sendMessage(sessionId, message);
// Assert.isTrue(sendResult, () -> {
// throw new ServiceException(key + "推送消息失败,推送消息体为:" + JSON.toJSONString(redisTemplate.opsForValue().get(key)));
// });
// redisTemplate.delete(key);
// });
// return ReturnT.SUCCESS;
// }
@XxlJob(GET_INTERACTIVE_RESULT)
public ReturnT getInteractiveResult(String params) {
List<String> sessionIds = redisTemplate.opsForList().range(HZIMS_BIGMODEL_ASK_KEY, 0, -1);
if(CollectionUtil.isEmpty(sessionIds)){

5
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/utils/RequestClientUtil.java

@ -4,6 +4,7 @@ import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import groovy.util.logging.Slf4j;
import org.springblade.core.log.exception.ServiceException;
import org.springframework.util.Assert;
@ -37,12 +38,12 @@ public class RequestClientUtil {
* @return 结果
* @param <T> 结果解析对象类型
*/
public static <T> T postCall(String url, Map body, Class<T> resultT) {
public static <T> T postCall(String url, Map body, TypeReference<T> typeRef) {
HttpResponse response = HttpRequest.post(url).body(JSON.toJSONString(body)).execute();
Assert.isTrue(response.getStatus() == HttpServletResponse.SC_OK, () -> {
throw new ServiceException("远程调用大模型接口" + url + "失败!");
});
return JSONObject.parseObject(response.body(), resultT);
return JSONObject.parseObject(response.body(), typeRef);
}
}

2
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/config/WebSocketConfig.java

@ -18,7 +18,7 @@ public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(interactiveHandler(), "/interactive/{sessionId}").setAllowedOrigins("*");
registry.addHandler(interactiveHandler(), "/websocket/interactive/{sessionId}").setAllowedOrigins("*");
}
@Bean
public WebSocketHandler interactiveHandler() {

6
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/service/InteractiveWsService.java

@ -20,9 +20,15 @@ public class InteractiveWsService implements WebSocketService {
@Override
public Boolean sendMessage(String sessionId, TextMessage message) {
if(InteractiveSessionManager.SESSION_POOL.containsKey(sessionId)) {
WebSocketSession session = InteractiveSessionManager.get(sessionId);
return this.sendMessage(session,message);
}
else {
log.error("未查询到sessionId,消息推送失败;");
return false;
}
}
@Override
public Boolean sendMessage(WebSocketSession session, TextMessage message) {

Loading…
Cancel
Save