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={}, body={}", status, err);
throw BusinessException.serverError(
"LLM 服务返回错误 (HTTP " + status + ")。请确认模型 "
+ resolveModel(options) + " 已通过 `ollama pull` 下载。详情: " + err);
}
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();
}
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;
}
}