Selaa lähdekoodia

feat:多线程逐句智评调通,Dify大模型节点topK修改至0.01

baiqi 1 vuosi sitten
vanhempi
commit
617f3e9727

+ 26 - 9
src/main/java/com/njuzr/eaibackend/service/AIEvaluationService.java

@@ -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()

+ 16 - 2
src/main/java/com/njuzr/eaibackend/service/AIRequestService.java

@@ -114,6 +114,20 @@ public class AIRequestService {
         }
     }
 
+    public WorkflowRunResponse requestPreSentenceWorkflowWithRetry(String content, String assignmentDescription, String sentence) {
+        int retry = 0;
+        while (retry < 3) {
+                WorkflowRunResponse response = requestPreSentenceWorkflow(content, assignmentDescription, sentence);
+                if (response == null) {
+                    log.error("requestPreSentenceWorkflowWithRetry failed, retry: {}/{}", retry+1, 3);
+                    retry++;
+                    continue;
+                }
+                return response;
+        }
+        throw new MyException(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()+"AI请求失败");
+    }
+
     public WorkflowRunResponse requestPreSentenceWorkflow(String content, String assignmentDescription, String sentence) {
         MyWorkflowRequestInput input = new MyWorkflowRequestInput(content, assignmentDescription,sentence);
         MyWorkflowRequestObject requestObject = new MyWorkflowRequestObject("abc-1234", "blocking", input);
@@ -122,8 +136,8 @@ public class AIRequestService {
         try {
             return chatglmClient.postWithToken(url, requestObject, WorkflowRunResponse.class, preSentenceWorkflowglm4Token);
         } catch (Exception e) {
-            log.error("WebClient请求失败,AI请求失败~"+e.getMessage());
-            throw new MyException(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()+":"+"服务器请求AI出错");
+            log.error("requestPreSentenceWorkflow failed, reason: {}", e.getMessage());
+            return null;
         }
     }