From 109d009dc10aa3e5fed0027c4027de34256e68f1 Mon Sep 17 00:00:00 2001
From: wangxy <1481820854@qq.com>
Date: Thu, 6 Mar 2025 10:15:48 +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

---
 .../src/main/resources/application.yml        |  8 ++++++++
 .../web/service/DeepSeekApiService.java       | 20 ++++++++++---------
 .../web/service/DeepSeekLocalService.java     | 17 +++++++---------
 3 files changed, 26 insertions(+), 19 deletions(-)

diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml
index bcf962a..c08c991 100644
--- a/ruoyi-admin/src/main/resources/application.yml
+++ b/ruoyi-admin/src/main/resources/application.yml
@@ -159,3 +159,11 @@ aj:
     aes-status: true
     # 滑动干扰项(0/1/2)
     interference-options: 0
+
+#seepseek配置
+deepseek:
+  api-key: sk-f1a69dbdd92a428db6737a7fe1c64740
+  base-url: https://api.deepseek.com/chat/completions  #http://192.168.110.56:11434/v1/chat/completions
+  model: deepseek-chat    #deepseek-r1:1.5b
+
+
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 582904a..5993d53 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
@@ -4,6 +4,7 @@ import cn.hutool.http.HttpResponse;
 import cn.hutool.http.HttpUtil;
 import cn.hutool.http.Method;
 import cn.hutool.json.JSONUtil;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.*;
 import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
@@ -23,14 +24,15 @@ import java.util.Optional;
 @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
+    @Value(value = "${deepseek.base-url}")
+    private String baseUrl;
 
-    private final RestTemplate restTemplate;
+    @Value(value = "${deepseek.api-key}")
+    private String apiKey;
+
+    @Value(value = "${deepseek.model}")
+    private String model;
 
-    public DeepSeekApiService(RestTemplate restTemplate) {
-        this.restTemplate = restTemplate;
-    }
 
     public String callDeepSeekApi(String content) {
         // 构建请求体对象
@@ -43,14 +45,14 @@ public class DeepSeekApiService {
         userMessage.setContent(content);
 
         DeepSeekRequest requestBody = new DeepSeekRequest();
-        requestBody.setModel("deepseek-chat");
+        requestBody.setModel(model);
         requestBody.setMessages(Arrays.asList(systemMessage, userMessage));
         requestBody.setStream(false);
         // 执行 HTTP 请求
-        HttpResponse response = HttpUtil.createRequest(Method.POST, API_URL)
+        HttpResponse response = HttpUtil.createRequest(Method.POST, baseUrl)
                 .body(JSONUtil.toJsonStr(requestBody))
                 .header("Content-Type", "application/json")
-                .header("Authorization", "Bearer " + API_KEY)
+                .header("Authorization", "Bearer " + apiKey)
                 .header("Accept", "application/json")
                 .execute();
         DeepSeekResponse chatResponse = JSONUtil.toBean(response.body(), DeepSeekResponse.class);
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 5d18516..e7da1f0 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
@@ -4,9 +4,8 @@ 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.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
-import org.springframework.web.client.RestTemplate;
 
 import java.util.Arrays;
 import java.util.Optional;
@@ -23,13 +22,11 @@ import java.util.Optional;
 @Service
 public class DeepSeekLocalService {
 
-    private static final String LOCAL_API_URL = "http://192.168.110.56:11434/v1/chat/completions";
+    @Value(value = "${deepseek.base-url}")
+    private String baseUrl;
 
-    private final RestTemplate restTemplate;
-
-    public DeepSeekLocalService(RestTemplate restTemplate) {
-        this.restTemplate = restTemplate;
-    }
+    @Value(value = "${deepseek.model}")
+    private String model;
 
     public String callLocalDeepSeek(String content) {
         // 构建请求体
@@ -42,11 +39,11 @@ public class DeepSeekLocalService {
         userMessage.setContent(content);
 
         DeepSeekRequest requestBody = new DeepSeekRequest();
-        requestBody.setModel("deepseek-r1:1.5b");
+        requestBody.setModel(model);
         requestBody.setMessages(Arrays.asList(systemMessage, userMessage));
         requestBody.setStream(false);
         // 执行 HTTP 请求
-        HttpResponse response = HttpUtil.createRequest(Method.POST, LOCAL_API_URL)
+        HttpResponse response = HttpUtil.createRequest(Method.POST, baseUrl)
                 .body(JSONUtil.toJsonStr(requestBody))
                 .header("Content-Type", "application/json")
                 .execute();