Browse Source

add:智谱分析,增加结果解析

zhongwei
luyie 3 months ago
parent
commit
8b14fb1fc2
  1. 2
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfig.java
  2. 2
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfigProperties.java
  3. 32
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/AnalysisResultType.java
  4. 22
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/ComparisonType.java
  5. 31
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/ResultDataType.java
  6. 39
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/ResultStrategyType.java
  7. 2
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/entity/ZhipuAnalysisInfoEntity.java
  8. 2
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/AbstractZhipuAnalysisHandler.java
  9. 34
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ResultBooleanStrategy.java
  10. 22
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ResultCommonStrategy.java
  11. 44
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ResultStrategy.java
  12. 12
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalyser.java
  13. 19
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalysisHandlerGlm4v.java
  14. 28
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/service/impl/ZhipuAnalysisServiceImpl.java
  15. 7
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisFileResponse.java
  16. 28
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisResult.java

2
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfig.java

@ -6,8 +6,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
/** /**
* @Author: ypj * @Author: ypj
* @Date: 2024/8/9 17:01 * @Date: 2024/8/9 17:01

2
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfigProperties.java

@ -2,8 +2,6 @@ package com.hnac.hzims.bigmodel.zhipuai.config;
import lombok.Data; import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
/** /**
* @Author: ypj * @Author: ypj

32
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/AnalysisResultType.java

@ -1,32 +0,0 @@
package com.hnac.hzims.bigmodel.zhipuai.constants;
import lombok.Getter;
import org.apache.poi.ss.formula.functions.T;
import java.math.BigDecimal;
/**
* @Author: ypj
* @Date: 2024/8/8 15:27
*/
@Getter
public enum AnalysisResultType implements BaseEnum {
BOOLEAN("BOOLEAN", "是非判断", "是", Boolean.class),
NUMBER("NUMBER", "确定数量", null, Integer.class),
DECIMAL("DECIMAL", "获取数值", null, BigDecimal.class);
private final String code;
private final String msg;
private final String condition;
private final Class<T> clazz;
AnalysisResultType(String code, String msg, String condition, Class clazz) {
this.code = code;
this.msg = msg;
this.condition = condition;
this.clazz = clazz;
}
}

22
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/ComparisonType.java

@ -0,0 +1,22 @@
package com.hnac.hzims.bigmodel.zhipuai.constants;
import lombok.Getter;
/**
* @Author: ypj
* @Date: 2024/8/14 9:23
*/
@Getter
public enum ComparisonType implements BaseEnum {
EQUAL("equal", "相等"),
GREATER("greater","大于"),
LESS("less","小于");
private final String code;
private final String msg;
ComparisonType(String code, String msg) {
this.code = code;
this.msg = msg;
}
}

31
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/ResultDataType.java

@ -0,0 +1,31 @@
package com.hnac.hzims.bigmodel.zhipuai.constants;
import lombok.Getter;
/**
* @Author: ypj
* @Date: 2024/8/14 8:28
*/
@Getter
public enum ResultDataType implements BaseEnum {
OBJECT("Object", "Object类型", Object.class.getName(), null),
STRING("String", "String类型", String.class.getName(), null),
BOOLEAN("Boolean","Boolean类型",Boolean.class.getName(),"parseBoolean"),
INTEGER("Integer","Integer类型",Integer.class.getName(),"parseInt");
private final String code;
private final String msg;
private final String className;
private final String method;
ResultDataType(String code, String msg, String className, String method) {
this.code = code;
this.msg = msg;
this.className = className;
this.method = method;
}
}

39
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/ResultStrategyType.java

@ -0,0 +1,39 @@
package com.hnac.hzims.bigmodel.zhipuai.constants;
import com.hnac.hzims.bigmodel.zhipuai.handler.ResultBooleanStrategy;
import com.hnac.hzims.bigmodel.zhipuai.handler.ResultCommonStrategy;
import com.hnac.hzims.bigmodel.zhipuai.handler.ResultStrategy;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
/**
* @Author: ypj
* @Date: 2024/8/8 15:27
*/
@Getter
@Slf4j
public enum ResultStrategyType implements BaseEnum {
COMMON("ResultCommonStrategy", "普通值", ResultCommonStrategy.class),
BOOLEAN("ResultBooleanStrategy", "是非判断", ResultBooleanStrategy.class);
private final String code;
private final String msg;
private final Class<? extends ResultStrategy> strategyClass;
ResultStrategyType(String code, String msg, Class<? extends ResultStrategy> strategy) {
this.code = code;
this.msg = msg;
this.strategyClass = strategy;
}
public ResultStrategy getStrategy() {
try {
return strategyClass.newInstance();
} catch (Exception e) {
log.error("get result strategy fail", e);
}
return null;
}
}

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

@ -12,7 +12,7 @@ import org.springblade.core.mp.base.BaseEntity;
* @Author: ypj * @Author: ypj
* @Date: 2024/8/8 15:17 * @Date: 2024/8/8 15:17
*/ */
@ApiModel(value ="大模型分析信息") @ApiModel(value = "大模型分析信息")
@Data @Data
@EqualsAndHashCode(callSuper = false) @EqualsAndHashCode(callSuper = false)
@TableName("hzims_analysis_info") @TableName("hzims_analysis_info")

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

