|
|
@@ -2,7 +2,10 @@ package com.njuzr.eaibackend.service;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
+import com.njuzr.eaibackend.constant.EvaluationPromptConstant;
|
|
|
import com.njuzr.eaibackend.constant.PromptConstant;
|
|
|
+import com.njuzr.eaibackend.dto.deepseek.DeepSeekMessage;
|
|
|
+import com.njuzr.eaibackend.dto.deepseek.DeepSeekResponse;
|
|
|
import com.njuzr.eaibackend.exception.MyException;
|
|
|
import com.njuzr.eaibackend.mapper.*;
|
|
|
import com.njuzr.eaibackend.po.*;
|
|
|
@@ -36,6 +39,7 @@ public class AIEvaluationService {
|
|
|
private static final int RETRY_COUNT = 3;
|
|
|
|
|
|
private final AIRequestService aiRequestService;
|
|
|
+ private final DeepSeekService deepSeekService;
|
|
|
|
|
|
private final StudentAssignmentMapper studentAssignmentMapper;
|
|
|
|
|
|
@@ -62,12 +66,14 @@ public class AIEvaluationService {
|
|
|
private SentenceEvaluationMapperServiceImpl sentenceEvaluationMapperServiceImpl;
|
|
|
|
|
|
@Autowired
|
|
|
- public AIEvaluationService(AIRequestService aiRequestService, StudentAssignmentMapper studentAssignmentMapper,
|
|
|
+ public AIEvaluationService(AIRequestService aiRequestService, DeepSeekService deepSeekService,
|
|
|
+ StudentAssignmentMapper studentAssignmentMapper,
|
|
|
AssignmentMapper assignmentMapper, OverallEvaluationMapper overallEvaluationMapper,
|
|
|
SentenceEvaluationMapper sentenceEvaluationMapper, EvaluationMapper evaluationMapper,
|
|
|
EvaluationMapperServiceImpl evaluationMapperServiceImpl,
|
|
|
PlatformTransactionManager transactionManager) {
|
|
|
this.aiRequestService = aiRequestService;
|
|
|
+ this.deepSeekService = deepSeekService;
|
|
|
this.studentAssignmentMapper = studentAssignmentMapper;
|
|
|
this.assignmentMapper = assignmentMapper;
|
|
|
this.overallEvaluationMapper = overallEvaluationMapper;
|
|
|
@@ -148,14 +154,17 @@ public class AIEvaluationService {
|
|
|
throw MyException.create(HttpStatus.BAD_REQUEST, "作业已进行过AI分析");
|
|
|
}
|
|
|
try {
|
|
|
- String prompt = String.format(PromptConstant.REWRITE_PROMPT_TEMPLATE, assignment.getDescription(), engagement.getTextContent());
|
|
|
+ String prompt = String.format(EvaluationPromptConstant.OVERALL_EVALUATION_PROMPT,
|
|
|
+ assignment.getDescription(), engagement.getTextContent());
|
|
|
AIRequestService.AIResponse response = requestAIWithRetry(prompt, 0);
|
|
|
String retContent = response.getChoices().get(0).getMessage().getContent();
|
|
|
String role = response.getChoices().get(0).getMessage().getRole();
|
|
|
|
|
|
- // 保存评估结果
|
|
|
+ // 保存Markdown格式的评估结果
|
|
|
saveOverallEvaluationResult(assignmentId, studentId, engagement, retContent);
|
|
|
|
|
|
+ log.info("【评分完成】作文长度: {}, 评价长度: {}",
|
|
|
+ engagement.getTextContent().length(), retContent.length());
|
|
|
log.info("evaluation cost: {}", sw.formatTime());
|
|
|
return new AIEntry(role, retContent);
|
|
|
|
|
|
@@ -223,14 +232,32 @@ public class AIEvaluationService {
|
|
|
}
|
|
|
|
|
|
private AIRequestService.AIResponse requestAIWithRetry(String prompt, int retryTime) {
|
|
|
- List<AIEntry> entry = new ArrayList<>();
|
|
|
- entry.add(new AIEntry("user", prompt));
|
|
|
- AIRequestService.AIResponse response = aiRequestService.requestChatGLM4(entry);
|
|
|
- if(response == null && retryTime < 3) {
|
|
|
- log.info("requestAI failed, retryTime: {}", retryTime + 1);
|
|
|
- return requestAIWithRetry(prompt, retryTime + 1);
|
|
|
+ List<DeepSeekMessage> messages = new ArrayList<>();
|
|
|
+ messages.add(DeepSeekMessage.user(prompt));
|
|
|
+ try {
|
|
|
+ DeepSeekResponse response = deepSeekService.chatCompletion(messages);
|
|
|
+ if (response == null && retryTime < 3) {
|
|
|
+ log.info("DeepSeek request failed, retryTime: {}", retryTime + 1);
|
|
|
+ return requestAIWithRetry(prompt, retryTime + 1);
|
|
|
+ }
|
|
|
+ if (response != null && response.getChoices() != null && !response.getChoices().isEmpty()) {
|
|
|
+ AIRequestService.Choice choice = new AIRequestService.Choice();
|
|
|
+ AIRequestService.ResponseMessage msg = new AIRequestService.ResponseMessage();
|
|
|
+ msg.setRole(response.getChoices().get(0).getMessage().getRole());
|
|
|
+ msg.setContent(response.getChoices().get(0).getMessage().getContent());
|
|
|
+ choice.setMessage(msg);
|
|
|
+ AIRequestService.AIResponse aiResponse = new AIRequestService.AIResponse();
|
|
|
+ aiResponse.setChoices(List.of(choice));
|
|
|
+ return aiResponse;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("DeepSeek request error: {}, retryTime: {}/{}", e.getMessage(), retryTime + 1, 3);
|
|
|
+ if (retryTime < 3) {
|
|
|
+ return requestAIWithRetry(prompt, retryTime + 1);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
}
|
|
|
- return response;
|
|
|
}
|
|
|
|
|
|
/**
|