From 5bc09a3391322320aef326cb3e360843f17651f7 Mon Sep 17 00:00:00 2001 From: Youzini-afk <13153778771cx@gmail.com> Date: Tue, 24 Mar 2026 16:58:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20LLM=20=E8=B0=83=E7=94=A8=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=20response=5Fformat:=20json=5Fobject=20=E5=BC=BA?= =?UTF-8?q?=E5=88=B6=20JSON=20=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - callDedicatedOpenAICompatible 新增 jsonMode 参数 - callLLMForJSON 自动启用 json_object 模式 - 大幅降低 JSON 解析失败率 --- llm.js | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/llm.js b/llm.js index 53bcc5a..098a49c 100644 --- a/llm.js +++ b/llm.js @@ -27,26 +27,33 @@ function hasDedicatedLLMConfig(config = getMemoryLLMConfig()) { return Boolean(config.apiUrl && config.model); } -async function callDedicatedOpenAICompatible(messages, { signal } = {}) { +async function callDedicatedOpenAICompatible(messages, { signal, jsonMode = false } = {}) { const config = getMemoryLLMConfig(); if (!hasDedicatedLLMConfig(config)) { return await sendOpenAIRequest('quiet', messages, signal); } + const body = { + chat_completion_source: chat_completion_sources.OPENAI, + reverse_proxy: config.apiUrl, + proxy_password: config.apiKey || '', + model: config.model, + messages, + temperature: 0.2, + max_tokens: 1200, + max_completion_tokens: 1200, + stream: false, + }; + + // 强制 JSON 输出(OpenAI / 兼容 API 支持) + if (jsonMode) { + body.response_format = { type: 'json_object' }; + } + const response = await fetch('/api/backends/chat-completions/generate', { method: 'POST', headers: getRequestHeaders(), - body: JSON.stringify({ - chat_completion_source: chat_completion_sources.OPENAI, - reverse_proxy: config.apiUrl, - proxy_password: config.apiKey || '', - model: config.model, - messages, - temperature: 0.2, - max_tokens: 1200, - max_completion_tokens: 1200, - stream: false, - }), + body: JSON.stringify(body), signal, }); @@ -100,7 +107,7 @@ export async function callLLMForJSON({ systemPrompt, userPrompt, maxRetries = 2 for (let attempt = 0; attempt <= maxRetries; attempt++) { try { - const response = await callDedicatedOpenAICompatible(messages); + const response = await callDedicatedOpenAICompatible(messages, { jsonMode: true }); if (!response || typeof response !== 'string') { console.warn(`[ST-BME] LLM 返回空响应 (尝试 ${attempt + 1})`);