From ce8b05678b6977d0c9f79bbd056356f0487d03b3 Mon Sep 17 00:00:00 2001 From: kicer Date: Mon, 21 Sep 2026 10:28:52 +0800 Subject: [PATCH] =?UTF-8?q?think=E6=A0=87=E7=AD=BE=E5=BD=92=E5=85=A5?= =?UTF-8?q?=E6=80=9D=E8=80=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/server/llm.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/app/server/llm.js b/app/server/llm.js index 6e8da2d..55faa01 100644 --- a/app/server/llm.js +++ b/app/server/llm.js @@ -283,6 +283,51 @@ function deepMergeInto(dst, src) { return dst; } +// 有些小模型(如 MiniCPM)不走 reasoning 字段,而是把思考过程混进 content 开头: +// - 成对前缀: 思考答复(可连多个,也兼容 ); +// - 仅结束标记:思考答复(无开标签,第一个 之前全是思考)。 +// 只在「正文开头」识别思考块:命中才剥离,正文中段出现的字面 / +//(代码示例、讲解标签等)一律不动,避免误吞答案。 +function splitInlineThinking(raw) { + const text0 = String(raw || ''); + if (!/<\/?think/i.test(text0)) return { content: text0, reasoning: '' }; + + let text = text0; + let think = ''; + let matched = false; + + // A) 开头成对块:^\s*…,非贪婪;连续多个块一并剥离,中段的不碰 + const paired = /^\s*([\s\S]*?)<\/think(?:ing)>/i; + let m; + while ((m = paired.exec(text))) { + think += m[1]; + text = text.slice(m[0].length); + matched = true; + } + + // B) 无成对块时,按 MiniCPM 风格处理孤立的结束标记: + // 只有第一个 之前算思考(indexOf 只吃前缀,绝不回退到最后, + // 否则正文里随便一个 会把前面的全部答案吸进思考面板); + // 且要求结束标记之前没有任何开标签——真正的「仅结束标记」风格本就无开标签, + // 正文里合法写出的 …(代码示例/讲解标签)因此得以放行。 + if (!matched) { + const lc = text.toLowerCase(); + const close = lc.indexOf(' close)) { + const gt = text.indexOf('>', close); + think += text.slice(0, close); + text = text.slice(gt === -1 ? text.length : gt + 1); + matched = true; + } + } + + // 没在开头命中思考块 → 原样返回,保护正文里的字面标签 + if (!matched) return { content: text0, reasoning: '' }; + + return { content: text.trim(), reasoning: think.trim() }; +} + // 调模型一次。onText(textDelta) 回调流式文本。 // 返回 { content, tool_calls: [{id,name,arguments(string)}], finish_reason, usage, aborted } // 可选回调 opts.onToolCallStart({id,name}):某 tool_call 首次凑齐 id+name 时触发一次; @@ -407,6 +452,12 @@ async function callModel(cfg, modelId, messages, tools, options) { } if (opts.shouldCancel && opts.shouldCancel()) aborted = true; + // 归一化:把混进 content 的思考标签剥离出来(流式阶段无法回溯,故在整轮收尾统一处理; + // engine 落库/发 assistant_message 用的是这里的返回值,前端收尾会用干净正文覆盖流式文本) + const split = splitInlineThinking(content); + content = split.content; + if (split.reasoning) reasoning = reasoning ? split.reasoning + '\n' + reasoning : split.reasoning; + return { content, reasoning, tool_calls: Array.from(toolCalls.values()), finish_reason: finishReason, usage, aborted, sseFrames }; }