|
|
@ -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.BigModelAnalysisRequestDTO; |
|
|
|
import com.hnac.hzims.bigmodel.api.dto.BigModelAnalysisResponseDTO; |
|
|
|
import com.hnac.hzims.bigmodel.api.dto.BigModelAnalysisResponseDTO; |
|
|
|
import com.hnac.hzims.bigmodel.zhipuai.constants.BaseEnum; |
|
|
|
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.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.ResultStrategy; |
|
|
@ -41,29 +42,29 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService { |
|
|
|
|
|
|
|
|
|
|
|
private final ZhipuAnalysisInfoService infoService; |
|
|
|
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 |
|
|
|
@Override |
|
|
|
public ZhipuAnalysisFileResponse analysis(ZhipuAnalysisFileRequest request) { |
|
|
|
public ZhipuAnalysisFileResponse analysis(ZhipuAnalysisFileRequest request) { |
|
|
|
Assert.isTrue(StringUtil.isNotBlank(request.getFilePath()) || StringUtil.isNotBlank(request.getUrl()), () -> { |
|
|
|
Assert.isTrue(StringUtil.isNotBlank(request.getFilePath()) || StringUtil.isNotBlank(request.getUrl()), () -> { |
|
|
|
throw new HzServiceException("文件为空"); |
|
|
|
throw ErrorCode.GET_FILE_CONTENT_FAILURE.throwException(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
ZhipuAnalysisFileResponse response = new ZhipuAnalysisFileResponse(); |
|
|
|
ZhipuAnalysisFileResponse response = new ZhipuAnalysisFileResponse(); |
|
|
|
|
|
|
|
|
|
|
|
ZhipuAnalysisInfoEntity info = getAnalysisInfo(request); |
|
|
|
ZhipuAnalysisInfoEntity info = getAnalysisInfo(request); |
|
|
|
Assert.isTrue(Objects.nonNull(info), () -> { |
|
|
|
Assert.isTrue(Objects.nonNull(info), () -> { |
|
|
|
throw new HzServiceException("没有找到对应的策略信息"); |
|
|
|
throw ErrorCode.STRATEGY_NOT_FOUND.throwException(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
ZhipuAnalyser analyser = analysisFactory.getAnalysisStrategy(info.getModel()); |
|
|
|
ZhipuAnalyser analyser = analysisFactory.getAnalysisStrategy(info.getModel()); |
|
|
|
Assert.isTrue(StringUtil.isNotBlank(info.getRequestContent()), () -> { |
|
|
|
Assert.isTrue(StringUtil.isNotBlank(info.getRequestContent()), () -> { |
|
|
|
throw new HzServiceException("智谱平台交互内容为空"); |
|
|
|
throw ErrorCode.REQUEST_TEXT_IS_EMPTY.throwException(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
//拼接文字内容
|
|
|
|
//拼接文字内容
|
|
|
|
StringBuilder textBuilder = new StringBuilder(); |
|
|
|
StringBuilder textBuilder = new StringBuilder(); |
|
|
@ -78,13 +79,13 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService { |
|
|
|
|
|
|
|
|
|
|
|
Object resultObject = analyser.getResultValue(analyser.getText(), info.getResultKey(), getSendUrl(request)); |
|
|
|
Object resultObject = analyser.getResultValue(analyser.getText(), info.getResultKey(), getSendUrl(request)); |
|
|
|
Assert.isTrue(Objects.nonNull(resultObject), () -> { |
|
|
|
Assert.isTrue(Objects.nonNull(resultObject), () -> { |
|
|
|
throw new HzServiceException("智谱平台分析失败"); |
|
|
|
throw ErrorCode.ANALYSIS_FAILURE.throwException(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
String resultStr = resultObject.toString(); |
|
|
|
String resultStr = resultObject.toString(); |
|
|
|
|
|
|
|
|
|
|
|
ResultStrategyType strategyType = BaseEnum.getInstance(info.getResultStrategy(), ResultStrategyType.class); |
|
|
|
ResultStrategyType strategyType = BaseEnum.getInstance(info.getResultStrategy(), ResultStrategyType.class); |
|
|
|
Assert.isTrue(Objects.nonNull(strategyType), () -> { |
|
|
|
Assert.isTrue(Objects.nonNull(strategyType), () -> { |
|
|
|
throw new HzServiceException("没有找到对应的结果数据解析策略"); |
|
|
|
throw ErrorCode.STRATEGY_NOT_FOUND.throwException(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
ResultStrategy strategy = strategyType.getStrategy(); |
|
|
|
ResultStrategy strategy = strategyType.getStrategy(); |
|
|
|
|
|
|
|
|
|
|
@ -100,14 +101,14 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService { |
|
|
|
if (StringUtil.isNotBlank(request.getUrl())) { |
|
|
|
if (StringUtil.isNotBlank(request.getUrl())) { |
|
|
|
return request.getUrl(); |
|
|
|
return request.getUrl(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (StringUtil.isNotBlank(request.getFilePath())) { |
|
|
|
Assert.isTrue(StringUtil.isNotBlank(request.getFilePath()), () -> { |
|
|
|
|
|
|
|
throw ErrorCode.GET_FILE_CONTENT_FAILURE.throwException(); |
|
|
|
|
|
|
|
}); |
|
|
|
try { |
|
|
|
try { |
|
|
|
return FileUtil.getBase64(request.getFilePath()); |
|
|
|
return FileUtil.getBase64(request.getFilePath()); |
|
|
|
} catch (IOException e) { |
|
|
|
} catch (IOException e) { |
|
|
|
throw new HzServiceException("获取文件内容失败"); |
|
|
|
throw ErrorCode.GET_FILE_CONTENT_FAILURE.throwException(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
@ -121,7 +122,7 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService { |
|
|
|
.eq(StringUtil.isNotBlank(request.getCheckTypeSon()), ZhipuAnalysisInfoEntity::getCheckTypeSon, request.getCheckTypeSon()) |
|
|
|
.eq(StringUtil.isNotBlank(request.getCheckTypeSon()), ZhipuAnalysisInfoEntity::getCheckTypeSon, request.getCheckTypeSon()) |
|
|
|
.last("limit 1;")); |
|
|
|
.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; |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -131,15 +132,15 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService { |
|
|
|
BigModelAnalysisResponseDTO response = new BigModelAnalysisResponseDTO(); |
|
|
|
BigModelAnalysisResponseDTO response = new BigModelAnalysisResponseDTO(); |
|
|
|
try { |
|
|
|
try { |
|
|
|
Assert.isTrue(StringUtil.isNotBlank(request.getUrl()), () -> { |
|
|
|
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()), () -> { |
|
|
|
Assert.isTrue(CollectionUtil.isNotEmpty(request.getCodeList()) || CollectionUtil.isNotEmpty(request.getCheckTypeSonList()), () -> { |
|
|
|
throw new HzServiceException("识别类型为空"); |
|
|
|
throw ErrorCode.EMPTY_ANALYSIS_TYPE.throwException(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
List<ZhipuAnalysisInfoEntity> infoList = infoService.list(Wrappers.<ZhipuAnalysisInfoEntity>lambdaQuery().in(CollectionUtil.isNotEmpty(request.getCodeList()), ZhipuAnalysisInfoEntity::getCode, request.getCodeList()) |
|
|
|
List<ZhipuAnalysisInfoEntity> infoList = infoService.list(Wrappers.<ZhipuAnalysisInfoEntity>lambdaQuery().in(CollectionUtil.isNotEmpty(request.getCodeList()), ZhipuAnalysisInfoEntity::getCode, request.getCodeList()) |
|
|
|
.in(CollectionUtil.isNotEmpty(request.getCheckTypeSonList()), ZhipuAnalysisInfoEntity::getCheckTypeSon, request.getCheckTypeSonList())); |
|
|
|
.in(CollectionUtil.isNotEmpty(request.getCheckTypeSonList()), ZhipuAnalysisInfoEntity::getCheckTypeSon, request.getCheckTypeSonList())); |
|
|
|
Assert.isTrue(CollectionUtil.isNotEmpty(infoList), () -> { |
|
|
|
Assert.isTrue(CollectionUtil.isNotEmpty(infoList), () -> { |
|
|
|
throw new HzServiceException("未知的识别类型"); |
|
|
|
throw ErrorCode.UNKNOWN_ANALYSIS_TYPE.throwException(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
ZhipuAnalyser analyser = analysisFactory.getAnalysisStrategy(infoList.get(0).getModel()); |
|
|
|
ZhipuAnalyser analyser = analysisFactory.getAnalysisStrategy(infoList.get(0).getModel()); |
|
|
|
|
|
|
|
|
|
|
@ -148,7 +149,7 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService { |
|
|
|
|
|
|
|
|
|
|
|
if (null == resultMap) { |
|
|
|
if (null == resultMap) { |
|
|
|
response.setCode(ResultCode.FAILURE.getCode()); |
|
|
|
response.setCode(ResultCode.FAILURE.getCode()); |
|
|
|
response.setMsg("智谱大模型分析失败"); |
|
|
|
response.setMsg(ErrorCode.ANALYSIS_FAILURE.getMsg()); |
|
|
|
return response; |
|
|
|
return response; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -184,9 +185,9 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService { |
|
|
|
response.setMsg(e.getMessage()); |
|
|
|
response.setMsg(e.getMessage()); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
response.setCode(ResultCode.FAILURE.getCode()); |
|
|
|
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; |
|
|
|
return response; |
|
|
|
} |
|
|
|
} |
|
|
@ -196,18 +197,18 @@ public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService { |
|
|
|
StringBuilder titleBuilder = new StringBuilder(); |
|
|
|
StringBuilder titleBuilder = new StringBuilder(); |
|
|
|
StringBuilder formatBuilder = new StringBuilder(); |
|
|
|
StringBuilder formatBuilder = new StringBuilder(); |
|
|
|
stringBuilder.append(ZhipuAnalyser.QUESTION_PROFILE) |
|
|
|
stringBuilder.append(ZhipuAnalyser.QUESTION_PROFILE) |
|
|
|
.append(StringUtil.format(multiQuestionBrief, infoList.size())); |
|
|
|
.append(StringUtil.format(MULTI_QUESTION_BRIEF, infoList.size())); |
|
|
|
formatBuilder.append("{"); |
|
|
|
formatBuilder.append("{"); |
|
|
|
for (int index = 1; index <= infoList.size(); index++) { |
|
|
|
for (int index = 1; index <= infoList.size(); index++) { |
|
|
|
ZhipuAnalysisInfoEntity infoEntity = infoList.get(index - 1); |
|
|
|
ZhipuAnalysisInfoEntity infoEntity = infoList.get(index - 1); |
|
|
|
stringBuilder.append(StringUtil.format(multiQuestionPrefix, index)); |
|
|
|
stringBuilder.append(StringUtil.format(MULTI_QUESTION_PREFIX, index)); |
|
|
|
titleBuilder.append(StringUtil.format(multiQuestionConstrainsList, infoEntity.getResultKey(), index)); |
|
|
|
titleBuilder.append(StringUtil.format(MULTI_QUESTION_CONSTRAINS_LIST, infoEntity.getResultKey(), index)); |
|
|
|
formatBuilder.append(infoEntity.getRequestOutputFormat()).append(","); |
|
|
|
formatBuilder.append(infoEntity.getRequestOutputFormat()).append(","); |
|
|
|
} |
|
|
|
} |
|
|
|
formatBuilder.deleteCharAt(formatBuilder.length() - 1); |
|
|
|
formatBuilder.deleteCharAt(formatBuilder.length() - 1); |
|
|
|
formatBuilder.append("}"); |
|
|
|
formatBuilder.append("}"); |
|
|
|
stringBuilder.append(ZhipuAnalyser.QUESTION_CONSTRAINS) |
|
|
|
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); |
|
|
|
.append(ZhipuAnalyser.QUESTION_OUTPUT_FORMAT).append(formatBuilder); |
|
|
|
return stringBuilder.toString(); |
|
|
|
return stringBuilder.toString(); |
|
|
|
} |
|
|
|
} |
|
|
|