AIRequestService.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package com.njuzr.eaibackend.service;
  2. import com.fasterxml.jackson.annotation.JsonProperty;
  3. import com.njuzr.eaibackend.exception.MyException;
  4. import com.njuzr.eaibackend.po.AIEntry;
  5. import com.njuzr.eaibackend.utils.WebClientUtil;
  6. import lombok.AllArgsConstructor;
  7. import lombok.Data;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.http.HttpStatus;
  11. import org.springframework.stereotype.Service;
  12. import java.util.List;
  13. /**
  14. * @author: Leonezhurui
  15. * @Date: 2024/3/2 - 00:00
  16. * @Package: EAI-Backend
  17. */
  18. @Slf4j
  19. @Service
  20. public class AIRequestService {
  21. @Value("${openai.chatgpt.model}")
  22. private String model;
  23. @Value("${openai.chatgpt.api.key}")
  24. private String key;
  25. private final WebClientUtil chatgptClient;
  26. private final WebClientUtil chatglmClient;
  27. private final WebClientUtil qwenClient;
  28. private final String prefix = "/v1/chat/completions";
  29. @Value("${config.llm.ChatGLM3.url}")
  30. private String glm3Url;
  31. @Value("${config.llm.ChatGLM4.url}")
  32. private String glm4Url;
  33. @Value("${config.llm.ChatGLM4.token}")
  34. private String glm4Token;
  35. @Value("${config.llm.ChatGLM4.modelName}")
  36. private String glm4ModelName;
  37. @Value("${config.llm.QWen14B.url}")
  38. private String qWen14BUrl;
  39. @Value("${config.llm.PreSentenceWorkflowChatGLM4.url}")
  40. private String preSentenceWorkflowglm4Url;
  41. @Value("${config.llm.PreSentenceWorkflowChatGLM4.token}")
  42. private String preSentenceWorkflowglm4Token;
  43. public AIRequestService(WebClientUtil chatgptClient, WebClientUtil chatglmClient, WebClientUtil qwenClient) {
  44. this.chatgptClient = chatgptClient;
  45. this.chatglmClient = chatglmClient;
  46. this.qwenClient = qwenClient;
  47. }
  48. public AIResponse requestChatGPT(List<AIEntry> messages) {
  49. MyRequestObject requestObject = new MyRequestObject(model, messages,0.7);
  50. String chatgptUrl = "https://api.openai.com";
  51. try {
  52. return chatgptClient.postWithToken(chatgptUrl+prefix, requestObject, AIResponse.class, key);
  53. } catch (Exception e) {
  54. log.error("WebClient请求失败,AI请求失败~");
  55. throw new MyException(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()+":"+"服务器请求AI出错");
  56. }
  57. }
  58. public AIResponse requestChatGLM(List<AIEntry> messages) {
  59. MyRequestObject requestObject = new MyRequestObject("ChatGLM3", messages,0.7);
  60. String url = glm3Url;
  61. try {
  62. return chatglmClient.post(url+prefix, requestObject, AIResponse.class);
  63. } catch (Exception e) {
  64. log.error("WebClient请求失败,AI请求失败~");
  65. throw new MyException(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()+":"+"服务器请求AI出错");
  66. }
  67. }
  68. public AIResponse requestChatGLM4(List<AIEntry> messages) {
  69. MyRequestObject requestObject = new MyRequestObject(glm4ModelName, messages,0.7);
  70. String url = glm4Url;
  71. try {
  72. return chatglmClient.postWithToken(url+prefix, requestObject, AIResponse.class, glm4Token);
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. log.error("WebClient请求失败,AI请求失败~"+e.getMessage());
  76. throw new MyException(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()+":"+"服务器请求AI出错");
  77. }
  78. }
  79. public AIResponse requestQWen(List<AIEntry> messages) {
  80. MyRequestObject requestObject = new MyRequestObject("QWen14B", messages,0.7);
  81. String url = qWen14BUrl;
  82. try {
  83. return qwenClient.post(url+prefix, requestObject, AIResponse.class);
  84. } catch (Exception e) {
  85. log.error("WebClient请求失败,AI请求失败~");
  86. throw new MyException(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()+":"+"服务器请求AI出错");
  87. }
  88. }
  89. public WorkflowRunResponse requestPreSentenceWorkflowWithRetry(String content, String assignmentDescription, String sentence) {
  90. int retry = 0;
  91. while (retry < 3) {
  92. WorkflowRunResponse response = requestPreSentenceWorkflow(content, assignmentDescription, sentence);
  93. if (response == null) {
  94. log.error("requestPreSentenceWorkflowWithRetry failed, retry: {}/{}", retry+1, 3);
  95. retry++;
  96. continue;
  97. }
  98. return response;
  99. }
  100. throw new MyException(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()+"AI请求失败");
  101. }
  102. public WorkflowRunResponse requestPreSentenceWorkflow(String content, String assignmentDescription, String sentence) {
  103. MyWorkflowRequestInput input = new MyWorkflowRequestInput(content, assignmentDescription,sentence);
  104. MyWorkflowRequestObject requestObject = new MyWorkflowRequestObject("abc-1234", "blocking", input);
  105. String url = preSentenceWorkflowglm4Url;
  106. try {
  107. return chatglmClient.postWithToken(url, requestObject, WorkflowRunResponse.class, preSentenceWorkflowglm4Token);
  108. } catch (Exception e) {
  109. log.error("requestPreSentenceWorkflow failed, reason: {}", e.getMessage());
  110. return null;
  111. }
  112. }
  113. @Data
  114. @AllArgsConstructor
  115. static class MyWorkflowRequestObject {
  116. private String user;
  117. private String response_mode;
  118. private MyWorkflowRequestInput inputs;
  119. }
  120. @Data
  121. @AllArgsConstructor
  122. static class MyWorkflowRequestInput {
  123. private String content;
  124. private String assignment_description;
  125. private String sentence;
  126. }
  127. @Data
  128. static class WorkflowRunResponse {
  129. private String task_id;
  130. private String workflow_run_id;
  131. private WorkflowRunData data;
  132. }
  133. @Data
  134. static
  135. class WorkflowRunData {
  136. private String id;
  137. private String workflow_id;
  138. private String status;
  139. private Outputs outputs;
  140. private Object error;
  141. private double elapsed_time;
  142. private int total_tokens;
  143. private int total_steps;
  144. private long created_at;
  145. private long finished_at;
  146. }
  147. @Data
  148. static
  149. class Outputs {
  150. private String result_type;
  151. private String result_category;
  152. private String result_content;
  153. }
  154. @Data
  155. @AllArgsConstructor
  156. static class MyRequestObject {
  157. private String model;
  158. private List<AIEntry> messages;
  159. private Double temperature;
  160. }
  161. @Data
  162. public static class AIResponse {
  163. private String id;
  164. private List<Choice> choices;
  165. private Long created;
  166. private String model;
  167. private String object;
  168. private String system_fingerprint;
  169. private Usage usage;
  170. }
  171. @Data
  172. public static class Choice {
  173. private String finish_reason;
  174. private int index;
  175. private String logprobs;
  176. private ResponseMessage message;
  177. }
  178. @Data
  179. public static class ResponseMessage {
  180. private String content; // AI生成的回复
  181. private String role;
  182. private String function_call;
  183. private List<String> tool_calls;
  184. }
  185. @Data
  186. public static class Usage {
  187. private int completion_tokens;
  188. private int prompt_tokens;
  189. private int total_tokens;
  190. }
  191. }