|
|
@@ -1,9 +1,12 @@
|
|
|
package com.njuzr.eaibackend.service;
|
|
|
|
|
|
+import com.njuzr.eaibackend.exception.MyException;
|
|
|
import com.njuzr.eaibackend.po.AIEntry;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.test.context.ActiveProfiles;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
@@ -15,6 +18,7 @@ import java.util.List;
|
|
|
* @Package: EAI-Backend
|
|
|
*/
|
|
|
|
|
|
+@Slf4j
|
|
|
@ActiveProfiles("dev")
|
|
|
@SpringBootTest
|
|
|
public class AIRequestServiceTest {
|
|
|
@@ -35,4 +39,43 @@ public class AIRequestServiceTest {
|
|
|
AIRequestService.AIResponse response= aiRequestService.requestChatGLM4(ms);
|
|
|
System.out.println(response);
|
|
|
}
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void testRewrite() {
|
|
|
+ String content = "hello world";
|
|
|
+ String assignmentDescription = "英文读写第1次作业:软院时光机\n" +
|
|
|
+ "穿梭时光,回到那美好而又艰苦的2002\n";
|
|
|
+ // 3 构造提示词
|
|
|
+ String promptTemplate =
|
|
|
+ "假设你现在是一位英文专业的教师,正在批阅一位同学的写作作业,作业描述是:%s"
|
|
|
+ + "请做如下工作:\n"
|
|
|
+ + "1. 满分100分,请综合英文用词、句式等方面,给出评分和评分原因\n"
|
|
|
+ + "2. 针对文章,给出改进的内容,格式为“xxx”可以修改为“xxx”\n"
|
|
|
+ + "3. 给出修改后的完整文章内容\n"
|
|
|
+ + "其余的任何描述都不需要,也不需要任何交互!\n"
|
|
|
+ + "以下是学生作业内容:\n\n"
|
|
|
+ + "%s";
|
|
|
+
|
|
|
+ String prompt = String.format(promptTemplate, assignmentDescription, content);
|
|
|
+ log.info("内置提示词为:"+ prompt);
|
|
|
+
|
|
|
+ // 4 请求AI
|
|
|
+ List<AIEntry> entry = new ArrayList<>();
|
|
|
+ entry.add(new AIEntry("user", prompt));
|
|
|
+ AIRequestService.AIResponse response = aiRequestService.requestChatGLM4(entry);
|
|
|
+
|
|
|
+ // 5 获取返回数据
|
|
|
+ String retContent;
|
|
|
+ String role;
|
|
|
+ Long timestamp;
|
|
|
+ try {
|
|
|
+ retContent = response.getChoices().get(0).getMessage().getContent();
|
|
|
+ role = response.getChoices().get(0).getMessage().getRole();
|
|
|
+ timestamp = response.getCreated();
|
|
|
+ log.info("AI返回的数据为:"+retContent);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("AI请求返回的数据内容有误,数据为{}", response);
|
|
|
+ throw new MyException(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()+":"+"请求失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|