AIRequestServiceTest.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package com.njuzr.eaibackend.service;
  2. import com.njuzr.eaibackend.exception.MyException;
  3. import com.njuzr.eaibackend.po.AIEntry;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.junit.jupiter.api.Test;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.http.HttpStatus;
  9. import org.springframework.test.context.ActiveProfiles;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. /**
  13. * @author: Leonezhurui
  14. * @Date: 2024/3/2 - 00:30
  15. * @Package: EAI-Backend
  16. */
  17. @Slf4j
  18. @ActiveProfiles("dev")
  19. @SpringBootTest
  20. public class AIRequestServiceTest {
  21. private final AIRequestService aiRequestService;
  22. @Autowired
  23. public AIRequestServiceTest(AIRequestService aiRequestService) {
  24. this.aiRequestService = aiRequestService;
  25. }
  26. @Test
  27. public void testChatGLM() {
  28. List<AIEntry> ms = new ArrayList<>();
  29. AIEntry a1 = new AIEntry();
  30. a1.setRole("user");
  31. a1.setContent("123");
  32. ms.add(a1);
  33. AIRequestService.AIResponse response = aiRequestService.requestChatGLM4(ms);
  34. System.out.println(response);
  35. }
  36. @Test
  37. public void testTranslate() {
  38. List<AIEntry> ms = new ArrayList<>();
  39. AIEntry a1 = new AIEntry();
  40. a1.setRole("user");
  41. String content = "任务说明:\n" +
  42. "\n" +
  43. "如果用户提供中文句子,请将该句子翻译为英文。\n" +
  44. "如果用户提供英文句子,请将该句子翻译为中文。\n" +
  45. "如果用户提供中文或英文单词,请提供该单词的所有可能翻译,并给出相关的例句。\n" +
  46. "示例:\n" +
  47. "\n" +
  48. "中文句子翻译:\n" +
  49. "用户:我今天去学校了。\n" +
  50. "模型:I went to school today.\n" +
  51. "\n" +
  52. "英文句子翻译:\n" +
  53. "用户:She is studying at the library.\n" +
  54. "模型:她正在图书馆学习。\n" +
  55. "\n" +
  56. "中文单词翻译与例句:\n" +
  57. "用户:书\n" +
  58. "模型:\n" +
  59. "\n" +
  60. "翻译: book, paper (某些上下文中可指书面材料)\n" +
  61. "例句:\n" +
  62. "I bought a new book yesterday. (我昨天买了一本新书。)\n" +
  63. "He wrote a letter on a piece of paper. (他在一张纸上写了封信。)\n" +
  64. "英文单词翻译与例句:\n" +
  65. "用户:apple\n" +
  66. "模型:\n" +
  67. "\n" +
  68. "翻译: 苹果 (名词), 苹果公司 (名词)\n" +
  69. "例句:\n" +
  70. "I ate an apple this morning. (我今天早上吃了一个苹果。)\n" +
  71. "Apple is a well-known technology company. (苹果公司是一个知名的科技公司。)\n" +
  72. "请确保根据用户输入的语言(中文或英文)进行适当的翻译或解释。\n" +
  73. "必须直接给出翻译结果,不要交互,不要任何其他信息。\n" +
  74. "以下是用户输入:\n\n";
  75. content += "回忆";
  76. a1.setContent(content);
  77. ms.add(a1);
  78. AIRequestService.AIResponse response = aiRequestService.requestChatGLM4(ms);
  79. System.out.println(response);
  80. }
  81. @Test
  82. void testRewrite() {
  83. String content = "hello world";
  84. String assignmentDescription = "英文读写第1次作业:软院时光机\n" +
  85. "穿梭时光,回到那美好而又艰苦的2002\n";
  86. // 3 构造提示词
  87. String promptTemplate =
  88. "假设你现在是一位英文专业的教师,正在批阅一位同学的写作作业,作业描述是:%s"
  89. + "请做如下工作:\n"
  90. + "1. 满分100分,请综合英文用词、句式等方面,给出评分和评分原因\n"
  91. + "2. 针对文章,给出改进的内容,格式为“xxx”可以修改为“xxx”\n"
  92. + "3. 给出修改后的完整文章内容\n"
  93. + "其余的任何描述都不需要,也不需要任何交互!\n"
  94. + "以下是学生作业内容:\n\n"
  95. + "%s";
  96. String prompt = String.format(promptTemplate, assignmentDescription, content);
  97. log.info("内置提示词为:" + prompt);
  98. // 4 请求AI
  99. List<AIEntry> entry = new ArrayList<>();
  100. entry.add(new AIEntry("user", prompt));
  101. AIRequestService.AIResponse response = aiRequestService.requestChatGLM4(entry);
  102. // 5 获取返回数据
  103. String retContent;
  104. String role;
  105. Long timestamp;
  106. try {
  107. retContent = response.getChoices().get(0).getMessage().getContent();
  108. role = response.getChoices().get(0).getMessage().getRole();
  109. timestamp = response.getCreated();
  110. log.info("AI返回的数据为:" + retContent);
  111. } catch (Exception e) {
  112. log.error("AI请求返回的数据内容有误,数据为{}", response);
  113. throw new MyException(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase() + ":" + "请求失败");
  114. }
  115. }
  116. }