Browse Source

add:大模型管理

zhongwei
haungxing 7 months ago
parent
commit
19652fbd6b
  1. 11
      hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/entity/FuncParamEntity.java
  2. 3
      hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/entity/FunctionEntity.java
  3. 27
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/constants/FuncRouteEnum.java
  4. 9
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/InteractiveController.java
  5. 2
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IInteractiveService.java
  6. 8
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/InteractiveServiceImpl.java
  7. 14
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/JumpPageServiceImpl.java
  8. 134
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/JumpRouteJoinStrategy.java
  9. 2
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/schedule/InteractiveSchedule.java
  10. 5
      hzims-service/hzims-big-model/src/main/resources/db/1.0.0.sql

11
hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/entity/FuncParamEntity.java

@ -9,6 +9,8 @@ import org.springblade.core.mp.support.SqlCondition;
import org.springblade.core.tenant.mp.TenantEntity;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
/**
@ -22,17 +24,20 @@ public class FuncParamEntity extends TenantEntity {
@ApiModelProperty(value = "函数表主键ID")
@QueryField(condition = SqlCondition.EQUAL)
@Max(20)
@NotNull
private Long funcId;
@ApiModelProperty("参数名称")
@QueryField(condition = SqlCondition.LIKE)
@Size(min = 1,max = 50)
@NotBlank
private String name;
@ApiModelProperty(value = "参数别名,传参参数")
@QueryField(condition = SqlCondition.EQUAL)
@Size(min = 1,max = 25)
private String alias;
@ApiModelProperty(value = "是否必须")
@QueryField(condition = SqlCondition.EQUAL)
private Boolean isRequire;
}

3
hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/entity/FunctionEntity.java

@ -42,4 +42,7 @@ public class FunctionEntity extends TenantEntity {
@ApiModelProperty("描述")
private String remake;
@ApiModelProperty("路由")
private String route;
}

27
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/constants/FuncRouteEnum.java

@ -0,0 +1,27 @@
package com.hnac.hzims.bigmodel.interactive.constants;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Arrays;
import java.util.Optional;
/**
* @Author: huangxing
* @Date: 2024/04/30 11:41
*/
@AllArgsConstructor
public enum FuncRouteEnum {
OPEN_SCADA("open_scada","打开实时画面"),
;
@Getter
private String funcCode;
@Getter
private String funcName;
public static FuncRouteEnum getEnumByFuncCode(String funcCode) {
Optional<FuncRouteEnum> FuncRoute = Arrays.stream(FuncRouteEnum.class.getEnumConstants()).filter(e -> funcCode.equals(e.getFuncCode())).findFirst();
return FuncRoute.get();
}
}

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

@ -42,4 +42,13 @@ public class InteractiveController {
return interactiveService.ask(request, question);
}
@ApiOperation("站点、菜单鉴权")
@ApiOperationSupport(order = 3)
@GetMapping("/authentication")
public R authentication(@RequestParam(required = false) @ApiParam("站点编号") String stationId,
@RequestParam @ApiParam("用户ID") String userId,
@RequestParam(required = false) @ApiParam("菜单ID") String menuId) {
return R.status(interactiveService.authentication(stationId,userId,menuId));
}
}

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

@ -17,4 +17,6 @@ public interface IInteractiveService {
R ask(HttpServletRequest request, String question);
Boolean authentication(String stationId, String userId, String menuId);
}

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

@ -58,7 +58,7 @@ public class InteractiveServiceImpl implements IInteractiveService {
Assert.isTrue(StringUtil.isNotBlank(extra) && Func.isNotEmpty(extra), () -> {
throw new ServiceException("解析" + function.getName() + "函数失败!");
});
R.data(extra);
return R.data(extra);
default:
throw new ServiceException("函数解析失败!");
}
@ -82,4 +82,10 @@ public class InteractiveServiceImpl implements IInteractiveService {
return R.data(JSONObject.parseObject(response.body()).getString("data"));
}
@Override
public Boolean authentication(String stationId, String userId, String menuId) {
//TODO 鉴权逻辑完善
return true;
}
}

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

