Browse Source

add:智谱对接增加数据,调试

zhongwei
luyie 3 months ago
parent
commit
997d3e2b2c
  1. 12
      hzims-biz-common/src/main/java/com/hnac/hzims/common/utils/FileUtil.java
  2. 2
      hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/api/dto/BigModelAnalysisDTO.java
  3. 2
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/controller/ZhipuAnalysisController.java
  4. 4
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/entity/ZhipuAnalysisInfoEntity.java
  5. 8
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalysisFactory.java
  6. 14
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalysisHandlerGlm4v.java
  7. 29
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/service/impl/ZhipuAnalysisServiceImpl.java
  8. 5
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisFileRequest.java
  9. 11
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisInfoVO.java

12
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/utils/ZhipuUtil.java → 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; import lombok.extern.slf4j.Slf4j;
@ -9,17 +9,17 @@ import java.util.Base64;
/** /**
* @Author: ypj * @Author: ypj
* @Date: 2024/8/12 21:07 * @Date: 2024/8/13 9:35
*/ */
@Slf4j @Slf4j
public class ZhipuUtil { public class FileUtil {
public static byte[] getFileBytes(String filePath) { public static byte[] getFileBytes(String filePath) {
File file = new File(filePath); File file = new File(filePath);
FileInputStream fileInputStream = null; FileInputStream fileInputStream = null;
byte[] bytes = null;
try { try {
fileInputStream = new FileInputStream(file); fileInputStream = new FileInputStream(file);
byte[] bytes = new byte[(int) file.length()]; bytes = new byte[(int) file.length()];
fileInputStream.read(bytes); fileInputStream.read(bytes);
return bytes; return bytes;
} catch (Exception e) { } catch (Exception e) {
@ -32,7 +32,7 @@ public class ZhipuUtil {
} }
} }
} }
return null; return bytes;
} }
public static String getBase64(byte[] bytes) { public static String getBase64(byte[] bytes) {

2
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("策略编码") @ApiModelProperty("策略编码")
private String code; private String code;
@ApiModelProperty("文件地址") @ApiModelProperty("文件地址或文件的base64文件内容")
private String url; private String url;
} }

2
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/controller/ZhipuController.java → 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") @RequestMapping("zhipu")
@Api(value = "智谱", tags = "智谱") @Api(value = "智谱", tags = "智谱")
@AllArgsConstructor @AllArgsConstructor
public class ZhipuController { public class ZhipuAnalysisController {
private final ZhipuAnalysisService analysisService; private final ZhipuAnalysisService analysisService;

4
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") @TableField("RESULT_CONDITION")
private String resultCondition; private String resultCondition;
@ApiModelProperty(value = "对比方式")
@TableField("RESULT_JUDGE_MODEL")
private String resultJudgeModel;
} }

8
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<String, ZhipuAnalyser> analysisStrategyMap = new ConcurrentHashMap<>(); private static final Map<String, ZhipuAnalyser> analysisStrategyMap = new ConcurrentHashMap<>();
public ZhipuAnalyser getAnalysisStrategy(final String modelName) { public ZhipuAnalyser getAnalysisStrategy(final String strategyCode) {
ZhipuAnalyser result = ZhipuAnalysisFactory.analysisStrategyMap.get(modelName); ZhipuAnalyser result = ZhipuAnalysisFactory.analysisStrategyMap.get(strategyCode);
if (Objects.nonNull(result)) { if (Objects.nonNull(result)) {
return result; return result;
} }
Optional<ZhipuAnalyser> strategyOptional = analysisStrategyList.stream().filter(strategy -> strategy.isSupport(modelName)).findFirst(); Optional<ZhipuAnalyser> strategyOptional = analysisStrategyList.stream().filter(strategy -> strategy.isSupport(strategyCode)).findFirst();
if (strategyOptional.isPresent()) { if (strategyOptional.isPresent()) {
result = strategyOptional.get(); result = strategyOptional.get();
ZhipuAnalysisFactory.analysisStrategyMap.put(modelName, result); ZhipuAnalysisFactory.analysisStrategyMap.put(strategyCode, result);
} }
return result; return result;
} }

