lllgodenough 1 год назад
Родитель
Сommit
1fed87c56a

+ 0 - 14
checkstyle.xml

@@ -1,14 +0,0 @@
-<?xml version="1.0"?>
-<!DOCTYPE module PUBLIC
-        "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
-        "https://checkstyle.org/dtds/configuration_1_3.dtd">
-<module name="Checker">
-    <module name="TreeWalker">
-        <module name="AvoidStarImport"/>
-        <module name="IllegalImport"/>
-        <module name="RedundantImport"/>
-        <module name="UnusedImports"/>
-        <module name="MethodLength"/>
-        <module name="ParameterNumber"/>
-    </module>
-</module>

+ 0 - 26
hooks/pre-commit

@@ -1,26 +0,0 @@
-#!/bin/bash
-
-echo "Running pre-commit checks..."
-
-# 获取项目根目录
-PROJECT_DIR=$(git rev-parse --show-toplevel)
-
-# 运行编译和代码检查
-cd "$PROJECT_DIR" && mvn compile
-
-# 检查编译是否成功
-if [ $? -ne 0 ]; then
-    echo "Compilation failed! Please fix errors before committing."
-    exit 1
-fi
-
-# 运行静态代码分析(例如Checkstyle)
-cd "$PROJECT_DIR" && mvn checkstyle:check
-
-if [ $? -ne 0 ]; then
-    echo "Code style check failed! Please fix errors before committing."
-    exit 1
-fi
-
-echo "All checks passed! Proceeding with commit."
-exit 0

+ 48 - 0
src/main/java/com/njuzr/eaibackend/config/DeepSeekConfig.java

@@ -0,0 +1,48 @@
+package com.njuzr.eaibackend.config;
+
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * @author Liululin
+ * @date 2025/9/11 - 20:53
+ */
+@Configuration
+public class DeepSeekConfig {
+
+    @Value("${deepseek.api.key}")
+    private String apiKey;
+
+    @Value("${deepseek.api.base-url}")
+    private String baseUrl;
+
+    @Value("${deepseek.api.model")
+    private String model;
+
+    public Double getTemperature() {
+        return temperature;
+    }
+
+    @Value("${deepseek.api.temperature}")
+    private Double temperature;
+
+    public String getModel() {
+        return model;
+    }
+
+    public String getApiKey() {
+        return apiKey;
+    }
+
+    public String getBaseUrl() {
+        return baseUrl;
+    }
+
+    @Bean
+    public CloseableHttpClient httpClient() {
+        return HttpClients.createDefault();
+    }
+}

+ 16 - 8
src/main/java/com/njuzr/eaibackend/service/AIEvaluationService.java

@@ -16,6 +16,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.io.IOException;
 import java.util.*;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.TimeUnit;
