diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/utils/ZhipuUtil.java b/hzims-biz-common/src/main/java/com/hnac/hzims/common/utils/FileUtil.java similarity index 84% rename from hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/utils/ZhipuUtil.java rename to hzims-biz-common/src/main/java/com/hnac/hzims/common/utils/FileUtil.java index cec9ca8..c6167b5 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/utils/ZhipuUtil.java +++ b/hzims-biz-common/src/main/java/com/hnac/hzims/common/utils/FileUtil.java @@ -1,4 +1,4 @@ -package com.hnac.hzims.bigmodel.zhipuai.utils; +package com.hnac.hzims.common.utils; import lombok.extern.slf4j.Slf4j; @@ -9,17 +9,17 @@ import java.util.Base64; /** * @Author: ypj - * @Date: 2024/8/12 21:07 + * @Date: 2024/8/13 9:35 */ @Slf4j -public class ZhipuUtil { - +public class FileUtil { public static byte[] getFileBytes(String filePath) { File file = new File(filePath); FileInputStream fileInputStream = null; + byte[] bytes = null; try { fileInputStream = new FileInputStream(file); - byte[] bytes = new byte[(int) file.length()]; + bytes = new byte[(int) file.length()]; fileInputStream.read(bytes); return bytes; } catch (Exception e) { @@ -32,7 +32,7 @@ public class ZhipuUtil { } } } - return null; + return bytes; } public static String getBase64(byte[] bytes) { diff --git a/hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/api/dto/BigModelAnalysisDTO.java b/hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/api/dto/BigModelAnalysisDTO.java index 3f0e8a3..5694e33 100644 --- a/hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/api/dto/BigModelAnalysisDTO.java +++ b/hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/api/dto/BigModelAnalysisDTO.java @@ -15,6 +15,6 @@ public class BigModelAnalysisDTO { @ApiModelProperty("策略编码") private String code; - @ApiModelProperty("文件地址") + @ApiModelProperty("文件地址或文件的base64文件内容") private String url; } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/controller/ZhipuController.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/controller/ZhipuAnalysisController.java similarity index 96% rename from hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/controller/ZhipuController.java rename to hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/controller/ZhipuAnalysisController.java index cbdfe95..e770857 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/controller/ZhipuController.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/controller/ZhipuAnalysisController.java @@ -23,7 +23,7 @@ import javax.validation.Valid; @RequestMapping("zhipu") @Api(value = "智谱", tags = "智谱") @AllArgsConstructor -public class ZhipuController { +public class ZhipuAnalysisController { private final ZhipuAnalysisService analysisService; diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/entity/ZhipuAnalysisInfoEntity.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/entity/ZhipuAnalysisInfoEntity.java index 7375afd..cacf55a 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/entity/ZhipuAnalysisInfoEntity.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/entity/ZhipuAnalysisInfoEntity.java @@ -50,4 +50,8 @@ public class ZhipuAnalysisInfoEntity extends BaseEntity { @TableField("RESULT_CONDITION") private String resultCondition; + @ApiModelProperty(value = "对比方式") + @TableField("RESULT_JUDGE_MODEL") + private String resultJudgeModel; + } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalysisFactory.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalysisFactory.java index 34f390e..e4945b3 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalysisFactory.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalysisFactory.java @@ -22,15 +22,15 @@ public class ZhipuAnalysisFactory { private static final Map analysisStrategyMap = new ConcurrentHashMap<>(); - public ZhipuAnalyser getAnalysisStrategy(final String modelName) { - ZhipuAnalyser result = ZhipuAnalysisFactory.analysisStrategyMap.get(modelName); + public ZhipuAnalyser getAnalysisStrategy(final String strategyCode) { + ZhipuAnalyser result = ZhipuAnalysisFactory.analysisStrategyMap.get(strategyCode); if (Objects.nonNull(result)) { return result; } - Optional strategyOptional = analysisStrategyList.stream().filter(strategy -> strategy.isSupport(modelName)).findFirst(); + Optional strategyOptional = analysisStrategyList.stream().filter(strategy -> strategy.isSupport(strategyCode)).findFirst(); if (strategyOptional.isPresent()) { result = strategyOptional.get(); - ZhipuAnalysisFactory.analysisStrategyMap.put(modelName, result); + ZhipuAnalysisFactory.analysisStrategyMap.put(strategyCode, result); } return result; } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalysisHandlerGlm4v.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalysisHandlerGlm4v.java index e24865b..5c8f425 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalysisHandlerGlm4v.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalysisHandlerGlm4v.java @@ -2,7 +2,6 @@ package com.hnac.hzims.bigmodel.zhipuai.handler; import com.hnac.hzims.bigmodel.zhipuai.constants.AnalysisModel; import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity; -import com.zhipu.oapi.ClientV4; import com.zhipu.oapi.Constants; import com.zhipu.oapi.service.v4.model.ChatCompletionRequest; import com.zhipu.oapi.service.v4.model.ChatMessage; @@ -13,10 +12,7 @@ import lombok.extern.slf4j.Slf4j; import org.springblade.core.tool.jackson.JsonUtil; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * @Author: ypj @@ -29,18 +25,18 @@ public class ZhipuAnalysisHandlerGlm4v extends AbstractZhipuAnalysisHandler { @Override public String getAnalysisStrategy() { - return AnalysisModel.GLM_4V.getCode(); + return Constants.ModelChatGLM4V; } @Override public String getAnalysisModel() { - return AnalysisModel.GLM_4V.getCode(); + return Constants.ModelChatGLM4V; } @Override public ModelApiResponse sendRequest(ZhipuAnalysisInfoEntity info, String url) { ChatCompletionRequest request = getRequest(info, url); - log.info("send zhipu ai request :{}", request); + log.info("send zhipu ai request :{}", Optional.ofNullable(request).map(i -> JsonUtil.toJson(i)).orElse("null")); return client.invokeModelApi(request); } @@ -75,7 +71,7 @@ public class ZhipuAnalysisHandlerGlm4v extends AbstractZhipuAnalysisHandler { @Override public String getResult(ZhipuAnalysisInfoEntity info, String url) { ModelApiResponse response = sendRequest(info, url); - log.info("get zhi pu ai response : {}", JsonUtil.toJson(response)); + log.info("get zhi pu ai response : {}", Optional.ofNullable(response).map(r -> JsonUtil.toJson(r)).orElse("null")); if (response.isSuccess()) { String resultJson = response.getData().getChoices().get(0).getMessage().getContent().toString(); resultJson = resultJson.substring(resultJson.indexOf("{"), resultJson.indexOf("}") + 1).trim(); diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/service/impl/ZhipuAnalysisServiceImpl.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/service/impl/ZhipuAnalysisServiceImpl.java index 6ea5bd6..401052e 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/service/impl/ZhipuAnalysisServiceImpl.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/service/impl/ZhipuAnalysisServiceImpl.java @@ -1,20 +1,23 @@ package com.hnac.hzims.bigmodel.zhipuai.service.impl; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity; import com.hnac.hzims.bigmodel.zhipuai.handler.ZhipuAnalyser; import com.hnac.hzims.bigmodel.zhipuai.handler.ZhipuAnalysisFactory; +import com.hnac.hzims.bigmodel.zhipuai.service.ZhipuAnalysisInfoService; import com.hnac.hzims.bigmodel.zhipuai.service.ZhipuAnalysisService; -import com.hnac.hzims.bigmodel.zhipuai.utils.ZhipuUtil; import com.hnac.hzims.bigmodel.zhipuai.vo.ZhipuAnalysisFileRequest; import com.hnac.hzims.bigmodel.zhipuai.vo.ZhipuAnalysisFileResponse; +import com.hnac.hzims.common.utils.FileUtil; import com.hnac.hzinfo.exception.HzServiceException; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springblade.core.tool.jackson.JsonUtil; import org.springblade.core.tool.utils.StringUtil; import org.springframework.stereotype.Service; import org.springframework.util.Assert; -import java.util.Objects; +import java.util.Optional; /** * @Author: ypj @@ -24,11 +27,14 @@ import java.util.Objects; @AllArgsConstructor @Slf4j public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService { + private final ZhipuAnalysisFactory analysisFactory; + private final ZhipuAnalysisInfoService infoService; + @Override public ZhipuAnalysisFileResponse analysis(ZhipuAnalysisFileRequest request) { - Assert.isTrue(StringUtil.isBlank(request.getFilePath()) && StringUtil.isBlank(request.getFileContent()) && StringUtil.isBlank(request.getUrl()), () -> { + Assert.isTrue(StringUtil.isNotBlank(request.getFilePath()) || StringUtil.isNotBlank(request.getUrl()), () -> { throw new HzServiceException("文件为空"); }); ZhipuAnalysisInfoEntity info = getAnalysisInfo(request); @@ -44,21 +50,24 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService { if (StringUtil.isNotBlank(request.getUrl())) { return request.getUrl(); } - if (StringUtil.isNotBlank(request.getFileContent())) { - return request.getFileContent(); - } if (StringUtil.isNotBlank(request.getFilePath())) { - return ZhipuUtil.getBase64(request.getFilePath()); + return FileUtil.getBase64(request.getFilePath()); } return null; } @Override public ZhipuAnalysisInfoEntity getAnalysisInfo(ZhipuAnalysisFileRequest request) { - if (Objects.isNull(request.getCode())) { - return request.toAnalysisInfo(); + ZhipuAnalysisInfoEntity result = null; + if (StringUtil.isBlank(request.getCode())) { + result = request.toAnalysisInfo(); + } else { + result = infoService.getOne(Wrappers.lambdaQuery() + .eq(ZhipuAnalysisInfoEntity::getCode, request.getCode()) + .last("limit 1;")); } - return null; + log.info("the analysis information is {}", Optional.ofNullable(result).map(r -> JsonUtil.toJson(r)).orElse("null")); + return result; } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisFileRequest.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisFileRequest.java index 50a0fae..451c4aa 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisFileRequest.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisFileRequest.java @@ -19,15 +19,12 @@ public class ZhipuAnalysisFileRequest { @ApiModelProperty("模型名称") private String model; - @ApiModelProperty("文件地址") + @ApiModelProperty("文件地址或文件的base64内容") private String url; @ApiModelProperty("本地文件路径") private String filePath; - @ApiModelProperty("文件内容") - private String fileContent; - @ApiModelProperty("发送内容") private String content; diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisInfoVO.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisInfoVO.java index 574dfd8..d5461b8 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisInfoVO.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisInfoVO.java @@ -1,5 +1,9 @@ package com.hnac.hzims.bigmodel.zhipuai.vo; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @@ -17,6 +21,10 @@ import javax.validation.constraints.NotNull; @Data @EqualsAndHashCode(callSuper = false) public class ZhipuAnalysisInfoVO { + @JsonSerialize(using = ToStringSerializer.class) + @ApiModelProperty(value = "主键id") + @TableId(value = "id", type = IdType.ASSIGN_ID) + private Long id; @ApiModelProperty(value = "策略编码") @NotNull @@ -44,6 +52,9 @@ public class ZhipuAnalysisInfoVO { @ApiModelProperty(value = "结果对比内容") private String resultCondition; + @ApiModelProperty(value = "对比方式") + private String resultJudgeModel; + public ZhipuAnalysisInfoEntity toEntity() { return BeanUtil.copy(this, ZhipuAnalysisInfoEntity.class); }