From a6032ba527b31697d1a3be91b118ecae2a6c9ea9 Mon Sep 17 00:00:00 2001
From: wangxy <1481820854@qq.com>
Date: Mon, 24 Feb 2025 11:19:03 +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 | 2 +-
ruoyi-framework/pom.xml | 4 ++
.../web/service/DeepSeekApiService.java | 46 +++++++-----
.../web/service/DeepSeekLocalService.java | 46 ++++++------
.../web/service/DeepSeekResponse.java | 70 +++++++++++++++++++
5 files changed, 127 insertions(+), 41 deletions(-)
create mode 100644 ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/DeepSeekResponse.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 0245633..339eb0b 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
@@ -101,7 +101,7 @@ public class LawController extends BaseController {
private DeepSeekLocalService deepSeekLocalService;
@PostMapping("/call-local-deepseek")
- public R callLocalDeepSeek(@RequestParam(required = false) String content) {
+ public R callLocalDeepSeek(@RequestParam(required = false) String content) throws JsonProcessingException {
return R.ok(deepSeekLocalService.callLocalDeepSeek(content));
}
diff --git a/ruoyi-framework/pom.xml b/ruoyi-framework/pom.xml
index de9e4a9..4a4e189 100644
--- a/ruoyi-framework/pom.xml
+++ b/ruoyi-framework/pom.xml
@@ -83,6 +83,10 @@
captcha-spring-boot-starter
1.2.7
+
+ org.projectlombok
+ lombok
+
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
index e9cac7b..582904a 100644
--- 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
@@ -1,10 +1,15 @@
package com.ruoyi.framework.web.service;
+import cn.hutool.http.HttpResponse;
+import cn.hutool.http.HttpUtil;
+import cn.hutool.http.Method;
+import cn.hutool.json.JSONUtil;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
+import java.util.Optional;
/**
* ClassName: DeepSeekApiService
@@ -28,10 +33,6 @@ public class DeepSeekApiService {
}
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");
@@ -45,21 +46,28 @@ public class DeepSeekApiService {
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());
- }
+ // 执行 HTTP 请求
+ HttpResponse response = HttpUtil.createRequest(Method.POST, API_URL)
+ .body(JSONUtil.toJsonStr(requestBody))
+ .header("Content-Type", "application/json")
+ .header("Authorization", "Bearer " + API_KEY)
+ .header("Accept", "application/json")
+ .execute();
+ DeepSeekResponse chatResponse = JSONUtil.toBean(response.body(), DeepSeekResponse.class);
+ System.out.println("返回数据: " + chatResponse.getChoices().get(0).getMessage().getContent());
+ return extractResponse(chatResponse);
+ }
+
+ /**
+ * 提取回复内容
+ */
+ private String extractResponse(DeepSeekResponse response) {
+ return Optional.ofNullable(response)
+ .map(DeepSeekResponse::getChoices)
+ .filter(choices -> !choices.isEmpty())
+ .map(choices -> choices.get(0))
+ .map(choice -> choice.getMessage().getContent())
+ .orElseThrow(() -> new RuntimeException("Empty response"));
}
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
index dd94cc6..5d18516 100644
--- 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
@@ -1,10 +1,15 @@
package com.ruoyi.framework.web.service;
+import cn.hutool.http.HttpResponse;
+import cn.hutool.http.HttpUtil;
+import cn.hutool.http.Method;
+import cn.hutool.json.JSONUtil;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
+import java.util.Optional;
/**
* ClassName: DeepSeekLocalService
@@ -27,10 +32,6 @@ public class DeepSeekLocalService {
}
public String callLocalDeepSeek(String content) {
- // 设置请求头
- HttpHeaders headers = new HttpHeaders();
- headers.setContentType(MediaType.APPLICATION_JSON);
-
// 构建请求体
DeepSeekRequest.Message systemMessage = new DeepSeekRequest.Message();
systemMessage.setRole("system");
@@ -44,22 +45,25 @@ public class DeepSeekLocalService {
requestBody.setModel("deepseek-r1:1.5b");
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) {
- System.out.println("成功了!");
- System.out.println("返回数据: " + response.getBody());
- return response.getBody();
- } else {
- throw new RuntimeException("Request failed with status code: " + response.getStatusCode());
- }
+ // 执行 HTTP 请求
+ HttpResponse response = HttpUtil.createRequest(Method.POST, LOCAL_API_URL)
+ .body(JSONUtil.toJsonStr(requestBody))
+ .header("Content-Type", "application/json")
+ .execute();
+ DeepSeekResponse chatResponse = JSONUtil.toBean(response.body(), DeepSeekResponse.class);
+ System.out.println("返回数据: " + chatResponse.getChoices().get(0).getMessage().getContent());
+ return extractResponse(chatResponse);
+ }
+
+ /**
+ * 提取回复内容
+ */
+ private String extractResponse(DeepSeekResponse response) {
+ return Optional.ofNullable(response)
+ .map(DeepSeekResponse::getChoices)
+ .filter(choices -> !choices.isEmpty())
+ .map(choices -> choices.get(0))
+ .map(choice -> choice.getMessage().getContent())
+ .orElseThrow(() -> new RuntimeException("Empty response"));
}
}
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/DeepSeekResponse.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/DeepSeekResponse.java
new file mode 100644
index 0000000..d058e56
--- /dev/null
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/DeepSeekResponse.java
@@ -0,0 +1,70 @@
+package com.ruoyi.framework.web.service;
+
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * ClassName: DeepSeekResponse
+ * Package: com.ruoyi.framework.web.service
+ * Description:
+ *
+ * @Author wangxy
+ * @Create 2025/2/24 10:38
+ * @Version 1.0
+ */
+@Data
+public class DeepSeekResponse {
+
+ private String id;
+
+ private String object;
+
+ private Integer created;
+
+ private String model;
+
+ private List choices;
+
+ private Usage usage;
+
+ private String system_fingerprint;
+
+ @Data
+ public static class Choices {
+
+ private Integer index;
+
+ private Message message;
+
+ private String logprobs;
+
+ private String finish_reason;
+
+ @Data
+ public static class Message {
+ private String role;
+ private String content;
+ }
+ }
+
+ @Data
+ public static class Usage {
+ private Integer prompt_tokens;
+
+ private Integer completion_tokens;
+
+ private Integer total_tokens;
+
+ private Prompt_tokens_details prompt_tokens_details;
+
+ private Integer prompt_cache_hit_tokens;
+
+ private Integer prompt_cache_miss_tokens;
+
+ @Data
+ public static class Prompt_tokens_details {
+ private Integer cached_tokens;
+ }
+ }
+}