kongdeqiang
2026-10-12 7c687dd3c8fba9f7632f5e29d94a708e5f497abd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
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.custom.webui.util.ReasoningLeakFilter;
import com.custom.webui.util.ThinkingTagStripper;
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;
import java.util.Map;
 
/**
 * OpenAI 兼容协议实现。
 *
 * <p><b>为什么这一个类能覆盖几乎全部推理服务</b>:
 * {@code POST {baseUrl}/chat/completions} 已经成为事实标准,
 * vLLM、LM Studio、SGLang、Xinference、Ollama(/v1 端点)、
 * one-api/new-api 网关,以及 OpenAI / DeepSeek / Kimi / 通义 / GLM
 * 云端 API 都实现了它。换服务只需改 {@code baseUrl} 与 {@code model}。
 *
 * <p>与 {@link OllamaLlmService} 的关键差异:
 * <table border="1">
 *   <caption>协议差异</caption>
 *   <tr><th></th><th>Ollama 原生</th><th>OpenAI 兼容</th></tr>
 *   <tr><td>流式格式</td><td>NDJSON,每行一个 JSON</td><td>SSE,{@code data: {...}},以 {@code data: [DONE]} 结束</td></tr>
 *   <tr><td>增量位置</td><td>{@code message.content}</td><td>{@code choices[0].delta.content}</td></tr>
 *   <tr><td>上下文窗口参数</td><td>{@code options.num_ctx}</td><td>无对应项,只有 {@code max_tokens}(输出上限)</td></tr>
 *   <tr><td>思考内容</td><td>{@code message.thinking}</td><td>{@code delta.reasoning_content},或正文里的 {@code <think>} 标签</td></tr>
 * </table>
 *
 * <p><b>思考内容的两种形态都要处理</b>:实测 vLLM 上同一模型,
 * 流式时走 {@code delta.reasoning_content},非流式却把 {@code <think>}
 * 标签写进 {@code content}。因此这里两条路都做了剥离,
 * 否则整段推理会灌进聊天气泡并永久写进数据库。
 */
@Slf4j
@Service
@ConditionalOnProperty(name = "webui.llm.provider", havingValue = "openai-compat")
public class OpenAiCompatLlmService implements LlmService {
 
    private static final String DONE_MARKER = "[DONE]";
 
    private final LlmProperties properties;
    private final ObjectMapper objectMapper;
 
    public OpenAiCompatLlmService(LlmProperties properties, ObjectMapper objectMapper) {
        this.properties = properties;
        this.objectMapper = objectMapper;
        log.info("LLM 实现: OpenAI 兼容, baseUrl={}, model={}",
                properties.getOpenaiCompat().getBaseUrl(),
                properties.getOpenaiCompat().getModel());
    }
 
    // ============================================================
    // 流式
    // ============================================================
 
    @Override
    public void streamChat(List<LlmMessage> messages, LlmOptions options, TokenConsumer consumer) {
        HttpURLConnection conn = null;
        // 统计变量声明在 try 之外:流结束后要打日志,作用域需覆盖整个方法
        int totalLines = 0;
        int dataLines = 0;
        int parsedFrames = 0;
        int contentFrames = 0;
        // 用于「推理泄漏」检测:累积全部正文,结束时统一判定。
        // 为什么不在流中实时判断:启发式规则需要看完整文本才准
        //(单个分片可能只是 "Wait" 这样的普通词),过早拦截会误伤。
        // 代价是开启该功能时正文要等流结束才推给前端,因此默认关闭。
        StringBuilder leakProbe = new StringBuilder();
        boolean stripLeak = properties.getOpenaiCompat().isStripReasoningLeak();
        ThinkingTagStripper stripper = new ThinkingTagStripper(
                properties.getOpenaiCompat().isIncludeThinking()
                        ? (thinking) -> consumer.accept(thinking, true)
                        : null);
 
        try {
            conn = openConnection("/chat/completions",
                    properties.getOpenaiCompat().getTimeoutMillis());
            writeBody(conn, buildRequestBody(messages, options, true));
 
            int status = conn.getResponseCode();
            if (status != 200) {
                String err = readErrorBody(conn);
                log.error("OpenAI 兼容服务返回非 200: status={}, body={}", status, err);
                throw BusinessException.serverError(
                        "LLM 服务返回错误 (HTTP " + status + ")。地址: "
                                + properties.getOpenaiCompat().getBaseUrl()
                                + ",模型: " + resolveModel(options) + "。详情: " + err);
            }
 
            try (BufferedReader reader = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    totalLines++;
                    // SSE 帧格式:以 "data: " 开头,空行分隔
                    if (line.isEmpty() || !line.startsWith("data:")) {
                        continue;
                    }
                    dataLines++;
                    String payload = line.substring("data:".length()).trim();
                    if (payload.isEmpty()) {
                        continue;
                    }
                    if (DONE_MARKER.equals(payload)) {
                        log.debug("收到 [DONE],结束流");
                        break;
                    }
 
                    JsonNode node;
                    try {
                        node = objectMapper.readTree(payload);
                    } catch (IOException parseError) {
                        log.warn("无法解析流式分片: {}", abbreviate(payload));
                        continue;
                    }
 
                    parsedFrames++;
                    if (node.hasNonNull("error")) {
                        throw BusinessException.serverError(
                                "LLM 服务错误: " + node.get("error").toString());
                    }
 
                    JsonNode delta = node.path("choices").path(0).path("delta");
                    if (delta.isMissingNode()) {
                        continue;
                    }
 
                    // 形态一:独立的推理字段(vLLM / DeepSeek 等)
                    for (String field : new String[]{"reasoning_content", "reasoning"}) {
                        JsonNode reasoning = delta.get(field);
                        if (reasoning != null && !reasoning.asText().isEmpty()
                                && properties.getOpenaiCompat().isIncludeThinking()) {
                            consumer.accept(reasoning.asText(), true);
                        }
                    }
 
                    // 形态二:正文里夹带 <think> 标签(可能跨 chunk 被切开)
                    JsonNode content = delta.get("content");
                    if (content != null && !content.asText().isEmpty()) {
                        contentFrames++;
                        String visible = stripper.accept(content.asText());
                        if (!visible.isEmpty()) {
                            if (stripLeak) {
                                leakProbe.append(visible);
                            } else {
                                consumer.accept(visible, false);
                            }
                        }
                    }
                }
            }
 
            log.info("流式读取完成: 总行数={} data行={} 可解析帧={} 含content帧={}",
                    totalLines, dataLines, parsedFrames, contentFrames);
 
            // 流结束:把缓冲区剩余内容吐出来,避免标签前缀恰好结尾时丢字
            String tail = stripper.flush();
            if (!tail.isEmpty()) {
                if (stripLeak) {
                    leakProbe.append(tail);
                } else {
                    consumer.accept(tail, false);
                }
            }
 
            // 开启推理泄漏过滤时,在这里统一判定并输出
            if (stripLeak) {
                String full = leakProbe.toString();
                if (ReasoningLeakFilter.looksLikeReasoning(full)) {
                    log.warn("正文被判定为推理泄漏(模型把思考写进了 content),已丢弃 {} 字。"
                                    + "如需保留请关闭 webui.llm.openaiCompat.strip-reasoning-leak", full.length());
                } else if (!full.isEmpty()) {
                    consumer.accept(full, false);
                }
            }
 
        } catch (BusinessException e) {
            throw e;
        } catch (IOException e) {
            log.error("调用 OpenAI 兼容服务失败", e);
            throw BusinessException.serverError(
                    "无法连接 LLM 服务 " + properties.getOpenaiCompat().getBaseUrl()
                            + "。请确认该服务已启动。原因: " + e.getMessage());
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
    }
 
