Prechádzať zdrojové kódy

feat:接入dify编排的逐句批改Agent

白白 1 rok pred
rodič
commit
c9627dc903

+ 1 - 0
src/main/java/com/njuzr/eaibackend/po/SentenceEvaluation.java

@@ -22,4 +22,5 @@ public class SentenceEvaluation {
     private String evaluation;
     private Integer version;
     private String type;
+    private String category;
 }

+ 14 - 11
src/main/java/com/njuzr/eaibackend/service/AIEvaluationService.java

@@ -248,8 +248,8 @@ public class AIEvaluationService {
         String[] res = content.split("\\.");
 
         // 4 初始化AI请求变量
-        List<AIEntry> entry = new ArrayList<>();
-        AIRequestService.AIResponse response;
+//        List<AIEntry> entry = new ArrayList<>();
+        AIRequestService.WorkflowRunResponse response;
 
         // 5 初始化获取返回数据
         String retContent = "";
@@ -259,19 +259,22 @@ public class AIEvaluationService {
         for(int i = 0; i < res.length; i++) {
             SentenceEvaluation sentenceEvaluation = new SentenceEvaluation();
 
-            // 1 构造提示词
-            String prompt2 = String.format(SENTENCE_PROMPT_TEMPLATE, assignmentDescription, content, res[i]);
-            log.info("内置提示词为:"+ prompt2);
 
-            // 2 请求AI
-            entry.clear();
-            entry.add(new AIEntry("user", prompt2));
-            response = aiRequestService.requestChatGLM4(entry);
+            // 1 构造提示词
+//            String prompt2 = String.format(SENTENCE_PROMPT_TEMPLATE, assignmentDescription, content, res[i]);
+//            log.info("内置提示词为:"+ prompt2);
+//
+//            // 2 请求AI
+//            entry.clear();
+//            entry.add(new AIEntry("user", prompt2));
+            response = aiRequestService.requestPreSentenceWorkflow(content, assignmentDescription, res[i]);
 
             // 3 获取返回数据
             try {
-                sentenceEvaluation.setEvaluation(response.getChoices().get(0).getMessage().getContent());
-                log.info("AI返回的数据为:"+retContent);
+                sentenceEvaluation.setEvaluation(response.getData().getOutputs().getResult_content());
+                sentenceEvaluation.setType(response.getData().getOutputs().getResult_type());
+                sentenceEvaluation.setCategory(response.getData().getOutputs().getResult_category());
+                log.info("AI返回的数据为:"+ response);
             } catch (Exception e) {
                 log.error("AI请求返回的数据内容有误,数据为{}", response);
                 throw new MyException(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()+":"+"请求失败");

+ 67 - 0
src/main/java/com/njuzr/eaibackend/service/AIRequestService.java

@@ -1,5 +1,6 @@
 package com.njuzr.eaibackend.service;
 
+import com.fasterxml.jackson.annotation.JsonProperty;
 import com.njuzr.eaibackend.exception.MyException;
 import com.njuzr.eaibackend.po.AIEntry;
 import com.njuzr.eaibackend.utils.WebClientUtil;
@@ -50,6 +51,12 @@ public class AIRequestService {
     @Value("${config.llm.QWen14B.url}")
     private String qWen14BUrl;
 
+    @Value("${config.llm.PreSentenceWorkflowChatGLM4.url}")
+    private String preSentenceWorkflowglm4Url;
+
+    @Value("${config.llm.PreSentenceWorkflowChatGLM4.token}")
+    private String preSentenceWorkflowglm4Token;
+
     public AIRequestService(WebClientUtil chatgptClient, WebClientUtil chatglmClient, WebClientUtil qwenClient) {
         this.chatgptClient = chatgptClient;
         this.chatglmClient = chatglmClient;
@@ -107,6 +114,66 @@ public class AIRequestService {
         }
     }
 
+    public WorkflowRunResponse requestPreSentenceWorkflow(String content, String assignmentDescription, String sentence) {
+        MyWorkflowRequestInput input = new MyWorkflowRequestInput(content, assignmentDescription,sentence);
+        MyWorkflowRequestObject requestObject = new MyWorkflowRequestObject("abc-1234", "blocking", input);
+        String url = preSentenceWorkflowglm4Url;
+
+        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出错");
+        }
+    }
+
+    @Data
+    @AllArgsConstructor
+    static class MyWorkflowRequestObject {
+        private String user;
+        private String response_mode;
+        private MyWorkflowRequestInput inputs;
+
+    }
+
+    @Data
+    @AllArgsConstructor
+    static class MyWorkflowRequestInput {
+        private String content;
+        private String assignment_description;
+        private String sentence;
+    }
+
+    @Data
+    static class WorkflowRunResponse {
+        private String task_id;
+        private String workflow_run_id;
+        private WorkflowRunData data;
+    }
+
+    @Data
+    static
+    class WorkflowRunData {
+        private String id;
+        private String workflow_id;
+        private String status;
+        private Outputs outputs;
+        private Object error;
+        private double elapsed_time;
+        private int total_tokens;
+        private int total_steps;
+        private long created_at;
+        private long finished_at;
+    }
+
+    @Data
+    static
+    class Outputs {
+        private String result_type;
+        private String result_category;
+        private String result_content;
+    }
+
     @Data
     @AllArgsConstructor
     static class MyRequestObject {

+ 13 - 0
src/test/java/com/njuzr/eaibackend/service/AIRequestServiceTest.java

@@ -124,4 +124,17 @@ public class AIRequestServiceTest {
             throw new MyException(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase() + ":" + "请求失败");
         }
     }
+
+    @Test
+    public void testWorkflow(){
+        String str1 = "The Impact of Technology on Modern Education: A Blessing or a Curse?\n" +
+                "In modern education, technology has undeniably revolutionized the way teaching and learning take place. Smartboards and online courses enable teachers to present knowledge more vividly, while students can access a vast amount of information with just a click.\n" +
+                "However, it also has drawbacks. For students, excessive reliance on technology may reduce their ability to think independently. Teachers might face challenges in adapting to new teaching tools. As for educational institutions, the high cost of technology implementation is a concern.\n" +
+                "According to a recent study, a balanced use of technology can enhance learning efficiency. Thus, I believe we should make good use of technology while not neglecting traditional teaching methods. Schools should provide teachers with training, and students should be guided to use technology properly.";
+        String str2 = "作文1:《现代教育中的技术影响:福音还是祸根?》\n" +
+                "在这个作业中,你需要探讨技术在现代教育中扮演的角色,并分析它对教育的影响。你需要讨论技术如何改变了教学和学习的方式,以及这些变化对学生、教师和教育机构的利弊。你的作文应该包括对现有研究的引用,并提出你自己的观点和建议。";
+        String str3 = "Schools should provide teachers with training, and students should be guided to use technology properly.";
+        AIRequestService.WorkflowRunResponse response = aiRequestService.requestPreSentenceWorkflow(str1, str2, str3);
+        System.out.println(response.toString());
+    }
 }