diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfig.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfig.java index 71bf8a7..1c5a8b2 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfig.java +++ b/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.Configuration; -import javax.annotation.Resource; - /** * @Author: ypj * @Date: 2024/8/9 17:01 diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfigProperties.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfigProperties.java index bfb5a98..0ba8b63 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfigProperties.java +++ b/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 org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.cloud.context.config.annotation.RefreshScope; -import org.springframework.stereotype.Component; /** * @Author: ypj diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/AnalysisResultType.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/AnalysisResultType.java deleted file mode 100644 index 29e0c8e..0000000 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/AnalysisResultType.java +++ /dev/null @@ -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 clazz; - - AnalysisResultType(String code, String msg, String condition, Class clazz) { - this.code = code; - this.msg = msg; - this.condition = condition; - this.clazz = clazz; - } -} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/ComparisonType.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/ComparisonType.java new file mode 100644 index 0000000..4eb09ba --- /dev/null +++ b/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; + } +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/ResultDataType.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/ResultDataType.java new file mode 100644 index 0000000..e85b8b5 --- /dev/null +++ b/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; + } + +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/ResultStrategyType.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/ResultStrategyType.java new file mode 100644 index 0000000..8532bb9 --- /dev/null +++ b/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 strategyClass; + + ResultStrategyType(String code, String msg, Class 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; + } +} 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 cacf55a..3e7076f 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 @@ -12,7 +12,7 @@ import org.springblade.core.mp.base.BaseEntity; * @Author: ypj * @Date: 2024/8/8 15:17 */ -@ApiModel(value ="大模型分析信息") +@ApiModel(value = "大模型分析信息") @Data @EqualsAndHashCode(callSuper = false) @TableName("hzims_analysis_info") diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/AbstractZhipuAnalysisHandler.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/AbstractZhipuAnalysisHandler.java index 796fbb9..09fa6dd 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/AbstractZhipuAnalysisHandler.java +++ b/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 javax.annotation.Resource; -import java.util.Base64; /** * @Author: ypj @@ -11,7 +10,6 @@ import java.util.Base64; */ public abstract class AbstractZhipuAnalysisHandler implements ZhipuAnalyser { protected final String requestIdTemplate = "big_model-%d"; - protected final String resultKey = "结果"; @Resource protected ClientV4 client; diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ResultBooleanStrategy.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ResultBooleanStrategy.java new file mode 100644 index 0000000..c769c56 --- /dev/null +++ b/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 { + @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; + } + + +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ResultCommonStrategy.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ResultCommonStrategy.java new file mode 100644 index 0000000..b1c21fe --- /dev/null +++ b/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 { + @Override + public String getCode() { + return ResultStrategyType.COMMON.getCode(); + } + + + @Override + public Object getResult(ZhipuAnalysisInfoEntity info, Object data) { + return data; + } + +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ResultStrategy.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ResultStrategy.java new file mode 100644 index 0000000..3ae3c7e --- /dev/null +++ b/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 { + + 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); +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalyser.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalyser.java index b7696ba..9de7349 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalyser.java +++ b/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; import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity; +import org.springblade.core.tool.jackson.JsonUtil; + +import java.util.Map; /** * @Author: ypj @@ -12,8 +15,8 @@ public interface ZhipuAnalyser { String getAnalysisModel(); - default boolean isSupport(String modelName) { - return getAnalysisStrategy().equals(modelName); + default boolean isSupport(String strategyCode) { + return getAnalysisStrategy().equals(strategyCode); } Resp sendRequest(ZhipuAnalysisInfoEntity info, String url); @@ -21,4 +24,9 @@ public interface ZhipuAnalyser { Req getRequest(ZhipuAnalysisInfoEntity info, String url); String getResult(ZhipuAnalysisInfoEntity info, String url); + + default Object getResultValue(ZhipuAnalysisInfoEntity info, String url) { + Map resultMap = JsonUtil.toMap(getResult(info, url)); + return resultMap.get(info.getResultKey()); + } } 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 5c8f425..23c7767 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 @@ -1,6 +1,5 @@ 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.Constants; import com.zhipu.oapi.service.v4.model.ChatCompletionRequest; @@ -36,7 +35,7 @@ public class ZhipuAnalysisHandlerGlm4v extends AbstractZhipuAnalysisHandler { @Override public ModelApiResponse sendRequest(ZhipuAnalysisInfoEntity info, String 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); } @@ -71,15 +70,17 @@ 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 : {}", 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()) { - String resultJson = response.getData().getChoices().get(0).getMessage().getContent().toString(); - resultJson = resultJson.substring(resultJson.indexOf("{"), resultJson.indexOf("}") + 1).trim(); - Map resultMap = JsonUtil.toMap(resultJson); - return resultMap.get(resultKey).toString(); - } else { - return "访问失败"; + try { + String resultJson = response.getData().getChoices().get(0).getMessage().getContent().toString(); + resultJson = resultJson.substring(resultJson.indexOf("{"), resultJson.lastIndexOf("}") + 1).trim(); + return resultJson; + } catch (Exception e) { + log.info("get data from response error", e); + } } + return null; } } 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 401052e..f2b2708 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,7 +1,10 @@ package com.hnac.hzims.bigmodel.zhipuai.service.impl; 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.handler.ResultStrategy; import com.hnac.hzims.bigmodel.zhipuai.handler.ZhipuAnalyser; import com.hnac.hzims.bigmodel.zhipuai.handler.ZhipuAnalysisFactory; 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.util.Assert; +import java.util.Objects; import java.util.Optional; /** @@ -37,12 +41,32 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService { Assert.isTrue(StringUtil.isNotBlank(request.getFilePath()) || StringUtil.isNotBlank(request.getUrl()), () -> { throw new HzServiceException("文件为空"); }); + ZhipuAnalysisFileResponse response = new ZhipuAnalysisFileResponse(); + ZhipuAnalysisInfoEntity info = getAnalysisInfo(request); + Assert.isTrue(Objects.nonNull(info), () -> { + throw new HzServiceException("没有找到对应的策略信息"); + }); + 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.setResult(conclusion); + response.setSuccess(Boolean.TRUE); return response; } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisFileResponse.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisFileResponse.java index 1755fad..dda5806 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisFileResponse.java +++ b/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; import io.swagger.annotations.ApiModel; -import io.swagger.annotations.ApiModelProperty; import lombok.Data; /** @@ -9,9 +8,7 @@ import lombok.Data; * @Date: 2024/8/12 10:23 */ @Data -@ApiModel(value = "智谱分析返回",description = "智谱分析返回") -public class ZhipuAnalysisFileResponse { +@ApiModel(value = "智谱分析返回", description = "智谱分析返回") +public class ZhipuAnalysisFileResponse extends ZhipuAnalysisResult { - @ApiModelProperty("返回结果") - String resultStr; } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisResult.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisResult.java new file mode 100644 index 0000000..65ef214 --- /dev/null +++ b/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 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; + +}