Browse Source

add:大模型分析增加多场景分析

zhongwei
luyie 3 months ago
parent
commit
9fc1177fb8
  1. 10
      hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/api/dto/BigModelAnalysisRequestDTO.java
  2. 56
      hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/api/dto/BigModelAnalysisResponseDTO.java
  3. 5
      hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/api/feign/IBigModelAnalysisClient.java
  4. 9
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/api/feign/BigModelAnalysisClient.java
  5. 4
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/controller/ZhipuAnalysisInfoController.java
  6. 5
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/entity/ZhipuAnalysisInfoEntity.java
  7. 6
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/AbstractZhipuAnalysisHandler.java
  8. 4
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ResultStrategy.java
  9. 32
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalyser.java
  10. 12
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalysisHandlerGlm4v.java
  11. 4
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/service/ZhipuAnalysisService.java
  12. 119
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/service/impl/ZhipuAnalysisServiceImpl.java
  13. 11
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisFileRequest.java
  14. 2
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisResult.java
  15. 4
      hzims-service/inspect/pom.xml
  16. 62
      hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/task/schedule/VideoTaskSchedule.java

10
hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/api/dto/BigModelAnalysisDTO.java → hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/api/dto/BigModelAnalysisRequestDTO.java

@ -4,16 +4,22 @@ import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import javax.validation.constraints.NotNull;
import java.util.List;
/** /**
* @Author: ypj * @Author: ypj
* @Date: 2024/8/12 16:02 * @Date: 2024/8/12 16:02
*/ */
@Data @Data
@ApiModel(value = "大模型分析",description = "大模型分析参数") @ApiModel(value = "大模型分析",description = "大模型分析参数")
public class BigModelAnalysisDTO { public class BigModelAnalysisRequestDTO {
@ApiModelProperty("策略编码") @ApiModelProperty("策略编码")
private String code; private List<String> codeList;
@ApiModelProperty(value = "检测类型子类")
private List<String> checkTypeSonList;
@ApiModelProperty("文件地址或文件的base64文件内容") @ApiModelProperty("文件地址或文件的base64文件内容")
private String url; private String url;

56
hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/api/dto/BigModelAnalysisResponseDTO.java

@ -0,0 +1,56 @@
package com.hnac.hzims.bigmodel.api.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
import java.util.Optional;
/**
* @Author: ypj
* @Date: 2024/8/19 10:38
*/
@Data
@ApiModel(description = "分析返回信息")
public class BigModelAnalysisResponseDTO implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "是否成功", required = true)
private Boolean success = null;
@ApiModelProperty(value = "分析结果数据列表")
private List<BigModelAnalysisResult> data;
@ApiModelProperty(value = "状态码", required = true)
private int code;
@ApiModelProperty(value = "返回消息")
private String msg;
@Data
@ApiModel(description = "分析结果")
public static class BigModelAnalysisResult implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "承载数据")
private Object result;
@ApiModelProperty(value = "结果字符串")
private String resultStr;
@ApiModelProperty(value = "结果描述")
private String description;
@ApiModelProperty(value = "结果描述")
private String checkTypeSon;
}
public Boolean getSuccess() {
return Optional.ofNullable(success).orElse(Boolean.FALSE);
}
}

5
hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/api/feign/IBigModelAnalysisClient.java

@ -1,7 +1,8 @@
package com.hnac.hzims.bigmodel.api.feign; package com.hnac.hzims.bigmodel.api.feign;
import com.hnac.hzims.bigmodel.BigModelConstants; import com.hnac.hzims.bigmodel.BigModelConstants;
import com.hnac.hzims.bigmodel.api.dto.BigModelAnalysisDTO; import com.hnac.hzims.bigmodel.api.dto.BigModelAnalysisRequestDTO;
import com.hnac.hzims.bigmodel.api.dto.BigModelAnalysisResponseDTO;
import org.springblade.core.tool.api.R; import org.springblade.core.tool.api.R;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
@ -16,5 +17,5 @@ public interface IBigModelAnalysisClient {
String PREFIX_API = "feign/bigModel"; String PREFIX_API = "feign/bigModel";
@PostMapping(PREFIX_API + "/analysis") @PostMapping(PREFIX_API + "/analysis")
R analysis(@RequestBody BigModelAnalysisDTO analysisInfo); BigModelAnalysisResponseDTO analysis(@RequestBody BigModelAnalysisRequestDTO analysisInfo);
} }

9
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/api/feign/BigModelAnalysisClient.java

@ -1,10 +1,9 @@
package com.hnac.hzims.bigmodel.api.feign; package com.hnac.hzims.bigmodel.api.feign;
import com.hnac.hzims.bigmodel.api.dto.BigModelAnalysisDTO; import com.hnac.hzims.bigmodel.api.dto.BigModelAnalysisRequestDTO;
import com.hnac.hzims.bigmodel.api.dto.BigModelAnalysisResponseDTO;
import com.hnac.hzims.bigmodel.zhipuai.service.ZhipuAnalysisService; import com.hnac.hzims.bigmodel.zhipuai.service.ZhipuAnalysisService;
import com.hnac.hzims.bigmodel.zhipuai.vo.ZhipuAnalysisFileRequest;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -20,7 +19,7 @@ public class BigModelAnalysisClient implements IBigModelAnalysisClient {
@Override @Override
@PostMapping(PREFIX_API + "/analysis") @PostMapping(PREFIX_API + "/analysis")
public R analysis(BigModelAnalysisDTO analysisInfo) { public BigModelAnalysisResponseDTO analysis(BigModelAnalysisRequestDTO analysisInfo) {
return R.data(analysisService.analysis(ZhipuAnalysisFileRequest.of(analysisInfo))); return analysisService.multiAnalysis(analysisInfo);
} }
} }

