cr před 1 dnem
rodič
revize
a437d1a861

+ 244 - 252
blade-common/src/main/java/org/springblade/common/utils/DeepSeekClient.java

@@ -1,252 +1,244 @@
-//
-//
-//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.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;
-//            }
-//        }
-//    }
-//}
-//
+
+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.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;
+            }
+        }
+    }
+}

+ 42 - 45
blade-service/blade-archive/src/main/java/org/springblade/archive/service/impl/ArchiveAiNameServiceImpl.java

@@ -25,6 +25,7 @@ import org.springblade.archive.service.IArchiveAiNameService;
 import org.springblade.archive.vo.ArchiveAiNameVO;
 import org.springblade.archive.vo.ArchiveAiNameVO1;
 
+import org.springblade.common.utils.DeepSeekClient;
 import org.springblade.core.mp.base.BaseServiceImpl;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.jdbc.core.BeanPropertyRowMapper;
@@ -58,58 +59,54 @@ public class ArchiveAiNameServiceImpl extends BaseServiceImpl<ArchiveAiNameMappe
 	@Autowired
 	private JdbcTemplate jdbcTemplate;
 
-//	@Autowired
-//	private DeepSeekClient deepSeekClient;
+	@Autowired
+	private DeepSeekClient deepSeekClient;
 
 	@Resource(name = "taskExecutor1")
 	private ThreadPoolExecutor executor;
 
 	private final Semaphore apiSemaphore = new Semaphore(MAX_CONCURRENT_REQUESTS);
 
-//	@Override
-//	@Async("taskExecutor1")
-//	@Transactional
-//	public void syncCreatAiName(List<ArchiveAiName> aiNames) {
-//		aiNames.parallelStream().forEach(name -> {
-//			try {
-//				apiSemaphore.acquire();
-//				try {
-//					String content = deepSeekClient.getSimplifiedContent(name.getArchiveName() + DEEPSEEK_ARCHIVE_NAME);
-//					if (content.startsWith("API_ERROR:") ||
-//							content.startsWith("PARSE_ERROR:") ||
-//							content.startsWith("IO_ERROR:")||content.startsWith("Error:")) {
-//						name.setStatus(4);
-//						name.setArchiveNameAi("获取失败");
-//					} else {
-//						name.setArchiveNameAi(content);
-//						name.setStatus(2); // 标记为成功
-//					}
-//				} finally {
-//					apiSemaphore.release();
-//				}
-//			} catch (InterruptedException e) {
-//				Thread.currentThread().interrupt();
-//				name.setStatus(4); // 设置失败状态
-//				name.setArchiveNameAi("获取失败");
-//			} catch (Exception e) {
-//				name.setStatus(4); // 设置失败状态
-//				name.setArchiveNameAi("获取失败");
-//			}
-//		});
-//
-//		// 分批更新数据库
-//		int batchSize = 100;
-//		for (int i = 0; i < aiNames.size(); i += batchSize) {
-//			int end = Math.min(i + batchSize, aiNames.size());
-//			List<ArchiveAiName> batch = aiNames.subList(i, end);
-//			this.updateBatchById(batch);
-//		}
-//	}
-
-    @Override
-    public void syncCreatAiName(List<ArchiveAiName> aiNames) throws IOException {
+	@Override
+	@Async("taskExecutor1")
+	@Transactional
+	public void syncCreatAiName(List<ArchiveAiName> aiNames) {
+		aiNames.parallelStream().forEach(name -> {
+			try {
+				apiSemaphore.acquire();
+				try {
+					String content = deepSeekClient.getSimplifiedContent(name.getArchiveName() + DEEPSEEK_ARCHIVE_NAME);
+					if (content.startsWith("API_ERROR:") ||
+							content.startsWith("PARSE_ERROR:") ||
+							content.startsWith("IO_ERROR:")||content.startsWith("Error:")) {
+						name.setStatus(4);
+						name.setArchiveNameAi("获取失败");
+					} else {
+						name.setArchiveNameAi(content);
+						name.setStatus(2); // 标记为成功
+					}
+				} finally {
+					apiSemaphore.release();
+				}
+			} catch (InterruptedException e) {
+				Thread.currentThread().interrupt();
+				name.setStatus(4); // 设置失败状态
+				name.setArchiveNameAi("获取失败");
+			} catch (Exception e) {
+				name.setStatus(4); // 设置失败状态
+				name.setArchiveNameAi("获取失败");
+			}
+		});
+
+		// 分批更新数据库
+		int batchSize = 100;
+		for (int i = 0; i < aiNames.size(); i += batchSize) {
+			int end = Math.min(i + batchSize, aiNames.size());
+			List<ArchiveAiName> batch = aiNames.subList(i, end);
+			this.updateBatchById(batch);
+		}
+	}
 
-    }
 
     @Override
 	public List<ArchiveAiNameVO> getArchiveAiTask(Long projectId, Long contractId) {