@ -29,25 +29,17 @@ import java.util.Set;
public class JumpPageServiceImpl implements IJumpPageService {
private final IFuncParamService paramService;
private final JumpRouteJoinStrategy strategy;
@Override
public String dealJumpTypeFunction(FunctionEntity function,Map<String,String> args) {
List<FuncParamEntity> params = paramService.getParamsByFuncId(function.getId());
Optional<FuncParamEntity> paramOptional = params.stream().filter(p -> !args.containsKey(p)).findAny();
Optional<FuncParamEntity> paramOptional = params.stream().filter(p -> p.getIsRequire()).filter(p -> !args.containsKey(p.getAlias())).findAny();
Assert.isTrue(!paramOptional.isPresent(), () -> {
throw new ServiceException("解析参数失败,缺少参数:" + paramOptional.get().getName());
});
// 跳转页面逻辑
ExtraVO extraVO = new ExtraVO();
String path = function.getPath();
Set<Map.Entry<String, String>> entries = args.entrySet();
// 替换path中变量
for (Map.Entry<String, String> entry : entries) {
String replaceVariables = "{" + entry.getKey() + "}";
path = StringUtil.replace(path,replaceVariables,entry.getValue());
}
extraVO.setType(FunctionConstants.TypeEnum.JUMP.getType());
extraVO.setRoute(path);
ExtraVO extraVO = strategy.resolve(function,args);
// TODO 将结果存入redis中供前端获取
return JSON.toJSONString(extraVO);
}

134
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/JumpRouteJoinStrategy.java

@ -0,0 +1,134 @@
package com.hnac.hzims.bigmodel.interactive.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hnac.hzims.bigmodel.entity.FunctionEntity;
import com.hnac.hzims.bigmodel.interactive.constants.FuncRouteEnum;
import com.hnac.hzims.bigmodel.interactive.constants.FunctionConstants;
import com.hnac.hzims.bigmodel.interactive.vo.ExtraVO;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @Author: huangxing
* @Date: 2024/04/30 11:38
*/
@Component
public class JumpRouteJoinStrategy {
/**
* 解析大模型传参
* @param args 大模型传参
* @return 跳转path所需要的参数
*/
public ExtraVO resolve(FunctionEntity function,Map<String,String> args) {
FuncRouteEnum routeEnum = FuncRouteEnum.getEnumByFuncCode(function.getCode());
if(Func.isNotEmpty(routeEnum)) {
switch(routeEnum) {
case OPEN_SCADA:
return this.getScadaExtra(args,function);
default:
break;
}
}
return null;
}
private ExtraVO getScadaExtra(Map<String,String> args,FunctionEntity function) {
// 跳转页面逻辑
ExtraVO extraVO = new ExtraVO();
Map<String, String> params = this.scadaResolve(args);
// 根据hz3000画面版本获取path
Integer picResource = Integer.valueOf(params.get("picResource"));
JSONObject pathObject = JSONObject.parseObject(function.getPath());
// 云组态
if(picResource == 0) {
String scada = pathObject.getString("scada");
extraVO.setRoute(this.replacePath(scada, params));
extraVO.setRoute(scada);
}
// v3.0
else if (picResource == 1) {
String v3 = pathObject.getString("v3");
extraVO.setRoute(this.replacePath(v3, params));
}
// v4.0
else if (picResource == 2) {
String v4 = pathObject.getString("v4");
// pic_name 去掉头部的/ 以及尾部的.js
String context = params.get("context");
context = this.removeHeadChars(context,"/");
context = this.removeTailChars(context,".js");
params.put("context",context);
extraVO.setRoute(this.replacePath(v4, params));
}
else {
throw new ServiceException("解析出来的画面类型在云组态、v3、v4类型之外,无法解析路由");
}
extraVO.setType(FunctionConstants.TypeEnum.JUMP.getType());
return extraVO;
}
/**
* 打开实时画面
* @param args 大模型解析参数
* @return 实时画面路径拼接所需参数
*/
private Map<String,String> scadaResolve(Map<String,String> args) {
Map<String,String> result = new HashMap<>();
Assert.isTrue(args.containsKey("params"), () -> {
throw new ServiceException("大模型传参缺少params参数");
});
// 参数格式为:picResource^context^stationNum^projectId^taskId^name^id
List<String> params = Func.toStrList("\\^", args.get("params"));
Assert.isTrue(params.size() == 7,() -> {
throw new ServiceException("大模型传参params长度错误,传参为:" + args.get("params"));
});
result.put("picResource",params.get(0));
result.put("context",params.get(1));
result.put("stationNum",params.get(2));
result.put("projectId",params.get(3));
result.put("taskId",params.get(4));
result.put("name",params.get(5));
result.put("id",params.get(6));
return result;
}
private String replacePath(String path,Map<String,String> params) {
Set<Map.Entry<String, String>> entries = params.entrySet();
// 替换path中变量
for (Map.Entry<String, String> entry : entries) {
String replaceVariables = "{" + entry.getKey() + "}";
path = StringUtil.replace(path,replaceVariables,entry.getValue());
}
return path;
}
public String removeHeadChars(String str, String charsToRemove) {
if (str == null || charsToRemove == null || charsToRemove.isEmpty()) {
return str;
}
while (str.startsWith(charsToRemove)) {
str = str.substring(charsToRemove.length());
}
return str;
}
public String removeTailChars(String str, String charsToRemove) {
if (str == null || charsToRemove == null || charsToRemove.isEmpty()) {
return str;
}
while (str.endsWith(charsToRemove)) {
str = str.substring(0, str.length() - charsToRemove.length());
}
return str;
}
}

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

@ -37,6 +37,8 @@ public class InteractiveSchedule {
String resultKey = ParamCache.getValue(GET_INTERACTIVE_RESULT);
Set<String> keySet = redisTemplate.keys(resultKey + "*");
keySet.parallelStream().forEach(key -> {
//TODO 保存问题结果
// 根据Key获取sessionId
List<String> keySplits = Func.toStrList(":", key);
String sessionId = keySplits.get(2);

5
hzims-service/hzims-big-model/src/main/resources/db/1.0.0.sql

@ -31,4 +31,7 @@ CREATE TABLE IF NOT EXISTS `hzims_func_param` (
`UPDATE_TIME` datetime DEFAULT NULL COMMENT '更新时间',
`IS_DELETED` tinyint(4) NOT NULL COMMENT '是否删除',
PRIMARY KEY (`ID`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='模型函数参数';
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='模型函数参数';
alter table `HZIMS_FUNCTION` add column `ROUTE` varchar(100) default null comment '菜单路由';
alter table `HZIMS_FUNC_PARAM` add column `IS_REQUIRE` TINYINT(2) default 0 comment '是否必须';
Loading…
Cancel
Save