|
@@ -1,72 +1,145 @@
|
|
|
package org.springblade.common.utils;
|
|
|
+
|
|
|
import okhttp3.*;
|
|
|
import com.google.gson.Gson;
|
|
|
+import com.google.gson.JsonObject;
|
|
|
+import com.google.gson.JsonParser;
|
|
|
import org.springblade.common.vo.DeepSeekResponse;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.util.Collections;
|
|
|
+
|
|
|
@Component
|
|
|
public class DeepSeekClient {
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(DeepSeekClient.class);
|
|
|
+
|
|
|
private static final String API_KEY = "sk-16ebce9ef2eb40a68f29d9c2a70fe6b6";
|
|
|
+
|
|
|
private static final String API_URL = "https://api.deepseek.com/v1/chat/completions";
|
|
|
|
|
|
private final OkHttpClient client = new OkHttpClient();
|
|
|
private final Gson gson = new Gson();
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
- * 请求deepseek
|
|
|
+ * 请求 DeepSeek API
|
|
|
* @param prompt 需要发送的文字
|
|
|
- * @return
|
|
|
+ * @return API 响应
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
public String callDeepSeek(String prompt) throws IOException {
|
|
|
+ // 使用 V3 模型
|
|
|
+ DeepSeekRequest request = new DeepSeekRequest(prompt, "deepseek-chat");
|
|
|
+
|
|
|
// 构造请求体
|
|
|
+ String requestJson = gson.toJson(request);
|
|
|
+ logger.debug("Sending request to DeepSeek: {}", requestJson);
|
|
|
+
|
|
|
RequestBody body = RequestBody.create(
|
|
|
- gson.toJson(new DeepSeekRequest(prompt)),
|
|
|
+ requestJson,
|
|
|
MediaType.parse("application/json")
|
|
|
);
|
|
|
|
|
|
// 构造请求
|
|
|
- Request request = new Request.Builder()
|
|
|
+ Request httpRequest = new Request.Builder()
|
|
|
.url(API_URL)
|
|
|
.post(body)
|
|
|
.addHeader("Authorization", "Bearer " + API_KEY)
|
|
|
.addHeader("Content-Type", "application/json")
|
|
|
+ .addHeader("Accept", "application/json")
|
|
|
.build();
|
|
|
|
|
|
// 发送请求并获取响应
|
|
|
- try (Response response = client.newCall(request).execute()) {
|
|
|
- if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
|
|
|
- return response.body().string();
|
|
|
+ try (Response response = client.newCall(httpRequest).execute()) {
|
|
|
+ if (!response.isSuccessful()) {
|
|
|
+ String errorBody = response.body() != null ? response.body().string() : "No error body";
|
|
|
+ logger.error("API request failed with code: {}, Body: {}", response.code(), errorBody);
|
|
|
+ throw new IOException("API request failed with code: " + response.code() + ", Body: " + errorBody);
|
|
|
+ }
|
|
|
+
|
|
|
+ String responseBody = response.body().string();
|
|
|
+ logger.debug("Received response from DeepSeek: {}", responseBody);
|
|
|
+ return responseBody;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 解析响应获取结果
|
|
|
- * @param responseJson 请求deepseek 返回响应值
|
|
|
- * @return
|
|
|
+ * @param responseJson 请求 deepseek 返回响应值
|
|
|
+ * @return 消息内容
|
|
|
+ */
|
|
|
+ public String analysisResponse(String responseJson) {
|
|
|
+ try {
|
|
|
+ // 使用 JsonParser 替代 Gson.fromJson 以便更好的错误处理
|
|
|
+ JsonObject jsonObject = JsonParser.parseString(responseJson).getAsJsonObject();
|
|
|
+
|
|
|
+ // 检查是否有错误
|
|
|
+ if (jsonObject.has("error")) {
|
|
|
+ JsonObject error = jsonObject.getAsJsonObject("error");
|
|
|
+ String errorMsg = error.get("message").getAsString();
|
|
|
+ logger.error("API returned error: {}", errorMsg);
|
|
|
+ return "API Error: " + errorMsg;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析为对象
|
|
|
+ DeepSeekResponse response = gson.fromJson(jsonObject, DeepSeekResponse.class);
|
|
|
+
|
|
|
+ // 安全地获取 content 内容
|
|
|
+ if (response == null) {
|
|
|
+ logger.error("Failed to parse response JSON");
|
|
|
+ return "Error: Failed to parse response";
|
|
|
+ }
|
|
|
+
|
|
|
+ if (response.getChoices() == null || response.getChoices().isEmpty()) {
|
|
|
+ logger.error("No choices in response");
|
|
|
+ return "Error: No choices in response";
|
|
|
+ }
|
|
|
+
|
|
|
+ DeepSeekResponse.Choice firstChoice = response.getChoices().get(0);
|
|
|
+ if (firstChoice.getMessage() == null) {
|
|
|
+ logger.error("No message in choice");
|
|
|
+ return "Error: No message in choice";
|
|
|
+ }
|
|
|
+
|
|
|
+ return firstChoice.getMessage().getContent();
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("Error parsing response: {}", e.getMessage());
|
|
|
+ return "Error: " + e.getMessage();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 直接获取精简后的内容(一步完成调用和解析)
|
|
|
+ * @param prompt 提示词
|
|
|
+ * @return 精简后的内容
|
|
|
*/
|
|
|
- public String analysisRespone(String responseJson){
|
|
|
- // 解析JSON响应
|
|
|
- Gson gson = new Gson();
|
|
|
- DeepSeekResponse response = gson.fromJson(responseJson, DeepSeekResponse.class);
|
|
|
- // 获取content内容
|
|
|
- String content = "";
|
|
|
- if (response != null && response.getChoices() != null && !response.getChoices().isEmpty() && response.getChoices().get(0).getMessage() != null) {
|
|
|
- content = response.getChoices().get(0).getMessage().getContent();
|
|
|
+ public String getSimplifiedContent(String prompt) {
|
|
|
+ try {
|
|
|
+ String response = callDeepSeek(prompt);
|
|
|
+ return analysisResponse(response);
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error("API call failed: {}", e.getMessage());
|
|
|
+ return "Error: " + e.getMessage();
|
|
|
}
|
|
|
- return content;
|
|
|
}
|
|
|
|
|
|
- // 请求参数类
|
|
|
+ // 请求参数类 - 支持多种模型
|
|
|
static class DeepSeekRequest {
|
|
|
- String model = "deepseek-chat";
|
|
|
+ String model;
|
|
|
Message[] messages;
|
|
|
double temperature = 0.7;
|
|
|
int max_tokens = 2000;
|
|
|
+ boolean stream = false;
|
|
|
|
|
|
public DeepSeekRequest(String prompt) {
|
|
|
+ this(prompt, "deepseek-chat"); // 默认使用 V3 模型
|
|
|
+ }
|
|
|
+
|
|
|
+ public DeepSeekRequest(String prompt, String model) {
|
|
|
+ this.model = model;
|
|
|
this.messages = new Message[]{
|
|
|
new Message("user", prompt)
|
|
|
};
|
|
@@ -82,5 +155,4 @@ public class DeepSeekClient {
|
|
|
this.content = content;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-}
|
|
|
+}
|