Browse Source

add:智谱大模型,优化代码结构

zhongwei
luyie 3 months ago
parent
commit
2691f22a7a
  1. 3
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/ErrorCode.java
  2. 30
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/ResultDataType.java
  3. 6
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/ResultStrategyType.java
  4. 2
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalysisHandlerGlm4v.java
  5. 3
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/ResultBooleanStrategy.java
  6. 4
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/ResultCommonStrategy.java
  7. 20
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/ResultStrategy.java
  8. 12
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/data/BooleanParser.java
  9. 12
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/data/IntegerParser.java
  10. 9
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/data/ResultDataParser.java
  11. 12
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/result/data/StringParser.java
  12. 2
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/service/impl/ZhipuAnalysisServiceImpl.java

3
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, "未知的识别类型"), UNKNOWN_ANALYSIS_TYPE(1888000007, "未知的识别类型"),
NO_COMPARATOR(1888000008, "未获取到对比对象"), NO_COMPARATOR(1888000008, "未获取到对比对象"),
NO_COMPARISON_TYPE(1888000009, "未获取到对比类型"), NO_COMPARISON_TYPE(1888000009, "未获取到对比类型"),
GET_RESULT_FAIL(1888000010, "获取结果失败"),; GET_RESULT_FAIL(1888000010, "获取结果失败"),
NO_ANSWER_FROM_PLATFORM(1888000011, "未从平台获取到答案");
private final int code; private final int code;
private final String msg; private final String msg;

30
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; 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.Getter;
import lombok.extern.slf4j.Slf4j;
/** /**
* @Author: ypj * @Author: ypj
* @Date: 2024/8/14 8:28 * @Date: 2024/8/14 8:28
*/ */
@Getter @Getter
@Slf4j
public enum ResultDataType implements BaseEnum { public enum ResultDataType implements BaseEnum {
OBJECT("Object", "Object类型", Object.class.getName(), null), STRING("String", "String类型", StringParser.class),
STRING("String", "String类型", String.class.getName(), null), BOOLEAN("Boolean", "Boolean类型", BooleanParser.class),
BOOLEAN("Boolean","Boolean类型",Boolean.class.getName(),"parseBoolean"), INTEGER("Integer", "Integer类型", IntegerParser.class);
INTEGER("Integer","Integer类型",Integer.class.getName(),"parseInt");
private final String code; private final String code;
private final String msg; private final String msg;
private final String className; private final Class<? extends ResultDataParser> parser;
private final String method; ResultDataType(String code, String msg, Class<? extends ResultDataParser> parser) {
ResultDataType(String code, String msg, String className, String method) {
this.code = code; this.code = code;
this.msg = msg; this.msg = msg;
this.className = className; this.parser = parser;
this.method = method;
} }
public ResultDataParser getParser() {
try {
return parser.newInstance();
} catch (Exception e) {
log.error("get result data parse fail", e);
}
return null;
}
} }

6
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; package com.hnac.hzims.bigmodel.zhipuai.constants;
import com.hnac.hzims.bigmodel.zhipuai.handler.ResultBooleanStrategy; import com.hnac.hzims.bigmodel.zhipuai.handler.result.ResultBooleanStrategy;
import com.hnac.hzims.bigmodel.zhipuai.handler.ResultCommonStrategy; import com.hnac.hzims.bigmodel.zhipuai.handler.result.ResultCommonStrategy;
import com.hnac.hzims.bigmodel.zhipuai.handler.ResultStrategy; import com.hnac.hzims.bigmodel.zhipuai.handler.result.ResultStrategy;
import lombok.Getter; import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;

2
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); ModelApiResponse response = sendRequest(text, url);
log.info("get zhi pu ai response : {}", Optional.ofNullable(response).map(JsonUtil::toJson).orElse("null")); log.info("get zhi pu ai response : {}", Optional.ofNullable(response).map(JsonUtil::toJson).orElse("null"));
Assert.<HzServiceException>isTrue(Objects.nonNull(response) && response.isSuccess(), () -> { Assert.<HzServiceException>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 { try {
String resultJson = response.getData().getChoices().get(0).getMessage().getContent().toString(); String resultJson = response.getData().getChoices().get(0).getMessage().getContent().toString();

3
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ResultBooleanStrategy.java → 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.BaseEnum;
import com.hnac.hzims.bigmodel.zhipuai.constants.ComparisonType; import com.hnac.hzims.bigmodel.zhipuai.constants.ComparisonType;
import com.hnac.hzims.bigmodel.zhipuai.constants.ErrorCode; import com.hnac.hzims.bigmodel.zhipuai.constants.ErrorCode;
import com.hnac.hzims.bigmodel.zhipuai.constants.ResultStrategyType; 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.result.ResultStrategy;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import java.util.Objects; import java.util.Objects;

4
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ResultCommonStrategy.java → 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.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.result.ResultStrategy;
/** /**
* @Author: ypj * @Author: ypj
@ -13,7 +14,6 @@ public class ResultCommonStrategy implements ResultStrategy<Object> {
return ResultStrategyType.COMMON.getCode(); return ResultStrategyType.COMMON.getCode();
} }
@Override @Override
public Object getResult(ZhipuAnalysisInfoEntity info, Object data) { public Object getResult(ZhipuAnalysisInfoEntity info, Object data) {
return data; return data;

20
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ResultStrategy.java → 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.BaseEnum;
import com.hnac.hzims.bigmodel.zhipuai.constants.ResultDataType; import com.hnac.hzims.bigmodel.zhipuai.constants.ResultDataType;
import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity; import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity;
import org.slf4j.Logger; import com.hnac.hzims.bigmodel.zhipuai.handler.result.data.ResultDataParser;
import org.slf4j.LoggerFactory;
import org.springblade.core.tool.utils.StringUtil;
import java.lang.reflect.Method; import java.util.Optional;
/** /**
* @Author: ypj * @Author: ypj
@ -23,15 +21,9 @@ public interface ResultStrategy<T> {
default Object convertOriginalData(String originalData, String className) { default Object convertOriginalData(String originalData, String className) {
ResultDataType type = BaseEnum.getInstance(className, ResultDataType.class); ResultDataType type = BaseEnum.getInstance(className, ResultDataType.class);
if (null != type && StringUtil.isNotBlank(type.getMethod())) { if (null != type) {
try { ResultDataParser parser = type.getParser();
Class<?> clazz = Class.forName(type.getClassName()); return Optional.ofNullable(parser).map(p -> p.parse(originalData)).orElse(originalData);
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; return originalData;
} }

12
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<Boolean> {
@Override
public Boolean parse(String originalData) {
return Boolean.parseBoolean(originalData);
}
}

12
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<Integer> {
@Override
public Integer parse(String originalData) {
return Integer.parseInt(originalData);
}
}

9
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> {
T parse(String originalData);
}

12
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<String> {
@Override
public String parse(String originalData) {
return originalData;
}
}

2
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.ErrorCode;
import com.hnac.hzims.bigmodel.zhipuai.constants.ResultStrategyType; 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.result.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;

Loading…
Cancel
Save