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 客户端
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...');
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',
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${DEEPSEEK_API_KEY}` },
body: JSON.stringify({ model: 'deepseek-v4-flash', messages, temperature, stream: false })
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]) {