4
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/controller/ZhipuAnalysisInfoController.java

@ -32,7 +32,7 @@ public class ZhipuAnalysisInfoController {
@PostMapping("/save") @PostMapping("/save")
@ApiOperation("保存智谱分析信息") @ApiOperation("保存智谱分析信息")
@ApiOperationSupport(order = 1) @ApiOperationSupport(order = 1)
public R<Boolean> save(@RequestBody @Validated ZhipuAnalysisInfoVO request) { public R<Boolean> save(@RequestBody ZhipuAnalysisInfoVO request) {
return R.status(analysisInfoService.save(request.toEntity())); return R.status(analysisInfoService.save(request.toEntity()));
} }
@ -53,7 +53,7 @@ public class ZhipuAnalysisInfoController {
@PutMapping("/update") @PutMapping("/update")
@ApiOperation("更新智谱分析配置信息") @ApiOperation("更新智谱分析配置信息")
@ApiOperationSupport(order = 4) @ApiOperationSupport(order = 4)
public R<Boolean> update(@RequestBody @Validated ZhipuAnalysisInfoEntity requestData) { public R<Boolean> update(@RequestBody ZhipuAnalysisInfoEntity requestData) {
return R.status(analysisInfoService.updateById(requestData)); return R.status(analysisInfoService.updateById(requestData));
} }

5
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/entity/ZhipuAnalysisInfoEntity.java

@ -7,7 +7,6 @@ import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.mp.support.QueryField; import org.springblade.core.mp.support.QueryField;
import org.springblade.core.tenant.mp.TenantEntity; import org.springblade.core.tenant.mp.TenantEntity;
@ -25,6 +24,10 @@ public class ZhipuAnalysisInfoEntity extends TenantEntity {
@TableField("CODE") @TableField("CODE")
private String code; private String code;
@ApiModelProperty(value = "对应字典表“AI识别类型”的key值")
@TableField("CHECK_TYPE_SON")
private String checkTypeSon;
@ApiModelProperty(value = "策略名称") @ApiModelProperty(value = "策略名称")
@TableField("NAME") @TableField("NAME")
@QueryField(condition = SqlCondition.LIKE) @QueryField(condition = SqlCondition.LIKE)

6
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/AbstractZhipuAnalysisHandler.java

@ -1,6 +1,7 @@
package com.hnac.hzims.bigmodel.zhipuai.handler; package com.hnac.hzims.bigmodel.zhipuai.handler;
import com.zhipu.oapi.ClientV4; import com.zhipu.oapi.ClientV4;
import lombok.Data;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -8,10 +9,15 @@ import javax.annotation.Resource;
* @Author: ypj * @Author: ypj
* @Date: 2024/8/12 8:44 * @Date: 2024/8/12 8:44
*/ */
@Data
public abstract class AbstractZhipuAnalysisHandler implements ZhipuAnalyser { public abstract class AbstractZhipuAnalysisHandler implements ZhipuAnalyser {
protected final String requestIdTemplate = "big_model-%d"; protected final String requestIdTemplate = "big_model-%d";
@Resource @Resource
protected ClientV4 client; protected ClientV4 client;
private String text;
private String url;
} }

4
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ResultStrategy.java

@ -36,8 +36,8 @@ public interface ResultStrategy<T> {
return originalData; return originalData;
} }
default T getResult(ZhipuAnalysisInfoEntity info, String originalData) { default T getResult(ZhipuAnalysisInfoEntity info, String originalValue) {
return getResult(info, convertOriginalData(originalData, info.getResultType())); return getResult(info, convertOriginalData(originalValue, info.getResultType()));
} }
T getResult(ZhipuAnalysisInfoEntity info, Object resultData); T getResult(ZhipuAnalysisInfoEntity info, Object resultData);

32
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalyser.java

@ -1,32 +1,50 @@
package com.hnac.hzims.bigmodel.zhipuai.handler; package com.hnac.hzims.bigmodel.zhipuai.handler;
import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity;
import org.springblade.core.tool.jackson.JsonUtil; import org.springblade.core.tool.jackson.JsonUtil;
import java.util.Map; import java.util.Map;
import java.util.Optional;
/** /**
* @Author: ypj * @Author: ypj
* @Date: 2024/8/8 14:45 * @Date: 2024/8/8 14:45
*/ */
public interface ZhipuAnalyser<Req, Resp> { public interface ZhipuAnalyser<Req, Resp> {
String QUESTION_PROFILE = "## Profile\n";
String QUESTION_CONSTRAINS = "## Constrains\n";
String QUESTION_OUTPUT_FORMAT = "## OutputFormat\n";
String getAnalysisStrategy(); String getAnalysisStrategy();
String getAnalysisModel(); String getAnalysisModel();
String getUrl();
void setUrl(String url);
String getText();
void setText(String text);
default boolean isSupport(String strategyCode) { default boolean isSupport(String strategyCode) {
return getAnalysisStrategy().equals(strategyCode); return getAnalysisStrategy().equals(strategyCode);
} }
Resp sendRequest(ZhipuAnalysisInfoEntity info, String url); Resp sendRequest(String text, String url);
Req getRequest(ZhipuAnalysisInfoEntity info, String url); Req getRequest(String text, String url);
String getResult(ZhipuAnalysisInfoEntity info, String url); String getResult(String text, String url);
default Map<String, Object> getResultMap(String text, String url) {
String result = getResult(text, url);
return Optional.ofNullable(result).map(str -> JsonUtil.toMap(result)).orElse(null);
}
default Object getResultValue(ZhipuAnalysisInfoEntity info, String url) { default Object getResultValue(String text, String key, String url) {
Map<String, Object> resultMap = JsonUtil.toMap(getResult(info, url)); Map<String, Object> resultMap = getResultMap(text, url);
return resultMap.get(info.getResultKey()); return Optional.ofNullable(resultMap).map(data -> data.get(key)).orElse(null);
} }
} }

12
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalysisHandlerGlm4v.java

@ -33,19 +33,19 @@ public class ZhipuAnalysisHandlerGlm4v extends AbstractZhipuAnalysisHandler {
} }
@Override @Override
public ModelApiResponse sendRequest(ZhipuAnalysisInfoEntity info, String url) { public ModelApiResponse sendRequest(String text, String url) {
ChatCompletionRequest request = getRequest(info, url); ChatCompletionRequest request = getRequest(text, url);
log.info("send zhipu ai request :{}", Optional.ofNullable(request).map(req -> JsonUtil.toJson(req)).orElse("null")); log.info("send zhipu ai request :{}", Optional.ofNullable(request).map(req -> JsonUtil.toJson(req)).orElse("null"));
return client.invokeModelApi(request); return client.invokeModelApi(request);
} }
@Override @Override
public ChatCompletionRequest getRequest(ZhipuAnalysisInfoEntity info, String url) { public ChatCompletionRequest getRequest(String text, String url) {
List<ChatMessage> messages = new ArrayList<>(); List<ChatMessage> messages = new ArrayList<>();
List<Map<String, Object>> contentList = new ArrayList<>(); List<Map<String, Object>> contentList = new ArrayList<>();
Map<String, Object> textMap = new HashMap<>(); Map<String, Object> textMap = new HashMap<>();
textMap.put("type", "text"); textMap.put("type", "text");
textMap.put("text", info.getRequestContent()); textMap.put("text", text);
Map<String, Object> typeMap = new HashMap<>(); Map<String, Object> typeMap = new HashMap<>();
typeMap.put("type", "image_url"); typeMap.put("type", "image_url");
Map<String, Object> urlMap = new HashMap<>(); Map<String, Object> urlMap = new HashMap<>();
@ -68,8 +68,8 @@ public class ZhipuAnalysisHandlerGlm4v extends AbstractZhipuAnalysisHandler {
} }
@Override @Override
public String getResult(ZhipuAnalysisInfoEntity info, String url) { public String getResult(String text, String url) {
ModelApiResponse response = sendRequest(info, url); ModelApiResponse response = sendRequest(text, url);
log.info("get zhi pu ai response : {}", Optional.ofNullable(response).map(resp -> JsonUtil.toJson(resp)).orElse("null")); log.info("get zhi pu ai response : {}", Optional.ofNullable(response).map(resp -> JsonUtil.toJson(resp)).orElse("null"));
if (response.isSuccess()) { if (response.isSuccess()) {
try { try {

4
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/service/ZhipuAnalysisService.java

@ -1,5 +1,7 @@
package com.hnac.hzims.bigmodel.zhipuai.service; package com.hnac.hzims.bigmodel.zhipuai.service;
import com.hnac.hzims.bigmodel.api.dto.BigModelAnalysisRequestDTO;
import com.hnac.hzims.bigmodel.api.dto.BigModelAnalysisResponseDTO;
import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity; import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity;
import com.hnac.hzims.bigmodel.zhipuai.vo.ZhipuAnalysisFileRequest; import com.hnac.hzims.bigmodel.zhipuai.vo.ZhipuAnalysisFileRequest;
import com.hnac.hzims.bigmodel.zhipuai.vo.ZhipuAnalysisFileResponse; import com.hnac.hzims.bigmodel.zhipuai.vo.ZhipuAnalysisFileResponse;
@ -13,4 +15,6 @@ public interface ZhipuAnalysisService {
ZhipuAnalysisFileResponse analysis(ZhipuAnalysisFileRequest request); ZhipuAnalysisFileResponse analysis(ZhipuAnalysisFileRequest request);
ZhipuAnalysisInfoEntity getAnalysisInfo(ZhipuAnalysisFileRequest request); ZhipuAnalysisInfoEntity getAnalysisInfo(ZhipuAnalysisFileRequest request);
BigModelAnalysisResponseDTO multiAnalysis(BigModelAnalysisRequestDTO analysisInfo);
} }

119
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/service/impl/ZhipuAnalysisServiceImpl.java

@ -1,6 +1,8 @@
package com.hnac.hzims.bigmodel.zhipuai.service.impl; package com.hnac.hzims.bigmodel.zhipuai.service.impl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hnac.hzims.bigmodel.api.dto.BigModelAnalysisRequestDTO;
import com.hnac.hzims.bigmodel.api.dto.BigModelAnalysisResponseDTO;
import com.hnac.hzims.bigmodel.zhipuai.constants.BaseEnum; import com.hnac.hzims.bigmodel.zhipuai.constants.BaseEnum;
import com.hnac.hzims.bigmodel.zhipuai.constants.ResultStrategyType; import com.hnac.hzims.bigmodel.zhipuai.constants.ResultStrategyType;
import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity; import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity;
@ -15,13 +17,15 @@ import com.hnac.hzims.common.utils.FileUtil;
import com.hnac.hzinfo.exception.HzServiceException; import com.hnac.hzinfo.exception.HzServiceException;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.api.ResultCode;
import org.springblade.core.tool.jackson.JsonUtil; import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.core.tool.utils.StringUtil; import org.springblade.core.tool.utils.StringUtil;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import java.util.Objects; import java.util.*;
import java.util.Optional;
/** /**
* @Author: ypj * @Author: ypj
@ -36,6 +40,14 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService {
private final ZhipuAnalysisInfoService infoService; private final ZhipuAnalysisInfoService infoService;
private final String multiQuestionBrief = "从给出的图片回答以下{}个问题\n";
private final String multiQuestionPrefix = "第{}个问题,";
private final String multiQuestionConstrains = "- 不要在输出中添加任何注释和思考过程和其他多余的内容,以JSON格式输出简单结果,{}结果只包含'是'或者'否'\n";
private final String multiQuestionConstrainsList = "'问题{}'代表第{}个问题,";
@Override @Override
public ZhipuAnalysisFileResponse analysis(ZhipuAnalysisFileRequest request) { public ZhipuAnalysisFileResponse analysis(ZhipuAnalysisFileRequest request) {
Assert.isTrue(StringUtil.isNotBlank(request.getFilePath()) || StringUtil.isNotBlank(request.getUrl()), () -> { Assert.isTrue(StringUtil.isNotBlank(request.getFilePath()) || StringUtil.isNotBlank(request.getUrl()), () -> {
@ -49,7 +61,21 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService {
}); });
ZhipuAnalyser analyser = analysisFactory.getAnalysisStrategy(info.getModel()); ZhipuAnalyser analyser = analysisFactory.getAnalysisStrategy(info.getModel());
Object resultObject = analyser.getResultValue(info, getSendUrl(request)); Assert.isTrue(StringUtil.isNotBlank(info.getRequestContent()), () -> {
throw new HzServiceException("智谱平台交互内容为空");
});
//拼接文字内容
StringBuilder textBuilder = new StringBuilder();
textBuilder.append(ZhipuAnalyser.QUESTION_PROFILE).append(info.getRequestContent());
if (StringUtil.isNotBlank(info.getRequestConstrains())) {
textBuilder.append(ZhipuAnalyser.QUESTION_CONSTRAINS).append(info.getRequestConstrains());
}
if (StringUtil.isNotBlank(info.getRequestOutputFormat())) {
textBuilder.append(ZhipuAnalyser.QUESTION_OUTPUT_FORMAT).append("{").append(info.getRequestOutputFormat()).append("}");
}
analyser.setText(textBuilder.toString());
Object resultObject = analyser.getResultValue(analyser.getText(), info.getResultKey(), getSendUrl(request));
Assert.isTrue(Objects.nonNull(resultObject), () -> { Assert.isTrue(Objects.nonNull(resultObject), () -> {
throw new HzServiceException("智谱平台分析失败"); throw new HzServiceException("智谱平台分析失败");
}); });
@ -82,11 +108,12 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService {
@Override @Override
public ZhipuAnalysisInfoEntity getAnalysisInfo(ZhipuAnalysisFileRequest request) { public ZhipuAnalysisInfoEntity getAnalysisInfo(ZhipuAnalysisFileRequest request) {
ZhipuAnalysisInfoEntity result = null; ZhipuAnalysisInfoEntity result = null;
if (StringUtil.isBlank(request.getCode())) { if (StringUtil.isBlank(request.getCode()) && StringUtil.isBlank(request.getCheckTypeSon())) {
result = request.toAnalysisInfo(); result = request.toAnalysisInfo();
} else { } else {
result = infoService.getOne(Wrappers.<ZhipuAnalysisInfoEntity>lambdaQuery() result = infoService.getOne(Wrappers.<ZhipuAnalysisInfoEntity>lambdaQuery()
.eq(ZhipuAnalysisInfoEntity::getCode, request.getCode()) .eq(StringUtil.isNotBlank(request.getCode()), ZhipuAnalysisInfoEntity::getCode, request.getCode())
.eq(StringUtil.isNotBlank(request.getCheckTypeSon()), ZhipuAnalysisInfoEntity::getCheckTypeSon, request.getCheckTypeSon())
.last("limit 1;")); .last("limit 1;"));
} }
log.info("the analysis information is {}", Optional.ofNullable(result).map(r -> JsonUtil.toJson(r)).orElse("null")); log.info("the analysis information is {}", Optional.ofNullable(result).map(r -> JsonUtil.toJson(r)).orElse("null"));
@ -94,4 +121,86 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService {
} }
@Override
public BigModelAnalysisResponseDTO multiAnalysis(BigModelAnalysisRequestDTO request) {
BigModelAnalysisResponseDTO response = new BigModelAnalysisResponseDTO();
try {
Assert.isTrue(StringUtil.isNotBlank(request.getUrl()), () -> {
throw new HzServiceException("文件为空");
});
Assert.isTrue(CollectionUtil.isNotEmpty(request.getCodeList()) || CollectionUtil.isNotEmpty(request.getCheckTypeSonList()), () -> {
throw new HzServiceException("识别类型为空");
});
List<ZhipuAnalysisInfoEntity> infoList = infoService.list(Wrappers.<ZhipuAnalysisInfoEntity>lambdaQuery().in(CollectionUtil.isNotEmpty(request.getCodeList()), ZhipuAnalysisInfoEntity::getCode, request.getCodeList())
.in(CollectionUtil.isNotEmpty(request.getCheckTypeSonList()), ZhipuAnalysisInfoEntity::getCheckTypeSon, request.getCheckTypeSonList()));
Assert.isTrue(CollectionUtil.isNotEmpty(infoList), () -> {
throw new HzServiceException("未知的识别类型");
});
ZhipuAnalyser analyser = analysisFactory.getAnalysisStrategy(infoList.get(0).getModel());
String text = getMultiAnalyserText(infoList);
Map<String, Object> resultMap = analyser.getResultMap(text, request.getUrl());
if (null == resultMap) {
response.setCode(ResultCode.FAILURE.getCode());
response.setMsg("智谱大模型分析失败");
return response;
}
List<BigModelAnalysisResponseDTO.BigModelAnalysisResult> results = new ArrayList<>(infoList.size());
for (int index = 1; index <= infoList.size(); index++) {
ZhipuAnalysisInfoEntity infoEntity = infoList.get(index - 1);
BigModelAnalysisResponseDTO.BigModelAnalysisResult result = new BigModelAnalysisResponseDTO.BigModelAnalysisResult();
results.add(result);
result.setCheckTypeSon(infoEntity.getCheckTypeSon());
Object resultObj = resultMap.get(infoEntity.getResultKey());
if (ObjectUtil.isEmpty(resultObj)) {
continue;
}
String resultStr = resultObj.toString();
result.setResultStr(resultStr);
result.setDescription(infoEntity.getName() + StringUtil.format(",结果为{}", resultStr));
ResultStrategyType strategyType = BaseEnum.getInstance(infoEntity.getResultStrategy(), ResultStrategyType.class);
if (ObjectUtil.isEmpty(strategyType)) {
continue;
}
result.setResult(strategyType.getStrategy().getResult(infoEntity, resultObj));
}
response.setData(results);
response.setCode(ResultCode.SUCCESS.getCode());
response.setMsg(ResultCode.SUCCESS.getMessage());
} catch (Exception e) {
if (e instanceof HzServiceException) {
response.setCode(ResultCode.FAILURE.getCode());
response.setMsg(e.getMessage());
} else {
response.setCode(ResultCode.FAILURE.getCode());
response.setMsg("智谱大模型分析失败");
}
log.error("智谱大模型分析失败", e);
}
return response;
}
String getMultiAnalyserText(List<ZhipuAnalysisInfoEntity> infoList) {
StringBuilder stringBuilder = new StringBuilder();
StringBuilder titleBuilder = new StringBuilder();
StringBuilder formatBuilder = new StringBuilder();
stringBuilder.append(ZhipuAnalyser.QUESTION_PROFILE)
.append(StringUtil.format(multiQuestionBrief, infoList.size()));
formatBuilder.append("{");
for (int index = 1; index <= infoList.size(); index++) {
ZhipuAnalysisInfoEntity infoEntity = infoList.get(index - 1);
stringBuilder.append(StringUtil.format(multiQuestionPrefix, index));
titleBuilder.append(StringUtil.format(multiQuestionConstrainsList, infoEntity.getResultKey(), index));
formatBuilder.append(infoEntity.getRequestOutputFormat()).append(",");
}
formatBuilder.deleteCharAt(formatBuilder.length() - 1);
formatBuilder.append("}");
stringBuilder.append(ZhipuAnalyser.QUESTION_CONSTRAINS)
.append(StringUtil.format(multiQuestionConstrains, titleBuilder.toString()))
.append(ZhipuAnalyser.QUESTION_OUTPUT_FORMAT).append(formatBuilder);
return stringBuilder.toString();
}
} }

11
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisFileRequest.java

@ -1,6 +1,6 @@
package com.hnac.hzims.bigmodel.zhipuai.vo; package com.hnac.hzims.bigmodel.zhipuai.vo;
import com.hnac.hzims.bigmodel.api.dto.BigModelAnalysisDTO; import com.hnac.hzims.bigmodel.api.dto.BigModelAnalysisRequestDTO;
import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity; import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
@ -16,6 +16,9 @@ public class ZhipuAnalysisFileRequest {
@ApiModelProperty("策略编码") @ApiModelProperty("策略编码")
private String code; private String code;
@ApiModelProperty(value = "检测类型子类")
private String checkTypeSon;
@ApiModelProperty("模型名称") @ApiModelProperty("模型名称")
private String model; private String model;
@ -39,10 +42,4 @@ public class ZhipuAnalysisFileRequest {
return info; return info;
} }
public static ZhipuAnalysisFileRequest of(BigModelAnalysisDTO analysisDTO) {
ZhipuAnalysisFileRequest request = new ZhipuAnalysisFileRequest();
request.setCode(analysisDTO.getCode());
request.setUrl(analysisDTO.getUrl());
return request;
}
} }

2
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisResult.java

@ -21,7 +21,7 @@ public class ZhipuAnalysisResult<T> implements Serializable {
private Boolean success = null; private Boolean success = null;
@ApiModelProperty(value = "承载数据") @ApiModelProperty(value = "承载数据")
private T result; private Object result;
@ApiModelProperty(value = "结果描述") @ApiModelProperty(value = "结果描述")
private String resultStr; private String resultStr;

4
hzims-service/inspect/pom.xml

@ -155,6 +155,10 @@
<artifactId>okhttp</artifactId> <artifactId>okhttp</artifactId>
<version>4.9.3</version> <version>4.9.3</version>
</dependency> </dependency>
<dependency>
<groupId>com.hnac.hzims</groupId>
<artifactId>big-model-api</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

62
hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/task/schedule/VideoTaskSchedule.java

@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.hnac.hzims.bigmodel.api.feign.IBigModelAnalysisClient;
import com.hnac.hzinfo.datasearch.soe.ISoeClient; import com.hnac.hzinfo.datasearch.soe.ISoeClient;
import com.hnac.hzinfo.datasearch.soe.vo.DeviceSoeVO; import com.hnac.hzinfo.datasearch.soe.vo.DeviceSoeVO;
import com.hnac.hzinfo.inspect.ai.entity.CameraInfoEntity; import com.hnac.hzinfo.inspect.ai.entity.CameraInfoEntity;
@ -13,6 +14,8 @@ import com.hnac.hzinfo.inspect.ai.service.ICameraInfoService;
import com.hnac.hzinfo.inspect.ai.service.IRobotTaskService; import com.hnac.hzinfo.inspect.ai.service.IRobotTaskService;
import com.hnac.hzinfo.inspect.areamonthly.vo.TaskObjectVO; import com.hnac.hzinfo.inspect.areamonthly.vo.TaskObjectVO;
import com.hnac.hzinfo.inspect.hikVideo.service.IHikApiService; import com.hnac.hzinfo.inspect.hikVideo.service.IHikApiService;
import com.hnac.hzinfo.inspect.obj.entity.ContentEntity;
import com.hnac.hzinfo.inspect.obj.services.ContentService;
import com.hnac.hzinfo.inspect.plan.PlanContants; import com.hnac.hzinfo.inspect.plan.PlanContants;
import com.hnac.hzinfo.inspect.task.TaskContants; import com.hnac.hzinfo.inspect.task.TaskContants;
import com.hnac.hzinfo.inspect.task.entity.EventEntity; import com.hnac.hzinfo.inspect.task.entity.EventEntity;
@ -34,12 +37,14 @@ import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.DateUtil; import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func; import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil; import org.springblade.core.tool.utils.StringUtil;
import org.springblade.resource.feign.IOssClient;
import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -63,9 +68,13 @@ public class VideoTaskSchedule {
private final ISoeClient soeClient; private final ISoeClient soeClient;
private final DataSourceTransactionManager dataSourceTransactionManager; private final DataSourceTransactionManager dataSourceTransactionManager;
private final TransactionDefinition transactionDefinition; private final TransactionDefinition transactionDefinition;
private final IOssClient ossClient;
private final ContentService contentService;
private final IBigModelAnalysisClient bigModelClient;
/** /**
* 视频巡检任务执行 * 视频巡检任务执行
*
* @param params 任务ID * @param params 任务ID
* @return * @return
*/ */
@ -74,15 +83,14 @@ public class VideoTaskSchedule {
public ReturnT<String> execute(String params) throws InterruptedException { public ReturnT<String> execute(String params) throws InterruptedException {
List<TaskEntity> taskList = Lists.newArrayList(); List<TaskEntity> taskList = Lists.newArrayList();
// 获取待执行的任务 // 获取待执行的任务
if(StringUtil.isBlank(params)) { if (StringUtil.isBlank(params)) {
LambdaQueryWrapper<TaskEntity> queryWrapper = Wrappers.<TaskEntity>lambdaQuery() LambdaQueryWrapper<TaskEntity> queryWrapper = Wrappers.<TaskEntity>lambdaQuery()
.eq(TaskEntity::getStatus, TaskContants.TaskStatusEnum.INIT_STATUS.getStatus()) .eq(TaskEntity::getStatus, TaskContants.TaskStatusEnum.INIT_STATUS.getStatus())
.le(TaskEntity::getPlanStartTime, LocalDateTime.now()) .le(TaskEntity::getPlanStartTime, LocalDateTime.now())
.ge(TaskEntity::getPlanEndTime, LocalDateTime.now()) .ge(TaskEntity::getPlanEndTime, LocalDateTime.now())
.eq(TaskEntity::getAutoVideo, PlanContants.InspectTypeEnum.VIDEO.getVal()); .eq(TaskEntity::getAutoVideo, PlanContants.InspectTypeEnum.VIDEO.getVal());
taskList.addAll(taskService.list(queryWrapper)); taskList.addAll(taskService.list(queryWrapper));
} } else {
else {
LambdaQueryWrapper<TaskEntity> queryWrapper = Wrappers.<TaskEntity>lambdaQuery() LambdaQueryWrapper<TaskEntity> queryWrapper = Wrappers.<TaskEntity>lambdaQuery()
.in(TaskEntity::getId, Func.toLongList(",", params)) .in(TaskEntity::getId, Func.toLongList(",", params))
.eq(TaskEntity::getStatus, TaskContants.TaskStatusEnum.UNDERWAY_STATUS.getStatus()) .eq(TaskEntity::getStatus, TaskContants.TaskStatusEnum.UNDERWAY_STATUS.getStatus())
@ -95,6 +103,7 @@ public class VideoTaskSchedule {
/** /**
* 执行视频巡检任务 * 执行视频巡检任务
*
* @param task 视频巡检任务 * @param task 视频巡检任务
*/ */
private void executeVideoTask(TaskEntity task) { private void executeVideoTask(TaskEntity task) {
@ -105,16 +114,15 @@ public class VideoTaskSchedule {
this.fillEvent(task); this.fillEvent(task);
// 更新视频巡检任务状态 // 更新视频巡检任务状态
taskService.update(Wrappers.<TaskEntity>lambdaUpdate() taskService.update(Wrappers.<TaskEntity>lambdaUpdate()
.set(TaskEntity::getStatus,TaskContants.TaskStatusEnum.FINISH_STATUS.getStatus()) .set(TaskEntity::getStatus, TaskContants.TaskStatusEnum.FINISH_STATUS.getStatus())
.set(TaskEntity::getEndTime,LocalDateTime.now()) .set(TaskEntity::getEndTime, LocalDateTime.now())
.eq(TaskEntity::getId,task.getId())); .eq(TaskEntity::getId, task.getId()));
} } catch (Exception e) {
catch(Exception e) {
e.printStackTrace(); e.printStackTrace();
XxlJobLogger.log("【taskId:"+task.getId()+"】视频巡检任务执行失败,报错信息为:"+e.getMessage()); XxlJobLogger.log("【taskId:" + task.getId() + "】视频巡检任务执行失败,报错信息为:" + e.getMessage());
} }
try { try {
Thread.sleep(3*1000); Thread.sleep(3 * 1000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -122,11 +130,12 @@ public class VideoTaskSchedule {
/** /**
* 填充视频巡检执行情况 * 填充视频巡检执行情况
*
* @param task 视频巡检任务 * @param task 视频巡检任务
*/ */
private void fillEvent(TaskEntity task) { private void fillEvent(TaskEntity task) {
List<com.hnac.hzinfo.inspect.task.vo.TaskObjectVO> taskObjects = taskService.getTaskById(task.getId()); List<com.hnac.hzinfo.inspect.task.vo.TaskObjectVO> taskObjects = taskService.getTaskById(task.getId());
List<TaskObjectVO> taskObjectList = JSONArray.parseArray(JSON.toJSONString(taskObjects),TaskObjectVO.class); List<TaskObjectVO> taskObjectList = JSONArray.parseArray(JSON.toJSONString(taskObjects), TaskObjectVO.class);
taskObjectList.forEach(object -> object.getProjects().forEach(project -> { taskObjectList.forEach(object -> object.getProjects().forEach(project -> {
EventEntity event = new EventEntity(); EventEntity event = new EventEntity();
event.setTaskId(task.getId()); event.setTaskId(task.getId());
@ -150,17 +159,17 @@ public class VideoTaskSchedule {
record.setUpdateUser(task.getUpdateUser()); record.setUpdateUser(task.getUpdateUser());
record.setCurrentStatus("1"); record.setCurrentStatus("1");
CameraInfoEntity camera = this.getCameraByProIdAndConId(project.getProjectId(), content.getContentId()); CameraInfoEntity camera = this.getCameraByProIdAndConId(project.getProjectId(), content.getContentId());
if(Func.isNotEmpty(camera)) { if (Func.isNotEmpty(camera)) {
// 获取计划时间段内视频的告警信息设置是否异常 // 获取计划时间段内视频的告警信息设置是否异常
if(Func.isNotEmpty(camera.getEmCode())) { if (Func.isNotEmpty(camera.getEmCode())) {
Result<HzPage<DeviceSoeVO>> deviceSoeR = soeClient.getDeviceCodeByTaosSoe(DateUtil.formatDateTime(task.getPlanStartTime()), DateUtil.formatDateTime(task.getPlanEndTime()), camera.getEmCode(), 0, String.valueOf(-1)); Result<HzPage<DeviceSoeVO>> deviceSoeR = soeClient.getDeviceCodeByTaosSoe(DateUtil.formatDateTime(task.getPlanStartTime()), DateUtil.formatDateTime(task.getPlanEndTime()), camera.getEmCode(), 0, String.valueOf(-1));
if(deviceSoeR.isSuccess() && CollectionUtil.isNotEmpty(deviceSoeR.getData().getRecords())) { if (deviceSoeR.isSuccess() && CollectionUtil.isNotEmpty(deviceSoeR.getData().getRecords())) {
record.setCurrentStatus("0"); record.setCurrentStatus("0");
} }
} }
// 调用海康威视api实现手动截屏回传至fileUrl // 调用海康威视api实现手动截屏回传至fileUrl
if(Func.isNotEmpty(camera.getPointCode())) { if (Func.isNotEmpty(camera.getPointCode())) {
String picUrl = hikApiService.manualCapture(camera.getPointCode()); String picUrl = hikApiService.manualCapture(camera.getPointCode());
record.setFileUrl(picUrl); record.setFileUrl(picUrl);
record.setCurrentValue(picUrl); record.setCurrentValue(picUrl);
@ -169,14 +178,15 @@ public class VideoTaskSchedule {
return record; return record;
}).collect(Collectors.toList()); }).collect(Collectors.toList());
eventRecordService.saveBatch(recordList); eventRecordService.saveBatch(recordList);
if(recordList.stream().anyMatch(r -> TaskContants.CurrentStatusEnum.abnormal.getType().equals(r.getCurrentStatus()))) { if (recordList.stream().anyMatch(r -> TaskContants.CurrentStatusEnum.abnormal.getType().equals(r.getCurrentStatus()))) {
eventService.update(Wrappers.<EventEntity>lambdaUpdate().set(EventEntity::getIsProblem,"1").eq(EventEntity::getId,event.getId())); eventService.update(Wrappers.<EventEntity>lambdaUpdate().set(EventEntity::getIsProblem, "1").eq(EventEntity::getId, event.getId()));
} }
})); }));
} }
/** /**
* 登记任务 * 登记任务
*
* @param task 视频巡检任务 * @param task 视频巡检任务
*/ */
private void registration(TaskEntity task) { private void registration(TaskEntity task) {
@ -188,7 +198,7 @@ public class VideoTaskSchedule {
taskUserEntity.setClaimStatus(TaskContants.ClaimStatusEnum.COLLECT.getStatus()); taskUserEntity.setClaimStatus(TaskContants.ClaimStatusEnum.COLLECT.getStatus());
taskUserEntity.setClaimTime(LocalDateTime.now()); taskUserEntity.setClaimTime(LocalDateTime.now());
taskUserService.updateById(taskUserEntity); taskUserService.updateById(taskUserEntity);
if(PlanContants.PlanMethodEnum.SEIZE.getMethod().equals(task.getMethod())) { if (PlanContants.PlanMethodEnum.SEIZE.getMethod().equals(task.getMethod())) {
taskUserService.removeByIds(taskUserList.stream().map(TaskUserEntity::getId).filter(id -> !taskUserEntity.getId().equals(id)).collect(Collectors.toList())); taskUserService.removeByIds(taskUserList.stream().map(TaskUserEntity::getId).filter(id -> !taskUserEntity.getId().equals(id)).collect(Collectors.toList()));
} }
// 更改任务状态 // 更改任务状态
@ -199,19 +209,31 @@ public class VideoTaskSchedule {
/** /**
* 根据项目ID以及内容ID获取摄像头编号 * 根据项目ID以及内容ID获取摄像头编号
*
* @param projectId 项目ID * @param projectId 项目ID
* @param contentId 内容ID * @param contentId 内容ID
* @return 摄像头编号 * @return 摄像头编号
*/ */
private CameraInfoEntity getCameraByProIdAndConId(Long projectId,Long contentId) { private CameraInfoEntity getCameraByProIdAndConId(Long projectId, Long contentId) {
RobotTaskEntity robotTaskEntity = robotTaskService.getOne(Wrappers.<RobotTaskEntity>lambdaQuery() RobotTaskEntity robotTaskEntity = robotTaskService.getOne(Wrappers.<RobotTaskEntity>lambdaQuery()
.eq(RobotTaskEntity::getProjectId, projectId) .eq(RobotTaskEntity::getProjectId, projectId)
.like(RobotTaskEntity::getContentIds, String.valueOf(contentId)) .like(RobotTaskEntity::getContentIds, String.valueOf(contentId))
); );
if(Func.isNotEmpty(robotTaskEntity)) { if (Func.isNotEmpty(robotTaskEntity)) {
return cameraInfoService.getById(robotTaskEntity.getCameraId()); return cameraInfoService.getById(robotTaskEntity.getCameraId());
} }
return null; return null;
} }
private void pictureHandle(String url, Long contentId) {
ContentEntity content = contentService.getById(contentId);
if (StringUtil.isBlank(content.getMultiCheckTypeSon())) {
return;
}
String[] typeArray = content.getMultiCheckTypeSon().split(",");
List<String> checkTypeSonList = Arrays.asList(typeArray);
}
} }

Loading…
Cancel
Save