Files
ai-xiaocai/server/ai/client.js
2026-07-29 08:00:51 +08:00

26 lines
1.0 KiB
JavaScript

// DeepSeek API 客户端
const DEEPSEEK_API_KEY = process.env.DEEPSEEK_API_KEY || '';
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...');
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',
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + DEEPSEEK_API_KEY },
body: JSON.stringify(body)
});
const data = await res.json();
if (!data.choices || !data.choices[0]) {
console.error('🤖 DeepSeek 返回异常:', JSON.stringify(data));
throw new Error('DeepSeek API 返回异常: ' + (data.error?.message || JSON.stringify(data)));
}
return data.choices[0].message.content;
}
module.exports = { callDeepSeek };