diff --git a/ssw-common/ssw-common-biz/src/main/java/ssw/common/util/SswLLMClient.java b/ssw-common/ssw-common-biz/src/main/java/ssw/common/util/SswLLMClient.java index 8b74d9e..3ddede6 100644 --- a/ssw-common/ssw-common-biz/src/main/java/ssw/common/util/SswLLMClient.java +++ b/ssw-common/ssw-common-biz/src/main/java/ssw/common/util/SswLLMClient.java @@ -83,14 +83,28 @@ public class SswLLMClient { body.put("messages", messages); try { - // v0.16+ (BS-新61): 末尾 / normalize, 避免 /v1//chat/completions 双斜杠 + // v0.17+ (2026-07-06 BS-新63): 协议自适应 + // - OpenAI 兼容 (https://api.deepseek.com/v1, https://token-plan-cn.xiaomimimo.com/v1/) + // → baseUrl/chat/completions + // - Anthropic 兼容 (https://api.minimaxi.com/anthropic) + // → baseUrl/v1/messages String normalized = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl; - String url = normalized.endsWith("/chat/completions") ? normalized : normalized + "/chat/completions"; - log.info("[SswLLMClient] POST {} model={} maxTokens={}", url, model, maxTokens); + String url; + boolean isAnthropic = normalized.contains("/anthropic"); + if (isAnthropic) { + url = normalized.endsWith("/v1/messages") ? normalized : normalized + "/v1/messages"; + log.info("[SswLLMClient] POST {} (anthropic) model={} maxTokens={}", url, model, maxTokens); + } else { + url = normalized.endsWith("/chat/completions") ? normalized : normalized + "/chat/completions"; + log.info("[SswLLMClient] POST {} (openai) model={} maxTokens={}", url, model, maxTokens); + } + String requestBody = isAnthropic + ? toAnthropicBody(model, systemPrompt, userPrompt, maxTokens) + : body.toJSONString(); HttpResponse resp = HttpRequest.post(url) .header("Authorization", "Bearer " + credential) .header("Content-Type", "application/json") - .body(body.toJSONString()) + .body(requestBody) .timeout(READ_TIMEOUT * 1000) .execute(); @@ -100,21 +114,39 @@ public class SswLLMClient { } JSONObject respJson = JSON.parseObject(resp.body()); - if (respJson.containsKey("error")) { - log.error("[SswLLMClient] LLM error: {}", respJson.getString("error")); + if (respJson.containsKey("error") || respJson.containsKey("type") && "error".equals(respJson.getString("type"))) { + log.error("[SswLLMClient] LLM error: {}", respJson.getString("error") != null + ? respJson.getString("error") : respJson.getString("message")); return null; } - String content = respJson.getJSONArray("choices") - .getJSONObject(0) - .getJSONObject("message") - .getString("content"); - - JSONObject usage = respJson.getJSONObject("usage"); - if (usage != null) { - log.info("[SswLLMClient] usage: prompt={} completion={}/{}", - usage.getInteger("prompt_tokens"), - usage.getInteger("completion_tokens"), maxTokens); + // 协议差异解析: OpenAI choices[0].message.content vs Anthropic content[0].text + String content; + if (isAnthropic) { + JSONArray contentArr = respJson.getJSONArray("content"); + if (contentArr != null && !contentArr.isEmpty()) { + content = contentArr.getJSONObject(0).getString("text"); + } else { + log.error("[SswLLMClient] Anthropic 响应无 content: {}", respJson.toJSONString()); + return null; + } + JSONObject usage = respJson.getJSONObject("usage"); + if (usage != null) { + log.info("[SswLLMClient] usage: input={} output={}/{}", + usage.getInteger("input_tokens"), + usage.getInteger("output_tokens"), maxTokens); + } + } else { + content = respJson.getJSONArray("choices") + .getJSONObject(0) + .getJSONObject("message") + .getString("content"); + JSONObject usage = respJson.getJSONObject("usage"); + if (usage != null) { + log.info("[SswLLMClient] usage: prompt={} completion={}/{}", + usage.getInteger("prompt_tokens"), + usage.getInteger("completion_tokens"), maxTokens); + } } return content; @@ -124,6 +156,29 @@ public class SswLLMClient { } } + /** + * v0.17+ 转换 OpenAI chat 格式 → Anthropic messages 格式 + * + * OpenAI: {model, messages:[{role, content}], max_tokens} + * Anthropic: {model, system, messages:[{role, content}], max_tokens} + */ + private String toAnthropicBody(String model, String system, String user, int maxTokens) { + JSONObject oaBody = new JSONObject(); + oaBody.put("model", model); + oaBody.put("max_tokens", maxTokens); + + JSONArray messages = new JSONArray(); + JSONObject userMsg = new JSONObject(); + userMsg.put("role", "user"); + userMsg.put("content", user); + messages.add(userMsg); + oaBody.put("messages", messages); + + // 拆 system 出来 + oaBody.put("system", system); + return oaBody.toJSONString(); + } + /** * 加载 AI 配置: 优先级 * 1. 环境变量 SSW_LLM_* (推荐, 2026-07-06 BS-新62)