package com.custom.webui.service.impl; import com.custom.webui.config.props.LlmProperties; import com.custom.webui.exception.BusinessException; import com.custom.webui.service.LlmService; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Service; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.List; /** * Ollama LLM 实现,对接 {@code POST /api/chat}。 * *

为什么用 HttpURLConnection 而不是 RestTemplate? * Ollama 的流式响应是 NDJSON(每行一个 JSON 对象),RestTemplate 没有开箱即用的 * 「逐行消费」能力,需要写自定义 ResponseExtractor 反而更绕。 * 直接用 {@link HttpURLConnection} + {@link BufferedReader#readLine()} 最直观, * 也避免引入 WebClient 这套响应式依赖(会显著增大打包体积)。 * *

请求体示例: *

 * {
 *   "model": "qwen2.5:7b",
 *   "messages": [{"role":"user","content":"你好"}],
 *   "stream": true,
 *   "options": {"temperature":0.7,"top_p":0.9,"num_ctx":8192}
 * }
 * 
* *

响应体每行形如: *

 * {"model":"qwen2.5:7b","message":{"role":"assistant","content":"你"},"done":false}
 * {"model":"qwen2.5:7b","message":{"role":"assistant","content":""},"done":true,...}
 * 
* *

前置条件:Ollama 必须监听 0.0.0.0 才能被其他进程/机器访问 —— * 启动命令 {@code OLLAMA_HOST=0.0.0.0 ollama serve}。 */ @Slf4j @Service @ConditionalOnProperty(name = "webui.llm.provider", havingValue = "ollama", matchIfMissing = true) public class OllamaLlmService implements LlmService { private final LlmProperties properties; private final ObjectMapper objectMapper; public OllamaLlmService(LlmProperties properties, ObjectMapper objectMapper) { this.properties = properties; this.objectMapper = objectMapper; log.info("LLM 实现: Ollama, baseUrl={}, defaultModel={}", properties.getOllama().getBaseUrl(), properties.getOllama().getDefaultModel()); } @Override public void streamChat(List messages, LlmOptions options, TokenConsumer consumer) { HttpURLConnection conn = null; try { conn = openConnection("/api/chat", properties.getOllama().getTimeoutMillis()); String body = buildRequestBody(messages, options, true); writeBody(conn, body); int status = conn.getResponseCode(); if (status != 200) { String err = readErrorBody(conn); log.error("Ollama 返回非 200: status={}, url={}/api/chat, body={}", status, properties.getOllama().getBaseUrl(), err); throw BusinessException.serverError(describeFailure(status, err, resolveModel(options))); } try (BufferedReader reader = new BufferedReader( new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) { String line; while ((line = reader.readLine()) != null) { if (line.isBlank()) { continue; } JsonNode node; try { node = objectMapper.readTree(line); } catch (IOException parseError) { // 单行解析失败不应中断整个流,跳过即可 log.warn("无法解析 Ollama 响应行: {}", abbreviate(line)); continue; } // 错误字段优先:Ollama 可能在中途返回 {"error": "..."} if (node.hasNonNull("error")) { throw BusinessException.serverError("Ollama 错误: " + node.get("error").asText()); } JsonNode messageNode = node.get("message"); if (messageNode != null) { // 思考内容(qwen3 / deepseek-r1 等 reasoning 模型) JsonNode thinkingNode = messageNode.get("thinking"); if (thinkingNode != null && !thinkingNode.asText().isEmpty()) { if (properties.getOllama().isIncludeThinking()) { consumer.accept(thinkingNode.asText(), true); } } JsonNode contentNode = messageNode.get("content"); if (contentNode != null && !contentNode.asText().isEmpty()) { consumer.accept(contentNode.asText(), false); } } if (node.path("done").asBoolean(false)) { break; } } } } catch (BusinessException e) { throw e; } catch (IOException e) { log.error("调用 Ollama 失败", e); throw BusinessException.serverError( "无法连接 LLM 服务 " + properties.getOllama().getBaseUrl() + ",请确认 Ollama 已启动(OLLAMA_HOST=0.0.0.0 ollama serve)。原因: " + e.getMessage()); } finally { if (conn != null) { conn.disconnect(); } } } @Override public String complete(List messages, LlmOptions options) { HttpURLConnection conn = null; try { conn = openConnection("/api/chat", 30000); writeBody(conn, buildRequestBody(messages, options, false)); if (conn.getResponseCode() != 200) { log.warn("Ollama 非流式调用失败, status={}", conn.getResponseCode()); return null; } try (InputStream in = conn.getInputStream()) { JsonNode node = objectMapper.readTree(in); JsonNode contentNode = node.path("message").path("content"); return contentNode.isMissingNode() ? null : contentNode.asText(); } } catch (Exception e) { // 标题生成属于锦上添花,失败不抛出,由调用方回退到截断标题 log.warn("Ollama 非流式调用异常: {}", e.getMessage()); return null; } finally { if (conn != null) { conn.disconnect(); } } } @Override public String defaultModel() { return properties.getOllama().getDefaultModel(); } // ==================== 内部辅助 ==================== private HttpURLConnection openConnection(String path, int timeoutMillis) throws IOException { String baseUrl = properties.getOllama().getBaseUrl(); // 去掉末尾斜杠,避免拼出 //api/chat if (baseUrl.endsWith("/")) { baseUrl = baseUrl.substring(0, baseUrl.length() - 1); } URI uri = URI.create(baseUrl + path); HttpURLConnection conn = (HttpURLConnection) uri.toURL().openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setConnectTimeout(5000); conn.setReadTimeout(timeoutMillis); conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); conn.setRequestProperty("Accept", "application/x-ndjson"); return conn; } /** 组装请求体 JSON */ private String buildRequestBody(List messages, LlmOptions options, boolean stream) throws IOException { ObjectNode root = objectMapper.createObjectNode(); root.put("model", resolveModel(options)); root.put("stream", stream); ArrayNode messageArray = root.putArray("messages"); for (LlmMessage m : messages) { ObjectNode item = messageArray.addObject(); item.put("role", m.role()); item.put("content", m.content()); } ObjectNode optionsNode = root.putObject("options"); Double temperature = options.temperature() != null ? options.temperature() : properties.getDefaults().getTemperature(); Double topP = options.topP() != null ? options.topP() : properties.getDefaults().getTopP(); Integer numCtx = options.numCtx() != null ? options.numCtx() : properties.getDefaults().getNumCtx(); if (temperature != null) { optionsNode.put("temperature", temperature); } if (topP != null) { optionsNode.put("top_p", topP); } if (numCtx != null) { optionsNode.put("num_ctx", numCtx); } return objectMapper.writeValueAsString(root); } /** 模型名解析优先级:会话参数 > 全局配置 */ private String resolveModel(LlmOptions options) { if (options != null && options.modelName() != null && !options.modelName().isBlank()) { return options.modelName().trim(); } return properties.getOllama().getDefaultModel(); } /** * 拼装「能定位问题」的失败信息。 * *

为什么要专门写这个方法:早先这里只有一句 * 「请确认模型 X 已通过 {@code ollama pull} 下载」。问题在于, * 触发非 200 的原因里,**模型没下载只占很小一部分**: *

* 这几种情况用户都会看到「模型没下载」,然后花大量时间去 * {@code ollama pull},而真实原因与模型毫无关系。 * *

所以这里把**实际请求地址**和**协议是否匹配的判断**一并给出。 * 尤其 404:它几乎从不意味着「模型没下载」,因为模型不存在时 * Ollama 返回的是 200 + 流内 {@code {"error": "model ... not found"}}。 */ private String describeFailure(int status, String err, String model) { String baseUrl = properties.getOllama().getBaseUrl(); StringBuilder sb = new StringBuilder(256); sb.append("LLM 服务返回错误 (HTTP ").append(status).append(")。") .append("请求地址: ").append(baseUrl).append("/api/chat") .append(",模型: ").append(model).append("。"); if (status == 404) { sb.append("404 基本可以断定**不是**模型没下载 —— Ollama 的模型不存在时返回的是 HTTP 200 " + "和流内的 model not found 消息。404 说明这个地址上跑的不是 Ollama:") .append("它的原生接口是 /api/chat,而 OpenAI 兼容服务(vLLM / LM Studio / SGLang / one-api 等)") .append("只有 /v1/chat/completions。若你用的是后者,请把 webui.llm.provider 改为 openai-compat,") .append("并把 webui.llm.openaiCompat.base-url 配成以 /v1 结尾的地址。"); } else if (status == 401 || status == 403) { sb.append("这通常是网关或鉴权层拒绝,请检查 webui.llm.ollama.base-url 是否指向了需要 API Key 的服务。"); } else { sb.append("请确认 Ollama 已启动,且模型已下载(`ollama pull ").append(model).append("`)。"); } sb.append("详情: ").append(sanitize(err)); return sb.toString(); } /** * 清洗错误响应体。 * *

踩过的坑:某些服务(实测那个 node 写的转发器)返回的 404 文本 * 后面跟着几十个 NUL 字节,原样塞进 JSON 之后前端提示里显示成一串方块, * 用户完全看不出内容。这里把控制字符去掉并限长。 */ private String sanitize(String body) { if (body == null || body.isBlank()) { return "(无响应体)"; } String cleaned = body.replaceAll("[\\p{Cntrl}&&[^\r\n\t]]", "").trim(); if (cleaned.isEmpty()) { return "(响应体只有控制字符,内容不可读)"; } return abbreviate(cleaned); } private void writeBody(HttpURLConnection conn, String body) throws IOException { try (var out = conn.getOutputStream()) { out.write(body.getBytes(StandardCharsets.UTF_8)); out.flush(); } } /** 读取错误响应体,供日志与前端提示使用 */ private String readErrorBody(HttpURLConnection conn) { try (InputStream err = conn.getErrorStream()) { if (err == null) { return "(无响应体)"; } return new String(err.readAllBytes(), StandardCharsets.UTF_8); } catch (IOException e) { return "(读取错误响应失败)"; } } private String abbreviate(String s) { return s.length() > 200 ? s.substring(0, 200) + "..." : s; } }