From 7885196aa66df4c9d53edf15ad6567b37aaaff7d Mon Sep 17 00:00:00 2001 From: Van0313 <60689272+Van0313@users.noreply.github.com> Date: Mon, 28 Apr 2025 17:39:46 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=84=E8=AE=BA+ds?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../business/util/ds/DeepSeekClientUtil.java | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/main/java/cn/van/business/util/ds/DeepSeekClientUtil.java b/src/main/java/cn/van/business/util/ds/DeepSeekClientUtil.java index a70f3c7..1d5620a 100644 --- a/src/main/java/cn/van/business/util/ds/DeepSeekClientUtil.java +++ b/src/main/java/cn/van/business/util/ds/DeepSeekClientUtil.java @@ -13,10 +13,14 @@ import java.util.Map; @Component public class DeepSeekClientUtil { + // logger + private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(DeepSeekClientUtil.class); + private static final String DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions"; // 确认 API 地址 private static final String DEEPSEEK_API_KEY = "d99b8cc6b7414cc88a5d950a3ff7585e"; // 替换为你的密钥 private static final ObjectMapper objectMapper = new ObjectMapper(); // Jackson JSON 解析器 + /** * 调用 DeepSeek API 并返回第一次回复的文本内容 * @@ -25,39 +29,53 @@ public class DeepSeekClientUtil { * @throws IOException 如果网络请求或 JSON 解析失败 * @throws IllegalArgumentException 如果输入为空或过长 */ - public String getDeepSeekResponse(String inputText) throws IOException { + public static String getDeepSeekResponse(String inputText) throws IOException { // 1. 输入校验 if (inputText == null || inputText.trim().isEmpty()) { throw new IllegalArgumentException("输入文本不能为空"); } - if (inputText.length() > 10000) { // 可根据 API 限制调整 + if (inputText.length() > 10000) { throw new IllegalArgumentException("输入文本过长"); } - // 2. 构建请求体(使用 Jackson 生成 JSON) + // 2. 构建请求体 Map requestBody = new HashMap<>(); requestBody.put("model", "deepseek-chat"); - requestBody.put("messages", new Map[]{Map.of("role", "user", "content", inputText)}); + requestBody.put("messages", new Map[]{ + Map.of("role", "user", "content", inputText) + }); requestBody.put("temperature", 0.7); String jsonBody = objectMapper.writeValueAsString(requestBody); // 3. 使用 Hutool HTTP 发送请求 - HttpResponse response = HttpRequest.of(DEEPSEEK_API_URL).method(Method.POST).header("Content-Type", "application/json").header("Authorization", "Bearer " + DEEPSEEK_API_KEY).header("Accept", "application/json").body(jsonBody).execute(); + HttpRequest request = HttpRequest.of(DEEPSEEK_API_URL) + .method(Method.POST) + .header("Content-Type", "application/json") + .header("Authorization", "Bearer " + DEEPSEEK_API_KEY) // 确保格式正确 + .header("Accept", "application/json") + .body(jsonBody); + + logger.info("请求 DeepSeek API: URL={}, Body={}", DEEPSEEK_API_URL, jsonBody); // 调试日志 + + HttpResponse response = request.execute(); // 4. 检查 HTTP 状态码 if (response.getStatus() != 200) { + logger.error("DeepSeek API 调用失败!状态码={}, 响应={}", response.getStatus(), response.body()); throw new IOException("API 调用失败,HTTP 状态码: " + response.getStatus()); } - // 5. 解析 JSON 响应(使用 Jackson) + // 5. 解析 JSON 响应 JsonNode rootNode = objectMapper.readTree(response.body()); JsonNode choices = rootNode.path("choices"); if (choices.isEmpty()) { throw new IOException("API 返回数据格式异常,未找到回复内容"); } - // 6. 提取第一个回复的 content - return choices.get(0).path("message").path("content").asText(); + return choices.get(0) + .path("message") + .path("content") + .asText(); } }