|
@@ -0,0 +1,238 @@
|
|
|
+package org.springblade.common.utils;
|
|
|
+
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.google.gson.JsonObject;
|
|
|
+import com.google.gson.JsonParser;
|
|
|
+import okhttp3.*;
|
|
|
+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.List;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+@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;
|
|
|
+ private final Gson gson = new Gson();
|
|
|
+
|
|
|
+ public DeepSeekClient() {
|
|
|
+ this.client = new OkHttpClient.Builder()
|
|
|
+ .connectTimeout(30, TimeUnit.SECONDS)
|
|
|
+ .readTimeout(60, TimeUnit.SECONDS)
|
|
|
+ .writeTimeout(30, TimeUnit.SECONDS)
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 请求 DeepSeek API
|
|
|
+ */
|
|
|
+ public String callDeepSeek(String prompt) throws IOException {
|
|
|
+ DeepSeekRequest request = new DeepSeekRequest(prompt, "deepseek-chat");
|
|
|
+ String requestJson = gson.toJson(request);
|
|
|
+
|
|
|
+ logger.debug("Sending request to DeepSeek: {}", requestJson);
|
|
|
+
|
|
|
+ Request httpRequest = new Request.Builder()
|
|
|
+ .url(API_URL)
|
|
|
+ .post(RequestBody.create(requestJson, MediaType.parse("application/json")))
|
|
|
+ .addHeader("Authorization", "Bearer " + API_KEY)
|
|
|
+ .addHeader("Content-Type", "application/json")
|
|
|
+ .addHeader("Accept", "application/json")
|
|
|
+ .build();
|
|
|
+
|
|
|
+ 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());
|
|
|
+ }
|
|
|
+ return response.body().string();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解析响应获取结果
|
|
|
+ */
|
|
|
+ public String analysisResponse(String responseJson) {
|
|
|
+ try {
|
|
|
+ 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 "Error: " + errorMsg;
|
|
|
+ }
|
|
|
+
|
|
|
+ DeepSeekResponse response = gson.fromJson(jsonObject, DeepSeekResponse.class);
|
|
|
+
|
|
|
+ if (response == null || response.getChoices() == null || response.getChoices().isEmpty()) {
|
|
|
+ logger.error("Invalid response structure");
|
|
|
+ return "Error: Invalid response structure";
|
|
|
+ }
|
|
|
+
|
|
|
+ return response.getChoices().get(0).getMessage().getContent();
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("Error parsing response: {}", e.getMessage());
|
|
|
+ return "Error: " + e.getMessage();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 直接获取精简后的内容
|
|
|
+ */
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ static class DeepSeekRequest {
|
|
|
+ private String model;
|
|
|
+ private Message[] messages;
|
|
|
+ private double temperature = 0.7;
|
|
|
+ private int max_tokens = 2000;
|
|
|
+ private boolean stream = false;
|
|
|
+
|
|
|
+ // 构造函数
|
|
|
+ public DeepSeekRequest(String prompt, String model) {
|
|
|
+ this.model = model;
|
|
|
+
|
|
|
+ // 构造专家级对话上下文
|
|
|
+ this.messages = new Message[]{
|
|
|
+ // 系统角色设定
|
|
|
+ new Message("system", "你是有20年经验的建筑施工资料专家,熟悉GB/T50328、GB50202等规范。请用专业术语回答。"),
|
|
|
+
|
|
|
+ // 用户问题
|
|
|
+ new Message("user", "作为资料管理专家,请处理以下案卷题名:" + prompt)
|
|
|
+ };
|
|
|
+
|
|
|
+ this.temperature = 0.3; // 降低随机性,提高专业性
|
|
|
+ this.max_tokens = 1000;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Getters and Setters
|
|
|
+ public String getModel() {
|
|
|
+ return model;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setModel(String model) {
|
|
|
+ this.model = model;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Message[] getMessages() {
|
|
|
+ return messages;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setMessages(Message[] messages) {
|
|
|
+ this.messages = messages;
|
|
|
+ }
|
|
|
+
|
|
|
+ public double getTemperature() {
|
|
|
+ return temperature;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setTemperature(double temperature) {
|
|
|
+ this.temperature = temperature;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getMax_tokens() {
|
|
|
+ return max_tokens;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setMax_tokens(int max_tokens) {
|
|
|
+ this.max_tokens = max_tokens;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean isStream() {
|
|
|
+ return stream;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setStream(boolean stream) {
|
|
|
+ this.stream = stream;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ static class Message {
|
|
|
+ private String role;
|
|
|
+ private String content;
|
|
|
+
|
|
|
+ // 构造函数
|
|
|
+ public Message(String role, String content) {
|
|
|
+ this.role = role;
|
|
|
+ this.content = 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ static class DeepSeekResponse {
|
|
|
+ private List<Choice> choices;
|
|
|
+
|
|
|
+ public List<Choice> getChoices() {
|
|
|
+ return choices;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setChoices(List<Choice> choices) {
|
|
|
+ this.choices = choices;
|
|
|
+ }
|
|
|
+
|
|
|
+ static class Choice {
|
|
|
+ private Message message;
|
|
|
+ private int index;
|
|
|
+ private String finish_reason;
|
|
|
+
|
|
|
+ public Message getMessage() {
|
|
|
+ return message;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setMessage(Message message) {
|
|
|
+ this.message = message;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getIndex() {
|
|
|
+ return index;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setIndex(int index) {
|
|
|
+ this.index = index;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getFinish_reason() {
|
|
|
+ return finish_reason;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setFinish_reason(String finish_reason) {
|
|
|
+ this.finish_reason = finish_reason;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|