码桶

发现社区成员的开源项目

奶狗 /

ng-webot

公开
main
ng-webot/lib/model-stats.js
model-stats.js1 KB
// 模型调用统计写入
// 每次 chat / vision / transcribe 调用都记一条,用于「系统统计」页的模型调用视图。
// 写入失败不影响主流程。
const db = require('./db');

async function logModelCall(rec) {
  try {
    await db.exec(
      `INSERT INTO model_calls
        (bot_id, peer_id, model, mode, prompt_tokens, completion_tokens, total_tokens, ttft_ms, latency_ms, success, error_msg, created_at)
       VALUES (?,?,?,?,?,?,?,?,?,?,?,?)`,
      [
        rec.bot_id != null ? rec.bot_id : null,
        rec.peer_id || null,
        rec.model || '',
        rec.mode || 'chat',
        rec.prompt_tokens || 0,
        rec.completion_tokens || 0,
        rec.total_tokens || 0,
        rec.ttft_ms || 0,
        rec.latency_ms || 0,
        rec.success ? 1 : 0,
        rec.error_msg || '',
        Math.floor(Date.now() / 1000),
      ]
    );
  } catch (e) {
    console.error('[model-stats] 写入失败:', e && e.message);
  }
}

module.exports = { logModelCall };