feat: LLM 调用添加 response_format: json_object 强制 JSON 输出

- callDedicatedOpenAICompatible 新增 jsonMode 参数
- callLLMForJSON 自动启用 json_object 模式
- 大幅降低 JSON 解析失败率
This commit is contained in:
Youzini-afk
2026-03-24 16:58:42 +08:00
parent 0ebfbb6086
commit 5bc09a3391

33
llm.js
View File

@@ -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})`);