@ -3,7 +3,6 @@ package com.hnac.hzims.bigmodel.zhipuai.handler;
import com.zhipu.oapi.ClientV4; import com.zhipu.oapi.ClientV4;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Base64;
/** /**
* @Author: ypj * @Author: ypj
@ -11,7 +10,6 @@ import java.util.Base64;
*/ */
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";
protected final String resultKey = "结果";
@Resource @Resource
protected ClientV4 client; protected ClientV4 client;

34
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ResultBooleanStrategy.java

@ -0,0 +1,34 @@
package com.hnac.hzims.bigmodel.zhipuai.handler;
import com.hnac.hzims.bigmodel.zhipuai.constants.BaseEnum;
import com.hnac.hzims.bigmodel.zhipuai.constants.ComparisonType;
import com.hnac.hzims.bigmodel.zhipuai.constants.ResultStrategyType;
import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity;
/**
* @Author: ypj
* @Date: 2024/8/14 7:41
*/
public class ResultBooleanStrategy implements ResultStrategy<Boolean> {
@Override
public String getCode() {
return ResultStrategyType.BOOLEAN.getCode();
}
@Override
public Boolean getResult(ZhipuAnalysisInfoEntity info, Object resultData) {
Object comparator = convertOriginalData(info.getResultCondition(), info.getResultType());
ComparisonType comparisonType = BaseEnum.getInstance(info.getResultJudgeModel(), ComparisonType.class);
switch (comparisonType) {
case EQUAL:
return comparator.equals(resultData);
case GREATER:
return ((Comparable) comparator).compareTo(resultData) < 0;
case LESS:
return ((Comparable) comparator).compareTo(resultData) > 0;
}
return null;
}
}

22
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ResultCommonStrategy.java

@ -0,0 +1,22 @@
package com.hnac.hzims.bigmodel.zhipuai.handler;
import com.hnac.hzims.bigmodel.zhipuai.constants.ResultStrategyType;
import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity;
/**
* @Author: ypj
* @Date: 2024/8/13 10:33
*/
public class ResultCommonStrategy implements ResultStrategy<Object> {
@Override
public String getCode() {
return ResultStrategyType.COMMON.getCode();
}
@Override
public Object getResult(ZhipuAnalysisInfoEntity info, Object data) {
return data;
}
}

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

@ -0,0 +1,44 @@
package com.hnac.hzims.bigmodel.zhipuai.handler;
import com.hnac.hzims.bigmodel.zhipuai.constants.BaseEnum;
import com.hnac.hzims.bigmodel.zhipuai.constants.ResultDataType;
import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.tool.utils.StringUtil;
import java.lang.reflect.Method;
/**
* @Author: ypj
* @Date: 2024/8/13 10:00
*/
public interface ResultStrategy<T> {
String getCode();
default boolean isSupport(String strategyCode) {
return getCode().equals(strategyCode);
}
default Object convertOriginalData(String originalData, String className) {
ResultDataType type = BaseEnum.getInstance(className, ResultDataType.class);
if (null != type && StringUtil.isNotBlank(type.getMethod())) {
try {
Class<?> clazz = Class.forName(type.getClassName());
Method method = clazz.getMethod(type.getMethod(), String.class);
return method.invoke(null, originalData);
} catch (Exception ignore) {
Logger logger = LoggerFactory.getLogger(ResultStrategy.class);
logger.info("parse data error", ignore);
}
}
return originalData;
}
default T getResult(ZhipuAnalysisInfoEntity info, String originalData) {
return getResult(info, convertOriginalData(originalData, info.getResultType()));
}
T getResult(ZhipuAnalysisInfoEntity info, Object resultData);
}

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

