Jelajahi Sumber

feat:增加单句智评重试功能

baiqi 1 tahun lalu
induk
melakukan
34df26856b

+ 34 - 0
src/main/java/com/njuzr/eaibackend/controller/EvaluationController.java

@@ -21,6 +21,22 @@ public class EvaluationController {
         this.aiEvaluationService = aiEvaluationService;
     }
 
+    /**
+     * 一键双评
+     * @param assignmentId
+     * @param studentId
+     * @return
+     * @throws ExecutionException
+     * @throws InterruptedException
+     */
+    @PostMapping("/all")
+    public MyResponse evaluateAll(
+            @RequestParam Long assignmentId,
+            @RequestParam Long studentId
+    ) throws ExecutionException, InterruptedException {
+        return MyResponse.success(aiEvaluationService.evaluateAll(assignmentId, studentId));
+    }
+
     /**
      * 对作文整体进行评价
      * @param assignmentId 作业ID
@@ -95,5 +111,23 @@ public class EvaluationController {
         return MyResponse.success(aiEvaluationService.getVersionList(assignmentId, studentId));
     }
 
+    /**
+     * 用于支持单句重试
+     * @param assignmentId
+     * @param studentId
+     * @param version
+     * @param sentenceNo
+     * @return
+     */
+    @PostMapping("/sentence/byOne")
+    public MyResponse evaluateSentenceByOne(
+            @RequestParam Long assignmentId,
+            @RequestParam Long studentId,
+            @RequestParam Integer version,
+            @RequestParam Integer sentenceNo
+    ){
+        return MyResponse.success(aiEvaluationService.evaluateSentenceByOne(assignmentId, studentId, version, sentenceNo));
+    }
+
 
 }

+ 19 - 0
src/main/java/com/njuzr/eaibackend/mapper/SentenceEvaluationMapperServiceImpl.java

@@ -1,5 +1,6 @@
 package com.njuzr.eaibackend.mapper;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.njuzr.eaibackend.po.Evaluation;
 import com.njuzr.eaibackend.po.SentenceEvaluation;
@@ -7,4 +8,22 @@ import org.springframework.stereotype.Service;
 
 @Service
 public class SentenceEvaluationMapperServiceImpl extends ServiceImpl<SentenceEvaluationMapper, SentenceEvaluation> {
+
+    public boolean saveOrUpdateByCondition(SentenceEvaluation entity) {
+        // 1. 构建唯一条件
+        LambdaQueryWrapper<SentenceEvaluation> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.eq(SentenceEvaluation::getAssignmentId, entity.getAssignmentId())
+                .eq(SentenceEvaluation::getStudentId, entity.getStudentId())
+                .eq(SentenceEvaluation::getVersion, entity.getVersion())
+                .eq(SentenceEvaluation::getNo, entity.getNo());
+
+        // 2. 尝试更新非空字段
+        int rows = baseMapper.update(entity, queryWrapper);
+        if (rows == 0) {
+            // 3. 无匹配记录则插入
+            baseMapper.insert(entity);
+        }
+        return true;
+    }
+
 }

+ 54 - 2
src/main/java/com/njuzr/eaibackend/service/AIEvaluationService.java

@@ -135,6 +135,16 @@ public class AIEvaluationService {
         this.evaluationMapperServiceImpl = evaluationMapperServiceImpl;
     }
 
+    /**
+     * 一键双评
+     * @param assignmentId
+     * @param studentId
+     * @return
+     */
+    public AIEntry evaluateAll(Long assignmentId, Long studentId) {
+
+    }
+
     public CompletableFuture<AIEntry> evaluationAsync(Long assignmentId, Long studentId) {
         return CompletableFuture.supplyAsync(() -> evaluation(assignmentId, studentId), aiTaskExecutor);
     }
@@ -254,8 +264,7 @@ public class AIEvaluationService {
         }
 
         String content = engagement.getTextContent();
-        // TODO: 句子分割需要详细考虑和判断
-        String[] sentences = content.split("\\.");
+        String[] sentences = getSentenceList(content);
         List<SentenceEvaluation> sentenceEvaluationsList = new ArrayList<>();
         CompletableFuture<?>[] futures = new CompletableFuture[sentences.length];
 
@@ -298,6 +307,11 @@ public class AIEvaluationService {
         return sentenceEvaluationsList;
     }
 
+    private static String[] getSentenceList(String content) {
+        String[] sentences = content.split("\\.");
+        return sentences;
+    }
+
     private SentenceEvaluation processSingleSentenceWithRetry(Long assignmentId, Long studentId, String content, String description, String sentence, int sentenceNo, int version) {
         int retryTime = 0;
         while (retryTime < RETRY_COUNT) {
@@ -311,6 +325,18 @@ public class AIEvaluationService {
         return new SentenceEvaluation().setVersion(version).setNo(sentenceNo + 1).setType("error").setCategory("error").setEvaluation("error").setStudentId(studentId).setAssignmentId(assignmentId);
     }
 
+    /**
+     * 向Dify发起请求智评单个句子
+     * @param assignmentId
+     * @param studentId
+     * @param content
+     * @param assignmentDesc
+     * @param sentence
+     * @param sentenceNo
+     * @param version
+     * @param retryCount
+     * @return
+     */
     private SentenceEvaluation processSingleSentence(
             Long assignmentId, Long studentId,
             String content, String assignmentDesc,
@@ -389,4 +415,30 @@ public class AIEvaluationService {
         return evaluationMapper.selectList(queryWrapper);
     }
 
+    /**
+     * 对某一个句子进行智评
+     * @param assignmentId
+     * @param studentId
+     * @param version
+     * @return
+     */
+    public SentenceEvaluation evaluateSentenceByOne(Long assignmentId, Long studentId, Integer version, Integer sentenceNo) {
+        Engagement engagement = studentAssignmentMapper.findEngagementByStudentIdAndAssignmentId(studentId, assignmentId);
+        if(engagement == null) throw MyException.create(HttpStatus.BAD_REQUEST, "作业参与不存在");
+        Assignment assignment = assignmentMapper.selectById(assignmentId);
+        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, "没有该版本的智评记录");
+
+        String content = engagement.getTextContent();
+        String[] sentences = getSentenceList(content);
+        SentenceEvaluation result = processSingleSentenceWithRetry(assignmentId, studentId, content, assignment.getDescription(), sentences[sentenceNo], sentenceNo, engagement.getVersion());
+        if(result == null || result.getType().equals("error")) {
+            throw MyException.create(HttpStatus.BAD_REQUEST, "智评失败,请重试");
+        }
+
+        sentenceEvaluationMapperServiceImpl.saveOrUpdateByCondition(result);
+        return result;
+    }
+
 }