|
|
@@ -27,6 +27,8 @@ import java.util.concurrent.TimeoutException;
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
public class AIEvaluationService {
|
|
|
+
|
|
|
+ private static final int RETRY_COUNT = 3;
|
|
|
// 整体评价
|
|
|
// 两个输入 1.作文描述 2.作文内容
|
|
|
private static final String REWRITE_PROMPT_TEMPLATE = "假设你现在是一位英文专业的教师,正在批阅一位同学的写作作业,作业描述是:%s"
|
|
|
@@ -151,9 +153,9 @@ public class AIEvaluationService {
|
|
|
if (assignment == null) throw MyException.create(HttpStatus.BAD_REQUEST, "作业不存在");
|
|
|
Evaluation evaluationJudge = evaluationMapperServiceImpl.getByVersion(assignmentId, studentId, engagement.getVersion());
|
|
|
if(evaluationJudge != null) throw MyException.create(HttpStatus.BAD_REQUEST, "作业已进行过AI分析");
|
|
|
-
|
|
|
+ log.info("evaluation start");
|
|
|
initEvaluationRecord(assignmentId, studentId, engagement);
|
|
|
-
|
|
|
+ log.info("evaluation end");
|
|
|
String prompt = String.format(REWRITE_PROMPT_TEMPLATE, assignment.getDescription(), engagement.getTextContent());
|
|
|
AIRequestService.AIResponse response = requestAI(prompt, 0);
|
|
|
String retContent;
|
|
|
@@ -195,7 +197,8 @@ public class AIEvaluationService {
|
|
|
.setSentence(0)
|
|
|
.setOverall(0);
|
|
|
evaluationMapperServiceImpl.save(evaluation);
|
|
|
-
|
|
|
+ log.info("evaluation record init");
|
|
|
+ log.info("evaluation record: {}", evaluation);
|
|
|
}
|
|
|
|
|
|
private AIRequestService.AIResponse requestAI(String prompt, int retryTime) {
|
|
|
@@ -228,10 +231,11 @@ public class AIEvaluationService {
|
|
|
int retryTime = 0;
|
|
|
while (retryTime < 3) {
|
|
|
Evaluation evaluationJudge = evaluationMapperServiceImpl.getByVersion(assignmentId, studentId, engagement.getVersion());
|
|
|
+ log.info("rewritePerSentence find evaluation record, assignmentId: {}, studentId: {}, version: {}", assignmentId, studentId, engagement.getVersion());
|
|
|
if (evaluationJudge == null){
|
|
|
try {
|
|
|
log.info("rewritePerSentence find evaluation record failed, assignmentId: {}, studentId: {}, retryTime: {}", assignmentId, studentId, retryTime + 1);
|
|
|
- Thread.sleep(300);
|
|
|
+ Thread.sleep(1500);
|
|
|
retryTime++;
|
|
|
} catch (InterruptedException e) {
|
|
|
throw new RuntimeException(e);
|
|
|
@@ -259,14 +263,14 @@ public class AIEvaluationService {
|
|
|
final int sentenceNo = i;
|
|
|
final String sentence = sentences[i] + ".";
|
|
|
futures[i] = CompletableFuture.supplyAsync(() ->
|
|
|
- processSingleSentence(assignmentId, studentId, content, assignment.getDescription(), sentence, sentenceNo, engagement.getVersion()),
|
|
|
+ processSingleSentenceWithRetry(assignmentId, studentId, content, assignment.getDescription(), sentence, sentenceNo, engagement.getVersion()),
|
|
|
aiPerSentenceExecutor)
|
|
|
.thenAccept(sentenceEvaluationsList::add);
|
|
|
}
|
|
|
|
|
|
// 等待所有任务完成
|
|
|
try {
|
|
|
- CompletableFuture.allOf(futures).get(180, TimeUnit.SECONDS);
|
|
|
+ CompletableFuture.allOf(futures).get(60, TimeUnit.SECONDS);
|
|
|
log.info("rewritePerSentence allOf success, assignmentId: {}, studentId: {}, version: {}", assignmentId, studentId, engagement.getVersion());
|
|
|
} catch (TimeoutException e) {
|
|
|
log.warn("rewritePerSentence failed, assignmentId: {}, studentId: {}, reason: timeout", assignmentId, studentId);
|
|
|
@@ -294,16 +298,29 @@ public class AIEvaluationService {
|
|
|
return sentenceEvaluationsList;
|
|
|
}
|
|
|
|
|
|
+ private SentenceEvaluation processSingleSentenceWithRetry(Long assignmentId, Long studentId, String content, String description, String sentence, int sentenceNo, int version) {
|
|
|
+ int retryTime = 0;
|
|
|
+ while (retryTime < RETRY_COUNT) {
|
|
|
+ SentenceEvaluation sentenceEvaluation = processSingleSentence(assignmentId, studentId, content, description, sentence, sentenceNo, version, retryTime);
|
|
|
+ if(sentenceEvaluation == null) {
|
|
|
+ retryTime++;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ return sentenceEvaluation;
|
|
|
+ }
|
|
|
+ return new SentenceEvaluation().setVersion(version).setNo(sentenceNo + 1).setType("error").setCategory("error").setEvaluation("error").setStudentId(studentId).setAssignmentId(assignmentId);
|
|
|
+ }
|
|
|
+
|
|
|
private SentenceEvaluation processSingleSentence(
|
|
|
Long assignmentId, Long studentId,
|
|
|
String content, String assignmentDesc,
|
|
|
String sentence, int sentenceNo,
|
|
|
- Integer version
|
|
|
+ Integer version, int retryCount
|
|
|
) {
|
|
|
AIRequestService.WorkflowRunResponse response = aiRequestService.requestPreSentenceWorkflow(content, assignmentDesc, sentence);
|
|
|
if(response == null || response.getData() == null){
|
|
|
- log.error("processSingleSentence failed, assignmentId: {}, studentId: {}, sentence: {}, response: {}", assignmentId, studentId, sentence, response);
|
|
|
- return new SentenceEvaluation().setVersion(version).setNo(sentenceNo + 1).setType("error").setCategory("error").setEvaluation("error");
|
|
|
+ log.error("processSingleSentence failed, assignmentId: {}, studentId: {}, sentence: {}, response: {}, retryCount: {}/{}", assignmentId, studentId, sentence, response, retryCount+1, RETRY_COUNT);
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
return new SentenceEvaluation()
|