14
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.constants.AnalysisModel;
import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity; import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity;
import com.zhipu.oapi.ClientV4;
import com.zhipu.oapi.Constants; import com.zhipu.oapi.Constants;
import com.zhipu.oapi.service.v4.model.ChatCompletionRequest; import com.zhipu.oapi.service.v4.model.ChatCompletionRequest;
import com.zhipu.oapi.service.v4.model.ChatMessage; 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.springblade.core.tool.jackson.JsonUtil;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** /**
* @Author: ypj * @Author: ypj
@ -29,18 +25,18 @@ public class ZhipuAnalysisHandlerGlm4v extends AbstractZhipuAnalysisHandler {
@Override @Override
public String getAnalysisStrategy() { public String getAnalysisStrategy() {
return AnalysisModel.GLM_4V.getCode(); return Constants.ModelChatGLM4V;
} }
@Override @Override
public String getAnalysisModel() { public String getAnalysisModel() {
return AnalysisModel.GLM_4V.getCode(); return Constants.ModelChatGLM4V;
} }
@Override @Override
public ModelApiResponse sendRequest(ZhipuAnalysisInfoEntity info, String url) { public ModelApiResponse sendRequest(ZhipuAnalysisInfoEntity info, String url) {
ChatCompletionRequest request = getRequest(info, 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); return client.invokeModelApi(request);
} }
@ -75,7 +71,7 @@ public class ZhipuAnalysisHandlerGlm4v extends AbstractZhipuAnalysisHandler {
@Override @Override
public String getResult(ZhipuAnalysisInfoEntity info, String url) { public String getResult(ZhipuAnalysisInfoEntity info, String url) {
ModelApiResponse response = sendRequest(info, 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()) { if (response.isSuccess()) {
String resultJson = response.getData().getChoices().get(0).getMessage().getContent().toString(); String resultJson = response.getData().getChoices().get(0).getMessage().getContent().toString();
resultJson = resultJson.substring(resultJson.indexOf("{"), resultJson.indexOf("}") + 1).trim(); resultJson = resultJson.substring(resultJson.indexOf("{"), resultJson.indexOf("}") + 1).trim();

29
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; 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.entity.ZhipuAnalysisInfoEntity;
import com.hnac.hzims.bigmodel.zhipuai.handler.ZhipuAnalyser; import com.hnac.hzims.bigmodel.zhipuai.handler.ZhipuAnalyser;
import com.hnac.hzims.bigmodel.zhipuai.handler.ZhipuAnalysisFactory; 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.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.ZhipuAnalysisFileRequest;
import com.hnac.hzims.bigmodel.zhipuai.vo.ZhipuAnalysisFileResponse; import com.hnac.hzims.bigmodel.zhipuai.vo.ZhipuAnalysisFileResponse;
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.jackson.JsonUtil;
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.Optional;
/** /**
* @Author: ypj * @Author: ypj
@ -24,11 +27,14 @@ import java.util.Objects;
@AllArgsConstructor @AllArgsConstructor
@Slf4j @Slf4j
public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService { public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService {
private final ZhipuAnalysisFactory analysisFactory; private final ZhipuAnalysisFactory analysisFactory;
private final ZhipuAnalysisInfoService infoService;
@Override @Override
public ZhipuAnalysisFileResponse analysis(ZhipuAnalysisFileRequest request) { 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("文件为空"); throw new HzServiceException("文件为空");
}); });
ZhipuAnalysisInfoEntity info = getAnalysisInfo(request); ZhipuAnalysisInfoEntity info = getAnalysisInfo(request);
@ -44,21 +50,24 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService {
if (StringUtil.isNotBlank(request.getUrl())) { if (StringUtil.isNotBlank(request.getUrl())) {
return request.getUrl(); return request.getUrl();
} }
if (StringUtil.isNotBlank(request.getFileContent())) {
return request.getFileContent();
}
if (StringUtil.isNotBlank(request.getFilePath())) { if (StringUtil.isNotBlank(request.getFilePath())) {
return ZhipuUtil.getBase64(request.getFilePath()); return FileUtil.getBase64(request.getFilePath());
} }
return null; return null;
} }
@Override @Override
public ZhipuAnalysisInfoEntity getAnalysisInfo(ZhipuAnalysisFileRequest request) { public ZhipuAnalysisInfoEntity getAnalysisInfo(ZhipuAnalysisFileRequest request) {
if (Objects.isNull(request.getCode())) { ZhipuAnalysisInfoEntity result = null;
return request.toAnalysisInfo(); if (StringUtil.isBlank(request.getCode())) {
result = request.toAnalysisInfo();
} else {
result = infoService.getOne(Wrappers.<ZhipuAnalysisInfoEntity>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;
} }

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

@ -19,15 +19,12 @@ public class ZhipuAnalysisFileRequest {
@ApiModelProperty("模型名称") @ApiModelProperty("模型名称")
private String model; private String model;
@ApiModelProperty("文件地址") @ApiModelProperty("文件地址或文件的base64内容")
private String url; private String url;
@ApiModelProperty("本地文件路径") @ApiModelProperty("本地文件路径")
private String filePath; private String filePath;
@ApiModelProperty("文件内容")
private String fileContent;
@ApiModelProperty("发送内容") @ApiModelProperty("发送内容")
private String content; private String content;

11
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; 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 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;
@ -17,6 +21,10 @@ import javax.validation.constraints.NotNull;
@Data @Data
@EqualsAndHashCode(callSuper = false) @EqualsAndHashCode(callSuper = false)
public class ZhipuAnalysisInfoVO { public class ZhipuAnalysisInfoVO {
@JsonSerialize(using = ToStringSerializer.class)
@ApiModelProperty(value = "主键id")
@TableId(value = "id", type = IdType.ASSIGN_ID)
private Long id;
@ApiModelProperty(value = "策略编码") @ApiModelProperty(value = "策略编码")
@NotNull @NotNull
@ -44,6 +52,9 @@ public class ZhipuAnalysisInfoVO {
@ApiModelProperty(value = "结果对比内容") @ApiModelProperty(value = "结果对比内容")
private String resultCondition; private String resultCondition;
@ApiModelProperty(value = "对比方式")
private String resultJudgeModel;
public ZhipuAnalysisInfoEntity toEntity() { public ZhipuAnalysisInfoEntity toEntity() {
return BeanUtil.copy(this, ZhipuAnalysisInfoEntity.class); return BeanUtil.copy(this, ZhipuAnalysisInfoEntity.class);
} }

Loading…
Cancel
Save