fix: response_format 自动降级 - 不支持时自动去掉重试

This commit is contained in:
Youzini-afk
2026-03-24 17:00:09 +08:00
parent 5bc09a3391
commit 963e4f3b7b

27
llm.js
View File

@@ -27,6 +27,9 @@ function hasDedicatedLLMConfig(config = getMemoryLLMConfig()) {
return Boolean(config.apiUrl && config.model);
}
// 自动检测:如果 API 不支持 response_format记住并跳过
let _jsonModeSupported = true;
async function callDedicatedOpenAICompatible(messages, { signal, jsonMode = false } = {}) {
const config = getMemoryLLMConfig();
if (!hasDedicatedLLMConfig(config)) {
@@ -45,8 +48,8 @@ async function callDedicatedOpenAICompatible(messages, { signal, jsonMode = fals
stream: false,
};
// 强制 JSON 输出(OpenAI / 兼容 API 支持)
if (jsonMode) {
// 强制 JSON 输出(仅当 API 支持
if (jsonMode && _jsonModeSupported) {
body.response_format = { type: 'json_object' };
}
@@ -57,6 +60,26 @@ async function callDedicatedOpenAICompatible(messages, { signal, jsonMode = fals
signal,
});
// 如果 400 且带了 response_format可能是 API 不支持,降级重试
if (!response.ok && response.status === 400 && jsonMode && _jsonModeSupported) {
console.warn('[ST-BME] API 不支持 response_format降级为普通模式');
_jsonModeSupported = false;
// 去掉 response_format 重试
delete body.response_format;
const retryResponse = await fetch('/api/backends/chat-completions/generate', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify(body),
signal,
});
return await _parseResponse(retryResponse);
}
return await _parseResponse(response);
}
async function _parseResponse(response) {
const responseText = await response.text().catch(() => '');
let data;