From 074e907cae5ccfe4e88322f640d8c53847026cc7 Mon Sep 17 00:00:00 2001 From: kicer Date: Wed, 29 Jul 2026 08:16:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=BC=93=E5=AD=98=E5=91=BD?= =?UTF-8?q?=E4=B8=AD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/ai/client.js | 11 +++++++++++ server/ai/prompt.js | 8 ++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/server/ai/client.js b/server/ai/client.js index 62220c7..8b44920 100644 --- a/server/ai/client.js +++ b/server/ai/client.js @@ -39,6 +39,17 @@ async function callDeepSeek(messages, userId, temperature = 0.1, jsonMode = true console.error('DeepSeek 返回异常:', JSON.stringify(data)); throw new Error('AI 返回数据异常'); } + // 缓存命中日志 + if (data.usage) { + var hit = data.usage.prompt_cache_hit_tokens || 0; + var miss = data.usage.prompt_cache_miss_tokens || 0; + var total = data.usage.prompt_tokens || 0; + var rate = total > 0 ? (hit / total * 100).toFixed(1) : 0; + console.log('缓存: hit=' + hit + ' miss=' + miss + ' total=' + total + ' 命中率=' + rate + '%'); + } + // 输入消息大小日志 + var inputChars = JSON.stringify(messages).length; + console.log('输入大小: ' + inputChars + ' 字符, ' + messages.length + ' 条消息'); return data.choices[0].message.content; } diff --git a/server/ai/prompt.js b/server/ai/prompt.js index e4b231a..71ac43b 100644 --- a/server/ai/prompt.js +++ b/server/ai/prompt.js @@ -2,7 +2,8 @@ function buildSystemPrompt(username, isAdmin) { const now = new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai', hour12: false }); - const role = '你是智能财务助手"小财"。当前服务器时间:' + now + '。结合对话历史理解用户意图,只返回JSON。'; + // 稳定前缀放前面(提升缓存命中率) + const role = '你是智能财务助手"小财"。结合对话历史理解用户意图,只返回JSON。'; const intent = [ '意图分类:', @@ -68,7 +69,10 @@ function buildSystemPrompt(username, isAdmin) { '- 无法确定物品名 → action="ask" 追问' ].join('\n'); - return [role, intent, permission, deleteRules, attachmentRules, prohibitionRules, modifyGuide, purchaseRules].join('\n'); + // 变动的放最后 + const timeInfo = '当前服务器时间:' + now; + + return [role, intent, permission, deleteRules, attachmentRules, prohibitionRules, modifyGuide, purchaseRules, timeInfo].join('\n'); } module.exports = { buildSystemPrompt };