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 4fac932..8b74d9e 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 @@ -20,8 +20,13 @@ import java.util.Map; * 背景: jnpf SendAiMessage 是 static 方法, 硬编码 maxTokens=1500, 不读 yml, * 也不对 LLM 输出做 format 修复. SSW 拦截层需要直接调 LLM API. * - * 数据源: 从 base_ai 表 (主库 dbLinkId=0) 读 default AI 配置 (f_baseUrl, f_credential, f_model) - * API 协议: OpenAI 兼容 (MiniMax / 通义千问 dashscope / OpenAI 都支持) + * 协议: OpenAI 兼容 chat/completions (base_ai 表里填 OpenAI 地址) + * 数据源优先级 (v0.16+ BS-新62, 2026-07-06): + * 1. 环境变量 SSW_LLM_BASE_URL / SSW_LLM_API_KEY / SSW_LLM_MODEL + * 2. base_ai 表 (主库 dbLinkId=0, f_enabled_mark=1, 最新创建) + * 3. 环境变量 ANTHROPIC_* (fallback 给 Claude Code 兼容场景) + * + * v0.16+ (BS-新61): minimaxi 切换, baseUrl 末尾带 / 时 normalize * * @author claude (2026-07-02) */ @@ -42,7 +47,7 @@ public class SswLLMClient { } /** - * 调 LLM API (chat/completions) + * 调 LLM API (OpenAI 兼容 /v1/chat/completions) * * @param systemPrompt 系统提示词 (如 jnpf 的 GEN_MODEL_QUETION) * @param userPrompt 用户提示词 (如 "--ai" 描述) @@ -55,7 +60,7 @@ public class SswLLMClient { String model = (String) aiConfig.get("model"); if (baseUrl == null || credential == null || model == null) { - log.error("SswLLMClient: AI 配置缺失 baseUrl={}, credential={}, model={}", + log.error("[SswLLMClient] AI 配置缺失 baseUrl={}, credential={}, model={}", baseUrl, credential, model); return null; } @@ -78,10 +83,9 @@ public class SswLLMClient { body.put("messages", messages); try { - // v0.15+ (2026-07-03): base_ai 的 baseUrl 是 API 前缀 (如 https://api.deepseek.com/v1), - // 实际 OpenAI-compatible endpoint 是 /v1/chat/completions. - // 之前的 HttpRequest.post(baseUrl) 直接 POST 到 /v1, minimaxi 返 301, deepseek 返 404. - String url = baseUrl.endsWith("/chat/completions") ? baseUrl : baseUrl + "/chat/completions"; + // v0.16+ (BS-新61): 末尾 / normalize, 避免 /v1//chat/completions 双斜杠 + 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); HttpResponse resp = HttpRequest.post(url) .header("Authorization", "Bearer " + credential) @@ -121,24 +125,59 @@ public class SswLLMClient { } /** - * 从 base_ai 表读 default AI 配置 (主库 dbLinkId=0) + * 加载 AI 配置: 优先级 + * 1. 环境变量 SSW_LLM_* (推荐, 2026-07-06 BS-新62) + * 2. base_ai 表 (fallback, 主库 dbLinkId=0) + * 3. 环境变量 ANTHROPIC_* (最后 fallback) + * + * 注意: 全部用 OpenAI 兼容 chat/completions 协议 (minimax/Anthropic/OpenAI/DeepSeek 都支持) */ @SuppressWarnings("unchecked") private Map loadDefaultAIConfig() { + // 1. SSW_LLM_* 环境变量 + String envBaseUrl = System.getenv("SSW_LLM_BASE_URL"); + String envCredential = System.getenv("SSW_LLM_API_KEY"); + String envModel = System.getenv("SSW_LLM_MODEL"); + if (envBaseUrl != null && envCredential != null && envModel != null) { + log.info("[SswLLMClient] AI 配置: 环境变量 SSW_LLM_* (model={})", envModel); + Map env = new HashMap<>(); + env.put("baseUrl", envBaseUrl); + env.put("credential", envCredential); + env.put("model", envModel); + return env; + } + + // 2. base_ai 表 try { String sql = "SELECT f_baseurl AS baseUrl, f_credential AS credential, f_model AS model " + "FROM base_ai WHERE f_enabled_mark = 1 " + "ORDER BY f_creator_time DESC LIMIT 1"; List> rows = jdbcTemplate.queryForList(sql); - if (rows == null || rows.isEmpty()) { - log.error("SswLLMClient: base_ai 表没有启用记录"); - return new HashMap<>(); + if (rows != null && !rows.isEmpty()) { + Map row = rows.get(0); + log.info("[SswLLMClient] AI 配置: base_ai 表 (model={})", row.get("model")); + return row; } - return rows.get(0); + log.warn("[SswLLMClient] base_ai 表没有启用记录"); } catch (Exception e) { - log.error("SswLLMClient: 读 base_ai 失败: {}", e.getMessage()); - return new HashMap<>(); + log.error("[SswLLMClient] 读 base_ai 失败: {}", e.getMessage()); } + + // 3. ANTHROPIC_* 环境变量 (last fallback) + String aBaseUrl = System.getenv("ANTHROPIC_BASE_URL"); + String aCred = System.getenv("ANTHROPIC_AUTH_TOKEN"); + String aModel = System.getenv("ANTHROPIC_MODEL"); + if (aBaseUrl != null && aCred != null && aModel != null) { + log.info("[SswLLMClient] AI 配置: 环境变量 ANTHROPIC_* (model={})", aModel); + Map env = new HashMap<>(); + env.put("baseUrl", aBaseUrl); + env.put("credential", aCred); + env.put("model", aModel); + return env; + } + + log.error("[SswLLMClient] AI 配置完全缺失 (SSW_LLM_*/base_ai/ANTHROPIC_* 都无)"); + return new HashMap<>(); } /** 兼容: 不传 systemPrompt 用默认 */ @@ -153,4 +192,4 @@ public class SswLLMClient { "fieldComponent (input/textarea/inputNumber/radio/select/switch/datePicker 等). " + "如果字段是 radio/select, 需要 fieldOptions 数组, 元素格式 {\"id\":\"1\",\"fullName\":\"选项名\"}. " + "请只返回 JSON, 不要包含其他文字或 markdown 包裹."; -} \ No newline at end of file +}