From ebcd55bbbeeabdcf775cb6f3a37e8255fcb2870a Mon Sep 17 00:00:00 2001 From: luyie Date: Fri, 23 Aug 2024 17:08:23 +0800 Subject: [PATCH] =?UTF-8?q?add:=E5=A4=A7=E6=A8=A1=E5=9E=8B=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=EF=BC=8C=E5=BC=82=E5=B8=B8=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bigmodel/zhipuai/constants/ErrorCode.java | 57 ++++++++++++++++++++++ .../zhipuai/handler/ResultBooleanStrategy.java | 16 ++++-- .../zhipuai/handler/ZhipuAnalysisHandlerGlm4v.java | 8 +-- .../service/impl/ZhipuAnalysisServiceImpl.java | 55 +++++++++++---------- 4 files changed, 100 insertions(+), 36 deletions(-) create mode 100644 hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/ErrorCode.java 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 new file mode 100644 index 0000000..c6f5d74 --- /dev/null +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/ErrorCode.java @@ -0,0 +1,57 @@ +package com.hnac.hzims.bigmodel.zhipuai.constants; + +import com.hnac.hzinfo.exception.HzServiceException; +import lombok.Getter; +import org.springblade.core.tool.api.IResultCode; +import org.springblade.core.tool.api.ResultCode; + +/** + * @Author: ypj + * @Date: 2024/8/22 11:17 + */ +@Getter +public enum ErrorCode implements IResultCode { + + GET_DATA_FAILURE(1888000001, "获取数据失败"), + ANALYSIS_FAILURE(1888000002, "分析失败"), + STRATEGY_NOT_FOUND(1888000003, "策略未找到"), + GET_FILE_CONTENT_FAILURE(1888000004, "获取文件内容失败"), + REQUEST_TEXT_IS_EMPTY(1888000005, "交互文字内容不能为空"), + EMPTY_ANALYSIS_TYPE(1888000006, "识别类型为空"), + UNKNOWN_ANALYSIS_TYPE(1888000007, "未知的识别类型"), + NO_COMPARATOR(1888000008, "未获取到对比对象"), + NO_COMPARISON_TYPE(1888000009, "未获取到对比类型"), + GET_RESULT_FAIL(1888000010, "获取结果失败"),; + + private final int code; + private final String msg; + + ErrorCode(int code, String msg) { + this.code = code; + this.msg = msg; + } + + + @Override + public String getMessage() { + return this.msg; + } + + public HzServiceException throwException() { + return new HzServiceException(this); + } + + public static HzServiceException throwCommonException(String msg) { + return new HzServiceException(new IResultCode() { + @Override + public String getMessage() { + return msg; + } + + @Override + public int getCode() { + return ResultCode.FAILURE.getCode(); + } + }); + } +} 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 index e2a85bd..477c3a8 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/ResultBooleanStrategy.java @@ -2,9 +2,9 @@ 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.ErrorCode; import com.hnac.hzims.bigmodel.zhipuai.constants.ResultStrategyType; import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity; -import com.hnac.hzinfo.exception.HzServiceException; import org.springframework.util.Assert; import java.util.Objects; @@ -23,18 +23,24 @@ public class ResultBooleanStrategy implements ResultStrategy { public Boolean getResult(ZhipuAnalysisInfoEntity info, Object resultData) { Object comparator = convertOriginalData(info.getResultCondition(), info.getResultType()); Assert.isTrue(Objects.nonNull(comparator), () -> { - throw new HzServiceException("没有获取到对比对象"); + throw ErrorCode.NO_COMPARATOR.throwException(); }); ComparisonType comparisonType = BaseEnum.getInstance(info.getResultJudgeModel(), ComparisonType.class); - switch (comparisonType) { + Assert.isTrue(Objects.nonNull(comparisonType), () -> { + throw ErrorCode.NO_COMPARISON_TYPE.throwException(); + }); + switch (Objects.requireNonNull(comparisonType)) { case EQUAL: return comparator.equals(resultData); case GREATER: - return ((Comparable) comparator).compareTo(resultData) < 0; + if (comparator instanceof Comparable && resultData instanceof Comparable) { + return ((Comparable) comparator).compareTo((Comparable)resultData) < 0; + } + break; case LESS: return ((Comparable) comparator).compareTo(resultData) > 0; } - return null; + throw ErrorCode.GET_RESULT_FAIL.throwException(); } } 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 0f0925a..6e98f7b 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,7 @@ package com.hnac.hzims.bigmodel.zhipuai.handler; import cn.hutool.core.lang.Assert; +import com.hnac.hzims.bigmodel.zhipuai.constants.ErrorCode; import com.hnac.hzinfo.exception.HzServiceException; import com.zhipu.oapi.Constants; import com.zhipu.oapi.service.v4.model.ChatCompletionRequest; @@ -58,14 +59,13 @@ public class ZhipuAnalysisHandlerGlm4v extends AbstractZhipuAnalysisHandler { messages.add(chatMessage); String requestId = String.format(requestIdTemplate, System.currentTimeMillis()); - ChatCompletionRequest request = ChatCompletionRequest.builder() + return ChatCompletionRequest.builder() .model(getAnalysisModel()) .stream(Boolean.FALSE) .invokeMethod(Constants.invokeMethod) .messages(messages) .requestId(requestId) .build(); - return request; } @Override @@ -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 new HzServiceException(response.getMsg()); + throw ErrorCode.throwCommonException(Optional.ofNullable(response).map(ModelApiResponse::getMsg).orElse("没有收到大模型平台响应")); }); try { String resultJson = response.getData().getChoices().get(0).getMessage().getContent().toString(); @@ -81,7 +81,7 @@ public class ZhipuAnalysisHandlerGlm4v extends AbstractZhipuAnalysisHandler { return resultJson; } catch (Exception e) { log.info("get data from response error", e); - throw new HzServiceException("获取数据失败"); + throw ErrorCode.GET_DATA_FAILURE.throwException(); } } 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 d3b9a78..0afde6f 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 @@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.hnac.hzims.bigmodel.api.dto.BigModelAnalysisRequestDTO; import com.hnac.hzims.bigmodel.api.dto.BigModelAnalysisResponseDTO; 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; @@ -41,29 +42,29 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService { private final ZhipuAnalysisInfoService infoService; - private final String multiQuestionBrief = "从给出的图片回答以下{}个问题\n"; + private static final String MULTI_QUESTION_BRIEF = "从给出的图片回答以下{}个问题\n"; - private final String multiQuestionPrefix = "第{}个问题,"; + private static final String MULTI_QUESTION_PREFIX = "第{}个问题,"; - private final String multiQuestionConstrains = "- 不要在输出中添加任何注释和思考过程和其他多余的内容,以JSON格式输出简单结果,{}结果只包含'是'或者'否'\n"; + private static final String MULTI_QUESTION_CONSTRAINS = "- 不要在输出中添加任何注释和思考过程和其他多余的内容,以JSON格式输出简单结果,{}结果只包含'是'或者'否'\n"; - private final String multiQuestionConstrainsList = "'{}'代表第{}个问题,"; + private static final String MULTI_QUESTION_CONSTRAINS_LIST = "'{}'代表第{}个问题,"; @Override public ZhipuAnalysisFileResponse analysis(ZhipuAnalysisFileRequest request) { Assert.isTrue(StringUtil.isNotBlank(request.getFilePath()) || StringUtil.isNotBlank(request.getUrl()), () -> { - throw new HzServiceException("文件为空"); + throw ErrorCode.GET_FILE_CONTENT_FAILURE.throwException(); }); ZhipuAnalysisFileResponse response = new ZhipuAnalysisFileResponse(); ZhipuAnalysisInfoEntity info = getAnalysisInfo(request); Assert.isTrue(Objects.nonNull(info), () -> { - throw new HzServiceException("没有找到对应的策略信息"); + throw ErrorCode.STRATEGY_NOT_FOUND.throwException(); }); ZhipuAnalyser analyser = analysisFactory.getAnalysisStrategy(info.getModel()); Assert.isTrue(StringUtil.isNotBlank(info.getRequestContent()), () -> { - throw new HzServiceException("智谱平台交互内容为空"); + throw ErrorCode.REQUEST_TEXT_IS_EMPTY.throwException(); }); //拼接文字内容 StringBuilder textBuilder = new StringBuilder(); @@ -78,13 +79,13 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService { Object resultObject = analyser.getResultValue(analyser.getText(), info.getResultKey(), getSendUrl(request)); Assert.isTrue(Objects.nonNull(resultObject), () -> { - throw new HzServiceException("智谱平台分析失败"); + throw ErrorCode.ANALYSIS_FAILURE.throwException(); }); String resultStr = resultObject.toString(); ResultStrategyType strategyType = BaseEnum.getInstance(info.getResultStrategy(), ResultStrategyType.class); Assert.isTrue(Objects.nonNull(strategyType), () -> { - throw new HzServiceException("没有找到对应的结果数据解析策略"); + throw ErrorCode.STRATEGY_NOT_FOUND.throwException(); }); ResultStrategy strategy = strategyType.getStrategy(); @@ -100,14 +101,14 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService { if (StringUtil.isNotBlank(request.getUrl())) { return request.getUrl(); } - if (StringUtil.isNotBlank(request.getFilePath())) { - try { - return FileUtil.getBase64(request.getFilePath()); - } catch (IOException e) { - throw new HzServiceException("获取文件内容失败"); - } + Assert.isTrue(StringUtil.isNotBlank(request.getFilePath()), () -> { + throw ErrorCode.GET_FILE_CONTENT_FAILURE.throwException(); + }); + try { + return FileUtil.getBase64(request.getFilePath()); + } catch (IOException e) { + throw ErrorCode.GET_FILE_CONTENT_FAILURE.throwException(); } - return null; } @Override @@ -121,7 +122,7 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService { .eq(StringUtil.isNotBlank(request.getCheckTypeSon()), ZhipuAnalysisInfoEntity::getCheckTypeSon, request.getCheckTypeSon()) .last("limit 1;")); } - log.info("the analysis information is {}", Optional.ofNullable(result).map(r -> JsonUtil.toJson(r)).orElse("null")); + log.debug("the analysis information is {}", Optional.ofNullable(result).map(r -> JsonUtil.toJson(r)).orElse("null")); return result; } @@ -131,15 +132,15 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService { BigModelAnalysisResponseDTO response = new BigModelAnalysisResponseDTO(); try { Assert.isTrue(StringUtil.isNotBlank(request.getUrl()), () -> { - throw new HzServiceException("文件为空"); + throw ErrorCode.GET_FILE_CONTENT_FAILURE.throwException(); }); Assert.isTrue(CollectionUtil.isNotEmpty(request.getCodeList()) || CollectionUtil.isNotEmpty(request.getCheckTypeSonList()), () -> { - throw new HzServiceException("识别类型为空"); + throw ErrorCode.EMPTY_ANALYSIS_TYPE.throwException(); }); List infoList = infoService.list(Wrappers.lambdaQuery().in(CollectionUtil.isNotEmpty(request.getCodeList()), ZhipuAnalysisInfoEntity::getCode, request.getCodeList()) .in(CollectionUtil.isNotEmpty(request.getCheckTypeSonList()), ZhipuAnalysisInfoEntity::getCheckTypeSon, request.getCheckTypeSonList())); Assert.isTrue(CollectionUtil.isNotEmpty(infoList), () -> { - throw new HzServiceException("未知的识别类型"); + throw ErrorCode.UNKNOWN_ANALYSIS_TYPE.throwException(); }); ZhipuAnalyser analyser = analysisFactory.getAnalysisStrategy(infoList.get(0).getModel()); @@ -148,7 +149,7 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService { if (null == resultMap) { response.setCode(ResultCode.FAILURE.getCode()); - response.setMsg("智谱大模型分析失败"); + response.setMsg(ErrorCode.ANALYSIS_FAILURE.getMsg()); return response; } @@ -184,9 +185,9 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService { response.setMsg(e.getMessage()); } else { response.setCode(ResultCode.FAILURE.getCode()); - response.setMsg("智谱大模型分析失败"); + response.setMsg(ErrorCode.ANALYSIS_FAILURE.getMsg()); } - log.error("智谱大模型分析失败", e); + log.error(ErrorCode.ANALYSIS_FAILURE.getMessage(), e); } return response; } @@ -196,18 +197,18 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService { StringBuilder titleBuilder = new StringBuilder(); StringBuilder formatBuilder = new StringBuilder(); stringBuilder.append(ZhipuAnalyser.QUESTION_PROFILE) - .append(StringUtil.format(multiQuestionBrief, infoList.size())); + .append(StringUtil.format(MULTI_QUESTION_BRIEF, infoList.size())); formatBuilder.append("{"); for (int index = 1; index <= infoList.size(); index++) { ZhipuAnalysisInfoEntity infoEntity = infoList.get(index - 1); - stringBuilder.append(StringUtil.format(multiQuestionPrefix, index)); - titleBuilder.append(StringUtil.format(multiQuestionConstrainsList, infoEntity.getResultKey(), index)); + stringBuilder.append(StringUtil.format(MULTI_QUESTION_PREFIX, index)); + titleBuilder.append(StringUtil.format(MULTI_QUESTION_CONSTRAINS_LIST, infoEntity.getResultKey(), index)); formatBuilder.append(infoEntity.getRequestOutputFormat()).append(","); } formatBuilder.deleteCharAt(formatBuilder.length() - 1); formatBuilder.append("}"); stringBuilder.append(ZhipuAnalyser.QUESTION_CONSTRAINS) - .append(StringUtil.format(multiQuestionConstrains, titleBuilder.toString())) + .append(StringUtil.format(MULTI_QUESTION_CONSTRAINS, titleBuilder.toString())) .append(ZhipuAnalyser.QUESTION_OUTPUT_FORMAT).append(formatBuilder); return stringBuilder.toString(); }