    // ============================================================
    // 非流式(会话标题等短任务)
    // ============================================================
 
    @Override
    public String complete(List<LlmMessage> messages, LlmOptions options) {
        HttpURLConnection conn = null;
        try {
            conn = openConnection("/chat/completions", 60000);
            writeBody(conn, buildRequestBody(messages, options, false));
 
            if (conn.getResponseCode() != 200) {
                log.warn("非流式调用失败, status={}", conn.getResponseCode());
                return null;
            }
            try (InputStream in = conn.getInputStream()) {
                JsonNode node = objectMapper.readTree(in);
                JsonNode content = node.path("choices").path(0).path("message").path("content");
                if (content.isMissingNode()) {
                    return null;
                }
                // 非流式响应里 reasoning_content 是独立字段,但 content 里
                // **可能仍夹带 <think> 标签**,先做标签剥离
                String cleaned = ThinkingTagStripper.stripAll(content.asText());
                // 再按配置决定是否过滤「模型把推理写进正文」的泄漏
                if (properties.getOpenaiCompat().isStripReasoningLeak()
                        && ReasoningLeakFilter.looksLikeReasoning(cleaned)) {
                    log.warn("非流式正文被判定为推理泄漏,已丢弃 {} 字", cleaned.length());
                    return "";
                }
                return cleaned;
            }
        } catch (Exception e) {
            // 标题生成属于锦上添花,失败不抛出,由调用方回退
            log.warn("非流式调用异常: {}", e.getMessage());
            return null;
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
    }
 
    @Override
    public String defaultModel() {
        return properties.getOpenaiCompat().getModel();
    }
 
    // ============================================================
    // 内部辅助
    // ============================================================
 
    private HttpURLConnection openConnection(String path, int timeoutMillis) throws IOException {
        String baseUrl = properties.getOpenaiCompat().getBaseUrl();
        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", "text/event-stream, application/json");
 
        String apiKey = properties.getOpenaiCompat().getApiKey();
        if (apiKey != null && !apiKey.isBlank()) {
            conn.setRequestProperty("Authorization", "Bearer " + apiKey);
        }
        // 部分网关需要额外头(如 X-Api-Key),从配置透传
        for (Map.Entry<String, String> header : properties.getOpenaiCompat().getExtraHeaders().entrySet()) {
            conn.setRequestProperty(header.getKey(), header.getValue());
        }
        return conn;
    }
 
    private String buildRequestBody(List<LlmMessage> 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());
        }
 
        // 采样参数名与 Ollama 一致(temperature / top_p),可直接复用
        Double temperature = options.temperature() != null
                ? options.temperature() : properties.getDefaults().getTemperature();
        Double topP = options.topP() != null
                ? options.topP() : properties.getDefaults().getTopP();
        if (temperature != null) {
            root.put("temperature", temperature);
        }
        if (topP != null) {
            root.put("top_p", topP);
        }
 
        // 输出长度上限。注意这与 Ollama 的 num_ctx 语义不同:
        // num_ctx 是「输入+输出」总窗口,max_tokens 只是输出上限。
        // OpenAI 协议没有暴露上下文窗口的字段,所以这里不能用 numCtx 顶替。
        Integer maxTokens = properties.getOpenaiCompat().getMaxTokens();
        if (maxTokens != null && maxTokens > 0) {
            root.put("max_tokens", maxTokens);
        }
 
        return objectMapper.writeValueAsString(root);
    }
 
    private String resolveModel(LlmOptions options) {
        if (options != null && options.modelName() != null && !options.modelName().isBlank()) {
            return options.modelName().trim();
        }
        return properties.getOpenaiCompat().getModel();
    }
 
    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;
    }
}