From 022ceb3cb99870f82d6795ab813f559eafa1db35 Mon Sep 17 00:00:00 2001 From: ssw Date: Mon, 6 Jul 2026 22:23:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(ssw-common):=20SswLLMClient=20=E5=8D=8F?= =?UTF-8?q?=E8=AE=AE=E8=87=AA=E9=80=82=E5=BA=94=20(BS-=E6=96=B063,=20v0.17?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 背景 回归 88b9cb2 (2026-07-03) 加 --table + AI 推理: - /ssw/ai/infer-category (取业务分类) - /ssw/ai/infer-fields (取字段组件类型) 走 SswLLMClient, 但仅支持 OpenAI /v1/chat/completions. minimax 实际: - token-plan-cn: OpenAI /v1/chat/completions ✅ - api.minimaxi.com/anthropic: Anthropic /v1/messages ❌ (SswLLMClient 走不通) 之前改 minimaxi 配置用 Anthropic URL, SswLLMClient 直接 404. ## 修复 (BS-新63) - SswLLMClient 自动检测协议 (normalized.contains('/anthropic')) - Anthropic 协议: - URL: baseUrl/v1/messages - Body: {model, system, messages:[{role, content}], max_tokens} - Response: content[0].text (vs OpenAI choices[0].message.content) - Usage: input_tokens/output_tokens (vs prompt_tokens/completion_tokens) - OpenAI 协议: 保持原逻辑 ## 验证 - mvn compile -pl ssw-common/ssw-common-biz -am → BUILD SUCCESS (4.771s) - 一键建表单 (form --ai / --table) 端点走通 --- .../java/ssw/common/util/SswLLMClient.java | 87 +++++++++++++++---- 1 file changed, 71 insertions(+), 16 deletions(-) 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)