修改缓存命中逻辑

This commit is contained in:
2026-07-29 08:16:57 +08:00
parent 0e17abd653
commit 074e907cae
2 changed files with 17 additions and 2 deletions

View File

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

View File

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