@@ -31,7 +32,11 @@ public class AIEvaluationService {
 
     private static final int RETRY_COUNT = 3;
 
-    private final AIRequestService aiRequestService;
+    @Autowired
+    DeepSeekService deepSeekService;
+
+    @Autowired
+    AIRequestService aiRequestService;
 
     private final StudentAssignmentMapper studentAssignmentMapper;
 
@@ -56,8 +61,7 @@ public class AIEvaluationService {
     private SentenceEvaluationMapperServiceImpl sentenceEvaluationMapperServiceImpl;
 
     @Autowired
-    public AIEvaluationService(AIRequestService aiRequestService, StudentAssignmentMapper studentAssignmentMapper, AssignmentMapper assignmentMapper, OverallEvaluationMapper overallEvaluationMapper, SentenceEvaluationMapper sentenceEvaluationMapper, EvaluationMapper evaluationMapper, EvaluationMapperServiceImpl evaluationMapperServiceImpl) {
-        this.aiRequestService = aiRequestService;
+    public AIEvaluationService(StudentAssignmentMapper studentAssignmentMapper, AssignmentMapper assignmentMapper, OverallEvaluationMapper overallEvaluationMapper, SentenceEvaluationMapper sentenceEvaluationMapper, EvaluationMapper evaluationMapper, EvaluationMapperServiceImpl evaluationMapperServiceImpl) {
         this.studentAssignmentMapper = studentAssignmentMapper;
         this.assignmentMapper = assignmentMapper;
         this.overallEvaluationMapper = overallEvaluationMapper;
@@ -92,9 +96,8 @@ public class AIEvaluationService {
 
             // 发起LLM请求
             String prompt = String.format(PromptConstant.REWRITE_PROMPT_TEMPLATE, 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();
+            String retContent = requestAIWithRetry(prompt, 0);
+            String role = "assistant";
 
             OverallEvaluation overallEvaluation = new OverallEvaluation()
                 .setStudentId(studentId)
@@ -132,10 +135,15 @@ public class AIEvaluationService {
         log.info("evaluation record: {}", evaluation);
     }
 
-    private AIRequestService.AIResponse requestAIWithRetry(String prompt, int retryTime) {
+    private String requestAIWithRetry(String prompt, int retryTime) {
         List<AIEntry> entry = new ArrayList<>();
         entry.add(new AIEntry("user", prompt));
-        AIRequestService.AIResponse response = aiRequestService.requestChatGLM4(entry);
+        String response = null;
+        try {
+            response = deepSeekService.chatCompletion(entry);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
         if(response == null && retryTime < 3) {
             log.info("requestAI failed, retryTime: {}", retryTime + 1);
             return requestAIWithRetry(prompt, retryTime + 1);

+ 72 - 0
src/main/java/com/njuzr/eaibackend/service/DeepSeekService.java

@@ -0,0 +1,72 @@
+package com.njuzr.eaibackend.service;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.njuzr.eaibackend.config.DeepSeekConfig;
+import com.njuzr.eaibackend.po.AIEntry;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.util.EntityUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author Liululin
+ * @date 2025/9/11 - 20:56
+ */
+@Service
+@Slf4j
+public class DeepSeekService {
+
+    private final CloseableHttpClient httpClient;
+    private final ObjectMapper objectMapper;
+
+    public DeepSeekService(
+                           CloseableHttpClient httpClient,
+                           ObjectMapper objectMapper) {
+        this.httpClient = httpClient;
+        this.objectMapper = objectMapper;
+    }
+
+    @Autowired
+    DeepSeekConfig deepSeekConfig;
+
+    public String chatCompletion(List<AIEntry> message) throws IOException {
+        HttpPost httpPost = new HttpPost(deepSeekConfig.getBaseUrl());
+
+        // 设置请求头
+        httpPost.setHeader("Content-Type", "application/json");
+        httpPost.setHeader("Authorization", "Bearer " + deepSeekConfig.getApiKey());
+
+        // 构建请求体
+        Map<String, Object> requestBody = new HashMap<>();
+        requestBody.put("model", deepSeekConfig.getModel());
+        requestBody.put("messages", message);
+        requestBody.put("temperature", deepSeekConfig.getTemperature());
+
+        StringEntity entity = new StringEntity(objectMapper.writeValueAsString(requestBody));
+        httpPost.setEntity(entity);
+
+        // 执行请求
+        try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
+            String responseBody = EntityUtils.toString(response.getEntity());
+            Map<String, Object> responseMap = objectMapper.readValue(responseBody, Map.class);
+
+            // 解析响应
+            List<Map<String, Object>> choices = (List<Map<String, Object>>) responseMap.get("choices");
+            if (choices != null && !choices.isEmpty()) {
+                Map<String, Object> res = (Map<String, Object>) choices.get(0).get("message");
+                return (String) res.get("content");
+            }
+            return "未获取到有效响应";
+        }
+    }
+}
+

+ 9 - 6
src/main/java/com/njuzr/eaibackend/service/impl/DoubaoServiceImpl.java

@@ -5,7 +5,7 @@ import com.njuzr.eaibackend.exception.MyException;
 import com.njuzr.eaibackend.mapper.SpeakingAIDialogueMapper;
 import com.njuzr.eaibackend.po.AIEntry;
 import com.njuzr.eaibackend.po.SpeakingAIDialogue;
-import com.njuzr.eaibackend.service.AIRequestService;
+import com.njuzr.eaibackend.service.DeepSeekService;
 import com.njuzr.eaibackend.service.DoubaoService;
 import com.njuzr.eaibackend.utils.OssUtil;
 import com.njuzr.eaibackend.vo.AIReplyVo;
@@ -50,7 +50,7 @@ public class DoubaoServiceImpl implements DoubaoService {
     AssignmentServiceImpl assignmentService;
 
     @Autowired
-    AIRequestService aiRequestService;
+    DeepSeekService deepSeekService;
 
     @Autowired
     OssUtil ossUtil;
@@ -260,8 +260,12 @@ public class DoubaoServiceImpl implements DoubaoService {
         List<SpeakingAIDialogue.DialogueEntry> dialogues = targetDialogue.getDialogues();
         // 构造用户结束对话消息
         addMessageToDialogue(dialogues, "user", "结束对话", null, null, -1);
-        AIRequestService.AIResponse aiResponse = aiRequestService.requestChatGLM4(dialoguesToAIEntry(dialogues));
-        String response = aiResponse.getChoices().get(0).getMessage().getContent();
+        String response = null;
+        try {
+            response = deepSeekService.chatCompletion(dialoguesToAIEntry(dialogues));
+        } catch (IOException e) {
+            throw new MyException(400, "deepseek出现异常,请稍后再试");
+        }
         addMessageToDialogue(dialogues, "assistant", response, null, null, -1);
         targetDialogue.setDialogues(dialogues);
         speakingAIDialogueMapper.save(targetDialogue);
@@ -274,8 +278,7 @@ public class DoubaoServiceImpl implements DoubaoService {
     private CompletableFuture<String> getArkServiceResponse(List<SpeakingAIDialogue.DialogueEntry> dialogues) {
         return CompletableFuture.supplyAsync(() -> {
             try {
-                AIRequestService.AIResponse response = aiRequestService.requestChatGLM4(dialoguesToAIEntry(dialogues));
-                String content = response.getChoices().get(0).getMessage().getContent();
+                String content = deepSeekService.chatCompletion(dialoguesToAIEntry(dialogues));
                 return content;
             } catch (Exception e) {
                 throw new MyException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "ArkService调用失败:" + e.getMessage());

+ 8 - 16
src/main/java/com/njuzr/eaibackend/service/impl/MediaAnalysisServiceImpl.java

@@ -9,7 +9,9 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import com.njuzr.eaibackend.config.DoubaoConfig;
 import com.njuzr.eaibackend.config.IseClientFactory;
 import com.njuzr.eaibackend.exception.MyException;
+import com.njuzr.eaibackend.po.AIEntry;
 import com.njuzr.eaibackend.po.SpeakingAIDialogue;
+import com.njuzr.eaibackend.service.DeepSeekService;
 import com.njuzr.eaibackend.service.MediaAnalysisService;
 import com.njuzr.eaibackend.utils.XmlParasUtil;
 import com.njuzr.eaibackend.vo.VideoAnalysisVo;
@@ -72,6 +74,9 @@ public class MediaAnalysisServiceImpl implements MediaAnalysisService {
     @Autowired
     IseClientFactory iseClientFactory;
 
+    @Autowired
+    DeepSeekService deepSeekService;
+
 
     @Autowired
     @Qualifier("doubaoTaskExecutor") // 指定 Bean 名称
@@ -284,22 +289,9 @@ public class MediaAnalysisServiceImpl implements MediaAnalysisService {
                 String message = buildSuggestionMessage(existingDialogue);
                 log.debug("{} 构建的建议请求消息: {}", SPEAKING_SUGGESTION_HEADER, message);
 
-                ChatMessage chatMessage = ChatMessage.builder()
-                        .role(ChatMessageRole.USER)
-                        .content(message)
-                        .build();
-
-                List<ChatMessage> list = Collections.singletonList(chatMessage);
-
-                ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
-                        .model(doubaoConfig.getEndpointId())
-                        .messages(list)
-                        .build();
-
-                String response = (String) doubaoConfig.arkService()
-                        .createChatCompletion(chatCompletionRequest)
-                        .getChoices().get(0).getMessage().getContent();
-
+                List<AIEntry>   list = new ArrayList<>();
+                list.add(new AIEntry("user",message));
+                String response = deepSeekService.chatCompletion(list);
                 log.info("{} 获取到口语建议响应: {}", SPEAKING_SUGGESTION_HEADER, response);
                 existingDialogue.setSuggestion(response);
                 mongoTemplate.save(existingDialogue);

+ 6 - 1
src/test/java/com/njuzr/eaibackend/EaiBackendApplicationTests.java

@@ -1,19 +1,24 @@
 package com.njuzr.eaibackend;
 
+import com.njuzr.eaibackend.service.DeepSeekService;
 import com.njuzr.eaibackend.service.impl.CourseServiceImpl;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 
+import java.io.IOException;
+
 @SpringBootTest
 class EaiBackendApplicationTests {
 
     @Autowired
     private CourseServiceImpl courseServiceImpl;
 
+
+
     @Test
     void contextLoads() {
-        System.out.println(courseServiceImpl.getEnrollCodeByCourseId(111112L));
+
     }
 
 }