1 line
6.3 KiB
JSON
1 line
6.3 KiB
JSON
{"repo": "aniping/minemind", "files": {"src/main/java/com/steve/ai/llm/DeepSeekClient.java": {"d20489bfa508ec2edec95ec9afe7952042c5e7d7": {"t": 1785745686.563661, "n": 6048, "h": "decd3697be97668c", "c": "package com.steve.ai.llm;\n\nimport com.google.gson.JsonArray;\nimport com.google.gson.JsonObject;\nimport com.google.gson.JsonParser;\nimport com.steve.ai.SteveMod;\nimport com.steve.ai.config.SteveConfig;\n\nimport java.net.URI;\nimport java.net.http.HttpClient;\nimport java.net.http.HttpRequest;\nimport java.net.http.HttpResponse;\nimport java.time.Duration;\n\npublic class DeepSeekClient {\n private static final int MAX_RETRIES = 3;\n private static final int INITIAL_RETRY_DELAY_MS = 1000;\n\n private final HttpClient client;\n private final String apiKey;\n private final String baseUrl;\n\n public DeepSeekClient() {\n this.apiKey = SteveConfig.DEEPSEEK_API_KEY.get();\n this.baseUrl = SteveConfig.DEEPSEEK_BASE_URL.get();\n this.client = HttpClient.newBuilder()\n .connectTimeout(Duration.ofSeconds(30))\n .build();\n }\n\n public String sendRequest(String systemPrompt, String userPrompt) {\n if (apiKey == null || apiKey.isBlank()) {\n SteveMod.LOGGER.error(\"DeepSeek API key not configured.\");\n return null;\n }\n\n JsonObject requestBody = buildRequestBody(systemPrompt, userPrompt);\n\n HttpRequest request = HttpRequest.newBuilder()\n .uri(URI.create(chatCompletionsUrl(baseUrl)))\n .header(\"Authorization\", \"Bearer \" + apiKey)\n .header(\"Content-Type\", \"application/json\")\n .timeout(Duration.ofSeconds(60))\n .POST(HttpRequest.BodyPublishers.ofString(requestBody.toString()))\n .build();\n\n for (int attempt = 0; attempt < MAX_RETRIES; attempt++) {\n try {\n HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());\n\n if (response.statusCode() == 200) {\n String responseBody = response.body();\n if (responseBody == null || responseBody.isEmpty()) {\n SteveMod.LOGGER.error(\"DeepSeek API returned empty response\");\n return null;\n }\n return parseResponse(responseBody);\n }\n\n if (response.statusCode() == 429 || response.statusCode() >= 500) {\n if (attempt < MAX_RETRIES - 1) {\n int delayMs = INITIAL_RETRY_DELAY_MS * (int) Math.pow(2, attempt);\n SteveMod.LOGGER.warn(\"DeepSeek API request failed with status {}, retrying in {}ms\",\n response.statusCode(), delayMs);\n Thread.sleep(delayMs);\n continue;\n }\n }\n\n SteveMod.LOGGER.error(\"DeepSeek API request failed: {}\", response.statusCode());\n SteveMod.LOGGER.error(\"DeepSeek response body: {}\", response.body());\n return null;\n } catch (InterruptedException e) {\n Thread.currentThread().interrupt();\n SteveMod.LOGGER.error(\"DeepSeek request interrupted\", e);\n return null;\n } catch (Exception e) {\n if (attempt < MAX_RETRIES - 1) {\n int delayMs = INITIAL_RETRY_DELAY_MS * (int) Math.pow(2, attempt);\n SteveMod.LOGGER.warn(\"Error communicating with DeepSeek API, retrying in {}ms\", delayMs, e);\n try {\n Thread.sleep(delayMs);\n } catch (InterruptedException interrupted) {\n Thread.currentThread().interrupt();\n return null;\n }\n } else {\n SteveMod.LOGGER.error(\"Error communicating with DeepSeek API after retries\", e);\n return null;\n }\n }\n }\n\n return null;\n }\n\n private JsonObject buildRequestBody(String systemPrompt, String userPrompt) {\n JsonObject body = new JsonObject();\n body.addProperty(\"model\", SteveConfig.DEEPSEEK_MODEL.get());\n body.addProperty(\"temperature\", SteveConfig.DEEPSEEK_TEMPERATURE.get());\n body.addProperty(\"max_tokens\", SteveConfig.DEEPSEEK_MAX_TOKENS.get());\n\n JsonArray messages = new JsonArray();\n\n JsonObject systemMessage = new JsonObject();\n systemMessage.addProperty(\"role\", \"system\");\n systemMessage.addProperty(\"content\", systemPrompt);\n messages.add(systemMessage);\n\n JsonObject userMessage = new JsonObject();\n userMessage.addProperty(\"role\", \"user\");\n userMessage.addProperty(\"content\", userPrompt);\n messages.add(userMessage);\n\n body.add(\"messages\", messages);\n return body;\n }\n\n private String parseResponse(String responseBody) {\n try {\n JsonObject json = JsonParser.parseString(responseBody).getAsJsonObject();\n\n if (json.has(\"choices\") && !json.getAsJsonArray(\"choices\").isEmpty()) {\n JsonObject firstChoice = json.getAsJsonArray(\"choices\").get(0).getAsJsonObject();\n JsonObject message = firstChoice.getAsJsonObject(\"message\");\n if (message != null && message.has(\"content\")) {\n return message.get(\"content\").getAsString();\n }\n }\n\n SteveMod.LOGGER.error(\"Unexpected DeepSeek response format: {}\", responseBody);\n return null;\n } catch (Exception e) {\n SteveMod.LOGGER.error(\"Error parsing DeepSeek response\", e);\n return null;\n }\n }\n\n public static String chatCompletionsUrl(String baseUrl) {\n String normalized = baseUrl == null || baseUrl.isBlank()\n ? \"https://api.deepseek.com\"\n : baseUrl.trim();\n\n while (normalized.endsWith(\"/\")) {\n normalized = normalized.substring(0, normalized.length() - 1);\n }\n\n if (normalized.endsWith(\"/chat/completions\")) {\n return normalized;\n }\n\n return normalized + \"/chat/completions\";\n }\n}\n"}}}} |