ds api请求时加上user_id

This commit is contained in:
2026-07-29 08:00:51 +08:00
parent e08c0cdff0
commit 416b173183
3 changed files with 15 additions and 9 deletions

View File

@@ -1,12 +1,18 @@
// DeepSeek API 客户端 // DeepSeek API 客户端
const DEEPSEEK_API_KEY = process.env.DEEPSEEK_API_KEY || ''; const DEEPSEEK_API_KEY = process.env.DEEPSEEK_API_KEY || '';
async function callDeepSeek(messages, temperature = 0.1) { function sanitizeUserId(username) {
return username.replace(/[^a-zA-Z0-9\-_]/g, '_').substring(0, 64) || 'anon';
}
async function callDeepSeek(messages, temperature = 0.1, userId = '') {
console.log('🤖 调用 DeepSeek...'); console.log('🤖 调用 DeepSeek...');
const res = await fetch('https://api.deepseek.com/chat/completions', { var body = { model: 'deepseek-v4-flash', messages: messages, temperature: temperature, stream: false };
if (userId) body.user_id = sanitizeUserId(userId);
var res = await fetch('https://api.deepseek.com/chat/completions', {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${DEEPSEEK_API_KEY}` }, headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + DEEPSEEK_API_KEY },
body: JSON.stringify({ model: 'deepseek-v4-flash', messages, temperature, stream: false }) body: JSON.stringify(body)
}); });
const data = await res.json(); const data = await res.json();
if (!data.choices || !data.choices[0]) { if (!data.choices || !data.choices[0]) {

View File

@@ -170,7 +170,7 @@ function handleAIResult(aiResult, roomId, username, originalText, attachments) {
if (aiResult.action === 'query') { if (aiResult.action === 'query') {
const purchaseData = db.prepare('SELECT item, amount, payment_method, status FROM purchases WHERE room_id = ? ORDER BY created_at DESC').all(roomId).map(p => p.item + ' ' + p.amount + ' ' + (p.payment_method||'') + ' ' + p.status).join('\n'); const purchaseData = db.prepare('SELECT item, amount, payment_method, status FROM purchases WHERE room_id = ? ORDER BY created_at DESC').all(roomId).map(p => p.item + ' ' + p.amount + ' ' + (p.payment_method||'') + ' ' + p.status).join('\n');
const summaryPrompt = '\u6839\u636e\u4ee5\u4e0b\u91c7\u8d2d\u8bb0\u5f55\uff0c\u7528\u81ea\u7136\u8bed\u8a00\u56de\u7b54\u7528\u6237\u67e5\u8be2"' + originalText + '"' + '\u3002\u91c7\u8d2d\u8bb0\u5f55\uff1a\n' + (purchaseData || '\u6682\u65e0\u8bb0\u5f55'); const summaryPrompt = '\u6839\u636e\u4ee5\u4e0b\u91c7\u8d2d\u8bb0\u5f55\uff0c\u7528\u81ea\u7136\u8bed\u8a00\u56de\u7b54\u7528\u6237\u67e5\u8be2"' + originalText + '"' + '\u3002\u91c7\u8d2d\u8bb0\u5f55\uff1a\n' + (purchaseData || '\u6682\u65e0\u8bb0\u5f55');
callDeepSeekForSummary(summaryPrompt).then(reply => { storeAndBroadcastText(roomId, '\u5c0f\u8d22', reply); addToHistory(roomId, 'assistant', reply); }); callDeepSeekForSummary(summaryPrompt, username).then(reply => { storeAndBroadcastText(roomId, '小财', reply); addToHistory(roomId, 'assistant', reply); });
return; return;
} }
@@ -181,13 +181,13 @@ function handleAIResult(aiResult, roomId, username, originalText, attachments) {
} }
} }
async function callDeepSeekForSummary(prompt) { async function callDeepSeekForSummary(prompt, userId) {
const { callDeepSeek } = require('./ai/client'); const { callDeepSeek } = require('./ai/client');
const messages = [ const messages = [
{ role: 'system', content: '\u4f60\u662f\u4e00\u4e2a\u8d22\u52a1\u52a9\u624b\uff0c\u8bf7\u6839\u636e\u91c7\u8d2d\u8bb0\u5f55\u751f\u6210\u7b80\u6d01\u56de\u590d\u3002' }, { role: 'system', content: '你是一个财务助手,请根据采购记录生成简洁回复。' },
{ role: 'user', content: prompt } { role: 'user', content: prompt }
]; ];
return callDeepSeek(messages, 0.3); return callDeepSeek(messages, 0.3, userId);
} }
module.exports = { module.exports = {

View File

@@ -63,7 +63,7 @@ async function analyzeWithDeepSeek(text, historyMessages, username, isAdmin, roo
...historyMessages.slice(-30), ...historyMessages.slice(-30),
{ role: 'user', content: `${username}: ${text}` } { role: 'user', content: `${username}: ${text}` }
]; ];
const content = await callDeepSeek(messages, 0.1); const content = await callDeepSeek(messages, 0.1, username);
console.log('🤖 AI 返回:', content); console.log('🤖 AI 返回:', content);
try { try {
const parsed = JSON.parse(content.replace(/```json|```/g, '').trim()); const parsed = JSON.parse(content.replace(/```json|```/g, '').trim());