diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/ErrorCode.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/ErrorCode.java index c6f5d74..02f55cb 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/ErrorCode.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/ErrorCode.java @@ -21,7 +21,8 @@ public enum ErrorCode implements IResultCode { UNKNOWN_ANALYSIS_TYPE(1888000007, "未知的识别类型"), NO_COMPARATOR(1888000008, "未获取到对比对象"), NO_COMPARISON_TYPE(1888000009, "未获取到对比类型"), - GET_RESULT_FAIL(1888000010, "获取结果失败"),; + GET_RESULT_FAIL(1888000010, "获取结果失败"), + NO_ANSWER_FROM_PLATFORM(1888000011, "未从平台获取到答案"); private final int code; private final String 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 index e85b8b5..2f82a7e 100644 --- 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 @@ -1,31 +1,41 @@ package com.hnac.hzims.bigmodel.zhipuai.constants; +import com.hnac.hzims.bigmodel.zhipuai.handler.result.data.BooleanParser; +import com.hnac.hzims.bigmodel.zhipuai.handler.result.data.IntegerParser; +import com.hnac.hzims.bigmodel.zhipuai.handler.result.data.ResultDataParser; +import com.hnac.hzims.bigmodel.zhipuai.handler.result.data.StringParser; import lombok.Getter; +import lombok.extern.slf4j.Slf4j; /** * @Author: ypj * @Date: 2024/8/14 8:28 */ @Getter +@Slf4j 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"); + STRING("String", "String类型", StringParser.class), + BOOLEAN("Boolean", "Boolean类型", BooleanParser.class), + INTEGER("Integer", "Integer类型", IntegerParser.class); private final String code; private final String msg; - private final String className; + private final Class parser; - private final String method; - - ResultDataType(String code, String msg, String className, String method) { + ResultDataType(String code, String msg, Class parser) { this.code = code; this.msg = msg; - this.className = className; - this.method = method; + this.parser = parser; } + public ResultDataParser getParser() { + try { + return parser.newInstance(); + } catch (Exception e) { + log.error("get result data parse fail", e); + } + return null; + } } 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 index 8532bb9..9f04f2f 100644 --- 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 @@ -1,8 +1,8 @@ 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 com.hnac.hzims.bigmodel.zhipuai.handler.result.ResultBooleanStrategy; +import com.hnac.hzims.bigmodel.zhipuai.handler.result.ResultCommonStrategy; +import com.hnac.hzims.bigmodel.zhipuai.handler.result.ResultStrategy; import lombok.Getter; import lombok.extern.slf4j.Slf4j; 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 6e98f7b..36ad46c 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 @@ -73,7 +73,7 @@ public class ZhipuAnalysisHandlerGlm4v extends AbstractZhipuAnalysisHandler { ModelApiResponse response = sendRequest(text, url); log.info("get zhi pu ai response : {}", Optional.ofNullable(response).map(JsonUtil::toJson).orElse("null")); Assert.isTrue(Objects.nonNull(response) && response.isSuccess(), () -> { - throw ErrorCode.throwCommonException(Optional.ofNullable(response).map(ModelApiResponse::getMsg).orElse("没有收到大模型平台响应")); + throw ErrorCode.throwCommonException(Optional.ofNullable(response).map(ModelApiResponse::getMsg).orElse(ErrorCode.NO_ANSWER_FROM_PLATFORM.getMsg())); }); try { String resultJson = response.getData().getChoices().get(0).getMessage().getContent().toString(); 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/result/ResultBooleanStrategy.java similarity index 93% rename from hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ResultBooleanStrategy.java rename to hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/ResultBooleanStrategy.java index 477c3a8..3b7e000 100644 --- 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/result/ResultBooleanStrategy.java @@ -1,10 +1,11 @@ -package com.hnac.hzims.bigmodel.zhipuai.handler; +package com.hnac.hzims.bigmodel.zhipuai.handler.result; import com.hnac.hzims.bigmodel.zhipuai.constants.BaseEnum; import com.hnac.hzims.bigmodel.zhipuai.constants.ComparisonType; import com.hnac.hzims.bigmodel.zhipuai.constants.ErrorCode; import com.hnac.hzims.bigmodel.zhipuai.constants.ResultStrategyType; import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity; +import com.hnac.hzims.bigmodel.zhipuai.handler.result.ResultStrategy; import org.springframework.util.Assert; import java.util.Objects; 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/result/ResultCommonStrategy.java similarity index 79% rename from hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ResultCommonStrategy.java rename to hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/ResultCommonStrategy.java index b1c21fe..e506065 100644 --- 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/result/ResultCommonStrategy.java @@ -1,7 +1,8 @@ -package com.hnac.hzims.bigmodel.zhipuai.handler; +package com.hnac.hzims.bigmodel.zhipuai.handler.result; import com.hnac.hzims.bigmodel.zhipuai.constants.ResultStrategyType; import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity; +import com.hnac.hzims.bigmodel.zhipuai.handler.result.ResultStrategy; /** * @Author: ypj @@ -13,7 +14,6 @@ public class ResultCommonStrategy implements ResultStrategy { 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/result/ResultStrategy.java similarity index 56% rename from hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ResultStrategy.java rename to hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/ResultStrategy.java index b86adea..90c6bbf 100644 --- 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/result/ResultStrategy.java @@ -1,13 +1,11 @@ -package com.hnac.hzims.bigmodel.zhipuai.handler; +package com.hnac.hzims.bigmodel.zhipuai.handler.result; 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 com.hnac.hzims.bigmodel.zhipuai.handler.result.data.ResultDataParser; -import java.lang.reflect.Method; +import java.util.Optional; /** * @Author: ypj @@ -23,15 +21,9 @@ public interface ResultStrategy { 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); - } + if (null != type) { + ResultDataParser parser = type.getParser(); + return Optional.ofNullable(parser).map(p -> p.parse(originalData)).orElse(originalData); } return originalData; } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/data/BooleanParser.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/data/BooleanParser.java new file mode 100644 index 0000000..1af115a --- /dev/null +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/data/BooleanParser.java @@ -0,0 +1,12 @@ +package com.hnac.hzims.bigmodel.zhipuai.handler.result.data; + +/** + * @Author: ypj + * @Date: 2024/8/28 16:12 + */ +public class BooleanParser implements ResultDataParser { + @Override + public Boolean parse(String originalData) { + return Boolean.parseBoolean(originalData); + } +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/data/IntegerParser.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/data/IntegerParser.java new file mode 100644 index 0000000..cc0cfe5 --- /dev/null +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/data/IntegerParser.java @@ -0,0 +1,12 @@ +package com.hnac.hzims.bigmodel.zhipuai.handler.result.data; + +/** + * @Author: ypj + * @Date: 2024/8/28 16:46 + */ +public class IntegerParser implements ResultDataParser { + @Override + public Integer parse(String originalData) { + return Integer.parseInt(originalData); + } +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/data/ResultDataParser.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/data/ResultDataParser.java new file mode 100644 index 0000000..854e7e0 --- /dev/null +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/data/ResultDataParser.java @@ -0,0 +1,9 @@ +package com.hnac.hzims.bigmodel.zhipuai.handler.result.data; + +/** + * @Author: ypj + * @Date: 2024/8/28 16:04 + */ +public interface ResultDataParser { + T parse(String originalData); +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/data/StringParser.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/data/StringParser.java new file mode 100644 index 0000000..a9218e1 --- /dev/null +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/data/StringParser.java @@ -0,0 +1,12 @@ +package com.hnac.hzims.bigmodel.zhipuai.handler.result.data; + +/** + * @Author: ypj + * @Date: 2024/8/28 16:11 + */ +public class StringParser implements ResultDataParser { + @Override + public String parse(String originalData) { + return originalData; + } +} 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 0afde6f..956f6f2 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 @@ -7,7 +7,7 @@ import com.hnac.hzims.bigmodel.zhipuai.constants.BaseEnum; import com.hnac.hzims.bigmodel.zhipuai.constants.ErrorCode; 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.result.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;