@ -1,6 +1,9 @@
package com.hnac.hzims.bigmodel.zhipuai.handler; package com.hnac.hzims.bigmodel.zhipuai.handler;
import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity; import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity;
import org.springblade.core.tool.jackson.JsonUtil;
import java.util.Map;
/** /**
* @Author: ypj * @Author: ypj
@ -12,8 +15,8 @@ public interface ZhipuAnalyser<Req, Resp> {
String getAnalysisModel(); String getAnalysisModel();
default boolean isSupport(String modelName) { default boolean isSupport(String strategyCode) {
return getAnalysisStrategy().equals(modelName); return getAnalysisStrategy().equals(strategyCode);
} }
Resp sendRequest(ZhipuAnalysisInfoEntity info, String url); Resp sendRequest(ZhipuAnalysisInfoEntity info, String url);
@ -21,4 +24,9 @@ public interface ZhipuAnalyser<Req, Resp> {
Req getRequest(ZhipuAnalysisInfoEntity info, String url); Req getRequest(ZhipuAnalysisInfoEntity info, String url);
String getResult(ZhipuAnalysisInfoEntity info, String url); String getResult(ZhipuAnalysisInfoEntity info, String url);
default Object getResultValue(ZhipuAnalysisInfoEntity info, String url) {
Map<String, Object> resultMap = JsonUtil.toMap(getResult(info, url));
return resultMap.get(info.getResultKey());
}
} }

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

@ -1,6 +1,5 @@
package com.hnac.hzims.bigmodel.zhipuai.handler; 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.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity;
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;
@ -36,7 +35,7 @@ public class ZhipuAnalysisHandlerGlm4v extends AbstractZhipuAnalysisHandler {
@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 :{}", Optional.ofNullable(request).map(i -> JsonUtil.toJson(i)).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);
} }
@ -71,15 +70,17 @@ 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 : {}", Optional.ofNullable(response).map(r -> JsonUtil.toJson(r)).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()) {
String resultJson = response.getData().getChoices().get(0).getMessage().getContent().toString(); try {
resultJson = resultJson.substring(resultJson.indexOf("{"), resultJson.indexOf("}") + 1).trim(); String resultJson = response.getData().getChoices().get(0).getMessage().getContent().toString();
Map<String, Object> resultMap = JsonUtil.toMap(resultJson); resultJson = resultJson.substring(resultJson.indexOf("{"), resultJson.lastIndexOf("}") + 1).trim();
return resultMap.get(resultKey).toString(); return resultJson;
} else { } catch (Exception e) {
return "访问失败"; log.info("get data from response error", e);
}
} }
return null;
} }
} }

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

@ -1,7 +1,10 @@
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.zhipuai.constants.BaseEnum;
import com.hnac.hzims.bigmodel.zhipuai.constants.ResultStrategyType;
import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity; import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity;
import com.hnac.hzims.bigmodel.zhipuai.handler.ResultStrategy;
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.ZhipuAnalysisInfoService;
@ -17,6 +20,7 @@ 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; import java.util.Optional;
/** /**
@ -37,12 +41,32 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService {
Assert.isTrue(StringUtil.isNotBlank(request.getFilePath()) || StringUtil.isNotBlank(request.getUrl()), () -> { Assert.isTrue(StringUtil.isNotBlank(request.getFilePath()) || StringUtil.isNotBlank(request.getUrl()), () -> {
throw new HzServiceException("文件为空"); throw new HzServiceException("文件为空");
}); });
ZhipuAnalysisFileResponse response = new ZhipuAnalysisFileResponse();
ZhipuAnalysisInfoEntity info = getAnalysisInfo(request); ZhipuAnalysisInfoEntity info = getAnalysisInfo(request);
Assert.isTrue(Objects.nonNull(info), () -> {
throw new HzServiceException("没有找到对应的策略信息");
});
ZhipuAnalyser analyser = analysisFactory.getAnalysisStrategy(info.getModel()); ZhipuAnalyser analyser = analysisFactory.getAnalysisStrategy(info.getModel());
Object resultObject = analyser.getResultValue(info, getSendUrl(request));
Assert.isTrue(Objects.nonNull(resultObject), () -> {
throw new HzServiceException("智谱平台分析失败");
});
String resultStr = resultObject.toString();
ResultStrategyType strategyType = BaseEnum.getInstance(info.getResultStrategy(), ResultStrategyType.class);
Assert.isTrue(Objects.nonNull(strategyType), () -> {
throw new HzServiceException("没有找到对应的结果数据解析策略");
});
ResultStrategy strategy = strategyType.getStrategy();
Object resultValue = strategy.convertOriginalData(resultStr, info.getResultType());
Object conclusion = strategy.getResult(info, resultValue);
String resultStr = analyser.getResult(info, getSendUrl(request));
ZhipuAnalysisFileResponse response = new ZhipuAnalysisFileResponse();
response.setResultStr(resultStr); response.setResultStr(resultStr);
response.setResult(conclusion);
response.setSuccess(Boolean.TRUE);
return response; return response;
} }

7
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisFileResponse.java

@ -1,7 +1,6 @@
package com.hnac.hzims.bigmodel.zhipuai.vo; package com.hnac.hzims.bigmodel.zhipuai.vo;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
/** /**
@ -9,9 +8,7 @@ import lombok.Data;
* @Date: 2024/8/12 10:23 * @Date: 2024/8/12 10:23
*/ */
@Data @Data
@ApiModel(value = "智谱分析返回",description = "智谱分析返回") @ApiModel(value = "智谱分析返回", description = "智谱分析返回")
public class ZhipuAnalysisFileResponse { public class ZhipuAnalysisFileResponse<T> extends ZhipuAnalysisResult {
@ApiModelProperty("返回结果")
String resultStr;
} }

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

@ -0,0 +1,28 @@
package com.hnac.hzims.bigmodel.zhipuai.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
/**
* @Author: ypj
* @Date: 2024/8/13 16:18
*/
@Data
@ApiModel(description = "分析返回信息")
public class ZhipuAnalysisResult<T> implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "是否成功", required = true)
private boolean success;
@ApiModelProperty(value = "承载数据")
private T result;
@ApiModelProperty(value = "结果描述")
private String resultStr;
}
Loading…
Cancel
Save