From 21afeb22b83c5bdd31c8ded5220d36e743007693 Mon Sep 17 00:00:00 2001 From: wangxy <1481820854@qq.com> Date: Fri, 14 Feb 2025 14:55:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/controller/home/LawController.java | 10 +++ .../web/service/DeepSeekLocalService.java | 63 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/DeepSeekLocalService.java 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 8793612..67477ec 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 @@ -7,6 +7,7 @@ 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.framework.web.service.DeepSeekLocalService; import com.ruoyi.system.domain.SysColumn; import com.ruoyi.system.domain.SysColumnVO; import com.ruoyi.system.domain.SysSpecial; @@ -95,6 +96,15 @@ public class LawController extends BaseController { } + @Autowired + private DeepSeekLocalService deepSeekLocalService; + + @GetMapping("/call-local-deepseek") + public R callLocalDeepSeek(@RequestParam(required = false) String content) { + return R.ok(deepSeekLocalService.callLocalDeepSeek(content)); + } + + } diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/DeepSeekLocalService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/DeepSeekLocalService.java new file mode 100644 index 0000000..7697667 --- /dev/null +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/DeepSeekLocalService.java @@ -0,0 +1,63 @@ +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: DeepSeekLocalService + * Package: com.ruoyi.framework.web.service + * Description: + * + * @Author wangxy + * @Create 2025/2/14 14:50 + * @Version 1.0 + */ +@Service +public class DeepSeekLocalService { + + private static final String LOCAL_API_URL = "http://localhost:8081/chat/completions"; + + private final RestTemplate restTemplate; + + public DeepSeekLocalService(RestTemplate restTemplate) { + this.restTemplate = restTemplate; + } + + public String callLocalDeepSeek(String content) { + // 设置请求头 + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + + // 构建请求体 + 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( + LOCAL_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()); + } + } +}