diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/home/LawController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/home/LawController.java index 9343d88..8793612 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/home/LawController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/home/LawController.java @@ -6,6 +6,7 @@ import com.github.pagehelper.PageInfo; import com.github.pagehelper.page.PageMethod; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.R; +import com.ruoyi.framework.web.service.DeepSeekApiService; import com.ruoyi.system.domain.SysColumn; import com.ruoyi.system.domain.SysColumnVO; import com.ruoyi.system.domain.SysSpecial; @@ -85,4 +86,15 @@ public class LawController extends BaseController { } + @Autowired + private DeepSeekApiService deepSeekApiService; + + @GetMapping("/call-deepseek") + public R callDeepSeek(@RequestParam(required = false) String content) { + return R.ok(deepSeekApiService.callDeepSeekApi(content)); + } + + + + } diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/AppConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/AppConfig.java new file mode 100644 index 0000000..f1a5558 --- /dev/null +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/AppConfig.java @@ -0,0 +1,22 @@ +package com.ruoyi.framework.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +/** + * ClassName: AppConfig + * Package: com.ruoyi.framework.config + * Description: + * + * @Author wangxy + * @Create 2025/2/14 10:06 + * @Version 1.0 + */ +@Configuration +public class AppConfig { + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } +} diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/DeepSeekApiService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/DeepSeekApiService.java new file mode 100644 index 0000000..e9cac7b --- /dev/null +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/DeepSeekApiService.java @@ -0,0 +1,66 @@ +package com.ruoyi.framework.web.service; + +import org.springframework.http.*; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +import java.util.Arrays; + +/** + * ClassName: DeepSeekApiService + * Package: com.ruoyi.framework.web.service + * Description: + * + * @Author wangxy + * @Create 2025/2/14 9:52 + * @Version 1.0 + */ +@Service +public class DeepSeekApiService { + + private static final String API_URL = "https://api.deepseek.com/chat/completions"; + private static final String API_KEY = "sk-f1a69dbdd92a428db6737a7fe1c64740"; // 替换为你的 API Key + + private final RestTemplate restTemplate; + + public DeepSeekApiService(RestTemplate restTemplate) { + this.restTemplate = restTemplate; + } + + public String callDeepSeekApi(String content) { + // 设置请求头 + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + headers.set("Authorization", "Bearer " + API_KEY); + // 构建请求体对象 + DeepSeekRequest.Message systemMessage = new DeepSeekRequest.Message(); + systemMessage.setRole("system"); + systemMessage.setContent("You are a helpful assistant."); + + DeepSeekRequest.Message userMessage = new DeepSeekRequest.Message(); + userMessage.setRole("user"); + userMessage.setContent(content); + + DeepSeekRequest requestBody = new DeepSeekRequest(); + requestBody.setModel("deepseek-chat"); + requestBody.setMessages(Arrays.asList(systemMessage, userMessage)); + requestBody.setStream(false); + // 创建 HttpEntity + HttpEntity requestEntity = new HttpEntity<>(requestBody, headers); + // 发送 POST 请求 + ResponseEntity response = restTemplate.exchange( + API_URL, + HttpMethod.POST, + requestEntity, + String.class + ); + // 处理响应 + if (response.getStatusCode() == HttpStatus.OK) { + return response.getBody(); + } else { + throw new RuntimeException("Request failed with status code: " + response.getStatusCode()); + } + } + + +} diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/DeepSeekRequest.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/DeepSeekRequest.java new file mode 100644 index 0000000..c9d74be --- /dev/null +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/DeepSeekRequest.java @@ -0,0 +1,68 @@ +package com.ruoyi.framework.web.service; + +import java.util.List; + +/** + * ClassName: DeepSeekRequest + * Package: com.ruoyi.system.domain + * Description: + * + * @Author wangxy + * @Create 2025/2/14 10:28 + * @Version 1.0 + */ +public class DeepSeekRequest { + + private String model; + private List messages; + private boolean stream; + + // Getters and Setters + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public List getMessages() { + return messages; + } + + public void setMessages(List messages) { + this.messages = messages; + } + + public boolean isStream() { + return stream; + } + + public void setStream(boolean stream) { + this.stream = stream; + } + + // 定义 Message 内部类 + public static class Message { + private String role; + private String content; + + // Getters and Setters + public String getRole() { + return role; + } + + public void setRole(String role) { + this.role = role; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + } + +}