|
@@ -1,20 +1,17 @@
|
|
|
package com.qmth.boot.core.ai.service;
|
|
|
|
|
|
import com.qmth.boot.core.ai.client.LlmApiClient;
|
|
|
-import com.qmth.boot.core.ai.client.OcrApiClient;
|
|
|
import com.qmth.boot.core.ai.model.llm.*;
|
|
|
-import com.qmth.boot.core.ai.model.ocr.OcrType;
|
|
|
import com.qmth.boot.core.retrofit.utils.SignatureInfo;
|
|
|
-import com.qmth.boot.core.retrofit.utils.UploadFile;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.validation.constraints.NotNull;
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.math.RoundingMode;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
-import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
@@ -29,9 +26,6 @@ public class AiService {
|
|
|
@Resource
|
|
|
private LlmApiClient llmApiClient;
|
|
|
|
|
|
- @Resource
|
|
|
- private OcrApiClient ocrApiClient;
|
|
|
-
|
|
|
/**
|
|
|
* 获取当前机构大模型接口调用次数余额
|
|
|
*
|
|
@@ -76,31 +70,19 @@ public class AiService {
|
|
|
* @param signature 使用当前机构AK作为鉴权信息
|
|
|
* @return 得分率,保留最多三位小数;null表示无法获取判分结果
|
|
|
*/
|
|
|
- public Double autoScore(@NotNull @Validated AutoScoreRequest request, @NotNull SignatureInfo signature) {
|
|
|
+ public AutoScoreResult autoScore(@NotNull @Validated AutoScoreRequest request, @NotNull SignatureInfo signature) {
|
|
|
ChatResult result = llmApiClient.chatTemplate(signature, LlmAppType.AUTO_SCORE, request);
|
|
|
String text = result.getChoices().stream().filter(choice -> choice.getMessage().getRole() == ChatRole.assistant)
|
|
|
.map(choice -> choice.getMessage().getContent()).findFirst().orElse("");
|
|
|
- Matcher m = score_pattern.matcher(text);
|
|
|
- if (m.find()) {
|
|
|
- return new BigDecimal(Integer.parseInt(m.group(1))).divide(BigDecimal.valueOf(100), 3, RoundingMode.HALF_UP)
|
|
|
- .doubleValue();
|
|
|
- } else {
|
|
|
+ try {
|
|
|
+ AutoScoreResult scoreResult = new AutoScoreResult();
|
|
|
+ String[] scores = StringUtils.split(text, ",");
|
|
|
+ scoreResult.setStepScore(Arrays.stream(scores).mapToDouble(Double::parseDouble).toArray());
|
|
|
+ scoreResult.setTotalScore(Arrays.stream(scoreResult.getStepScore()).mapToObj(BigDecimal::new)
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add).doubleValue());
|
|
|
+ return scoreResult;
|
|
|
+ } catch (Exception e) {
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 使用预设模版进行自动判分,考生作答为图片
|
|
|
- *
|
|
|
- * @param request 自动判分请求参数
|
|
|
- * @param image 图片内容
|
|
|
- * @param signature 使用当前机构AK作为鉴权信息
|
|
|
- * @return 得分率,保留最多三位小数;null表示无法获取判分结果
|
|
|
- */
|
|
|
- public Double autoScore(@NotNull @Validated AutoScoreRequest request, @NotNull byte[] image,
|
|
|
- @NotNull SignatureInfo signature) {
|
|
|
- request.setStudentAnswer(
|
|
|
- ocrApiClient.forImage(signature, OcrType.HANDWRITING, UploadFile.build("image", "", image)));
|
|
|
- return autoScore(request, signature);
|
|
|
- }
|
|
|
}
|