Update server.js via upload script - 2026-10-15 10:55:48
This commit is contained in:
parent
1bca2fb095
commit
a49198f9c8
188
server.js
188
server.js
|
|
@ -95,20 +95,66 @@ const AI_CONFIG_FILE = path.join(__dirname, 'ai-config.json');
|
||||||
// (e.g. testing against a mock server); defaults to the real DeepSeek API.
|
// (e.g. testing against a mock server); defaults to the real DeepSeek API.
|
||||||
const DEEPSEEK_API_BASE = process.env.DEEPSEEK_API_BASE || 'https://api.deepseek.com';
|
const DEEPSEEK_API_BASE = process.env.DEEPSEEK_API_BASE || 'https://api.deepseek.com';
|
||||||
|
|
||||||
|
// 备用服务商:阿里云百炼(通义千问),OpenAI 兼容接口。DeepSeek 繁忙/故障时自动切换过去。
|
||||||
|
// 阿里云的 Key 分地域:中国大陆(北京)和国际(新加坡)的 Key 不能混用。
|
||||||
|
const ALIYUN_BASES = {
|
||||||
|
cn: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
|
||||||
|
intl: 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1',
|
||||||
|
};
|
||||||
|
const ALIYUN_DEFAULT_MODEL = 'qwen-plus';
|
||||||
|
|
||||||
|
function readAiConfigRaw() {
|
||||||
|
try { return JSON.parse(fs.readFileSync(AI_CONFIG_FILE, 'utf8')) || {}; } catch (e) { return {}; }
|
||||||
|
}
|
||||||
|
function writeAiConfigRaw(raw) {
|
||||||
|
fs.writeFileSync(AI_CONFIG_FILE, JSON.stringify(raw, null, 2), 'utf8');
|
||||||
|
}
|
||||||
function loadAiConfig() {
|
function loadAiConfig() {
|
||||||
try {
|
const raw = readAiConfigRaw();
|
||||||
const raw = JSON.parse(fs.readFileSync(AI_CONFIG_FILE, 'utf8'));
|
return {
|
||||||
return { apiKey: decrypt(raw.apiKeyEnc || '') };
|
apiKey: raw.apiKeyEnc ? decrypt(raw.apiKeyEnc) : '',
|
||||||
} catch (e) {
|
backup: {
|
||||||
return { apiKey: '' };
|
apiKey: raw.backupKeyEnc ? decrypt(raw.backupKeyEnc) : '',
|
||||||
}
|
model: raw.backupModel || ALIYUN_DEFAULT_MODEL,
|
||||||
|
region: raw.backupRegion === 'intl' ? 'intl' : 'cn',
|
||||||
|
},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
function saveAiConfig(apiKey) {
|
function saveAiConfig(apiKey) {
|
||||||
fs.writeFileSync(AI_CONFIG_FILE, JSON.stringify({ apiKeyEnc: encrypt(apiKey) }, null, 2), 'utf8');
|
const raw = readAiConfigRaw();
|
||||||
|
raw.apiKeyEnc = encrypt(apiKey);
|
||||||
|
writeAiConfigRaw(raw);
|
||||||
|
}
|
||||||
|
function aliyunProvider(backup) {
|
||||||
|
return {
|
||||||
|
name: '阿里云',
|
||||||
|
base: ALIYUN_BASES[backup.region] || ALIYUN_BASES.cn,
|
||||||
|
apiKey: backup.apiKey,
|
||||||
|
model: backup.model || ALIYUN_DEFAULT_MODEL,
|
||||||
|
// 通义千问非思考模式的输出上限一般是 8K~16K,超了会直接报 400,所以先封顶到 16384,
|
||||||
|
// 如果还是报 max_tokens 相关的 400,会自动降到 8192 再试一次
|
||||||
|
maxTokensCap: 16384,
|
||||||
|
extraBody: { enable_thinking: false },
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async function callDeepSeek(apiKey, messages, opts) {
|
async function callDeepSeek(apiKey, messages, opts) {
|
||||||
const { maxTokens = 600, temperature = 0.7, model = 'deepseek-v4-flash', timeoutMs = 120000 } = opts || {};
|
return callLLM({
|
||||||
|
name: 'DeepSeek', base: DEEPSEEK_API_BASE, apiKey,
|
||||||
|
model: (opts && opts.model) || 'deepseek-v4-flash',
|
||||||
|
// deepseek-v4-flash 默认开启thinking mode,复杂输入会把整个max_tokens预算都花在推理链上,
|
||||||
|
// content留空、finish_reason变成length(就是"AI返回空响应"这个报错的根因)。这里都是纯
|
||||||
|
// 结构化提取/翻译任务,不需要推理,显式关掉thinking让token预算全部用来输出实际内容。
|
||||||
|
extraBody: { thinking: { type: 'disabled' } },
|
||||||
|
}, messages, opts);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function callLLM(provider, messages, opts) {
|
||||||
|
const { temperature = 0.7, timeoutMs = 120000 } = opts || {};
|
||||||
|
let maxTokens = (opts && opts.maxTokens) || 600;
|
||||||
|
if (provider.maxTokensCap) maxTokens = Math.min(maxTokens, provider.maxTokensCap);
|
||||||
|
const model = provider.model;
|
||||||
|
const label = provider.name;
|
||||||
// 超时保护:没有这个的话,DeepSeek 一旦卡住或输出失控(一直生成到 max_tokens 上限),请求会无限期挂着,
|
// 超时保护:没有这个的话,DeepSeek 一旦卡住或输出失控(一直生成到 max_tokens 上限),请求会无限期挂着,
|
||||||
// 前端就一直转圈。超时后直接报错,用户可以重试。
|
// 前端就一直转圈。超时后直接报错,用户可以重试。
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
|
|
@ -116,64 +162,109 @@ async function callDeepSeek(apiKey, messages, opts) {
|
||||||
const startedAt = Date.now();
|
const startedAt = Date.now();
|
||||||
let resp;
|
let resp;
|
||||||
try {
|
try {
|
||||||
resp = await fetch(`${DEEPSEEK_API_BASE}/chat/completions`, {
|
resp = await fetch(`${provider.base}/chat/completions`, {
|
||||||
signal: controller.signal,
|
signal: controller.signal,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
Authorization: `Bearer ${apiKey}`,
|
Authorization: `Bearer ${provider.apiKey}`,
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
model,
|
model,
|
||||||
messages,
|
messages,
|
||||||
temperature,
|
temperature,
|
||||||
max_tokens: maxTokens,
|
max_tokens: maxTokens,
|
||||||
// deepseek-v4-flash 默认开启thinking mode,复杂输入会把整个max_tokens预算都花在推理链上,
|
...(provider.extraBody || {}),
|
||||||
// content留空、finish_reason变成length(就是"AI返回空响应"这个报错的根因)。这里都是纯
|
|
||||||
// 结构化提取/翻译任务,不需要推理,显式关掉thinking让token预算全部用来输出实际内容。
|
|
||||||
thinking: { type: 'disabled' },
|
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
if (e.name === 'AbortError') throw new Error(`DeepSeek 超过 ${Math.round(timeoutMs / 1000)} 秒没有返回结果,请重试`);
|
if (e.name === 'AbortError') throw new Error(`${label} 超过 ${Math.round(timeoutMs / 1000)} 秒没有返回结果,请重试`);
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
if (!resp.ok) {
|
if (!resp.ok) {
|
||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
const errText = await resp.text().catch(() => '');
|
const errText = await resp.text().catch(() => '');
|
||||||
throw new Error(`DeepSeek API error ${resp.status}: ${errText.slice(0, 200)}`);
|
const err = new Error(`${label} API error ${resp.status}: ${errText.slice(0, 200)}`);
|
||||||
|
err.status = resp.status;
|
||||||
|
throw err;
|
||||||
}
|
}
|
||||||
let data;
|
let data;
|
||||||
try { data = await resp.json(); }
|
try { data = await resp.json(); }
|
||||||
catch (e) {
|
catch (e) {
|
||||||
if (e.name === 'AbortError') throw new Error(`DeepSeek 超过 ${Math.round(timeoutMs / 1000)} 秒没有返回结果,请重试`);
|
if (e.name === 'AbortError') throw new Error(`${label} 超过 ${Math.round(timeoutMs / 1000)} 秒没有返回结果,请重试`);
|
||||||
throw e;
|
throw e;
|
||||||
} finally { clearTimeout(timer); }
|
} finally { clearTimeout(timer); }
|
||||||
const choice = data.choices && data.choices[0];
|
const choice = data.choices && data.choices[0];
|
||||||
// 记录耗时和 token 用量,以后再遇到"很慢"可以直接在日志里看到原因
|
// 记录耗时和 token 用量,以后再遇到"很慢"可以直接在日志里看到原因
|
||||||
console.log(`[DeepSeek] ${Math.round((Date.now() - startedAt) / 1000)}s, finish=${choice && choice.finish_reason}, usage=${JSON.stringify(data.usage || {})}`);
|
console.log(`[${label}] ${model} ${Math.round((Date.now() - startedAt) / 1000)}s, finish=${choice && choice.finish_reason}, usage=${JSON.stringify(data.usage || {})}`);
|
||||||
const content = choice && choice.message && choice.message.content;
|
const content = choice && choice.message && choice.message.content;
|
||||||
if (!content) {
|
if (!content) {
|
||||||
const reason = choice && choice.finish_reason ? `(finish_reason: ${choice.finish_reason})` : '';
|
const reason = choice && choice.finish_reason ? `(finish_reason: ${choice.finish_reason})` : '';
|
||||||
throw new Error(`DeepSeek API returned an empty response${reason}`);
|
throw new Error(`${label} API returned an empty response${reason}`);
|
||||||
}
|
}
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 空响应这种问题偶发性比较大(有时候纯粹是API抽风),重试一次往往就好了;
|
// 可以重试的错误:
|
||||||
// 其他类型的错误(比如key不对、额度用完)重试也没用,直接抛出不浪费时间
|
// - 空响应(偶发,重试一次往往就好)
|
||||||
async function callDeepSeekWithRetry(apiKey, messages, opts, retries = 1) {
|
// - 服务器繁忙/临时故障:429 限流、500/502/503/504(DeepSeek 高峰期常见 "Service is too busy")
|
||||||
let lastErr;
|
// - 网络瞬断(fetch failed / ECONNRESET)
|
||||||
for (let attempt = 0; attempt <= retries; attempt++) {
|
// 其他错误(key不对、额度用完 401/402、超时)重试没用,直接抛出。
|
||||||
|
const LLM_BUSY_STATUS = new Set([429, 500, 502, 503, 504]);
|
||||||
|
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
||||||
|
const isBusyError = (e) => LLM_BUSY_STATUS.has(e.status) || /fetch failed|ECONNRESET|ETIMEDOUT|socket hang up/i.test(e.message);
|
||||||
|
|
||||||
|
// 对单个服务商做"繁忙重试 + 空响应重试"
|
||||||
|
async function callWithRetries(provider, messages, opts, backoffs, emptyRetries) {
|
||||||
|
let busyTries = 0, emptyTries = 0, shrunk = false;
|
||||||
|
for (;;) {
|
||||||
try {
|
try {
|
||||||
return await callDeepSeek(apiKey, messages, opts);
|
return await callLLM(provider, messages, opts);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
lastErr = e;
|
if (isBusyError(e)) {
|
||||||
if (!/empty response/i.test(e.message) || attempt === retries) throw e;
|
if (busyTries >= backoffs.length) { e.busyExhausted = true; e.busyTries = busyTries; throw e; }
|
||||||
|
const wait = backoffs[busyTries++];
|
||||||
|
console.warn(`[${provider.name}] ${e.status || 'network'} 繁忙,${wait / 1000}s 后第 ${busyTries} 次重试`);
|
||||||
|
await sleep(wait);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// 阿里云:max_tokens 超过模型上限会报 400,降到 8192 再试一次
|
||||||
|
if (e.status === 400 && !shrunk && provider.maxTokensCap && /max_tokens|max_output|maximum/i.test(e.message)) {
|
||||||
|
shrunk = true;
|
||||||
|
provider = { ...provider, maxTokensCap: 8192 };
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (/empty response/i.test(e.message) && emptyTries < emptyRetries) { emptyTries++; continue; }
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 主力 DeepSeek,失败(繁忙/网络问题)时自动切到备用的阿里云。
|
||||||
|
// 名字保留 callDeepSeekWithRetry,所有调用方(目录导入、词汇翻译、网站翻译)都不用改。
|
||||||
|
async function callDeepSeekWithRetry(apiKey, messages, opts, retries = 1) {
|
||||||
|
const { backup } = loadAiConfig();
|
||||||
|
const hasBackup = !!backup.apiKey;
|
||||||
|
if (!apiKey && hasBackup) return callWithRetries(aliyunProvider(backup), messages, opts, [3000, 8000], retries);
|
||||||
|
const primary = { name: 'DeepSeek', base: DEEPSEEK_API_BASE, apiKey, model: (opts && opts.model) || 'deepseek-v4-flash', extraBody: { thinking: { type: 'disabled' } } };
|
||||||
|
try {
|
||||||
|
// 有备用时只重试一次就切换,不让用户干等;没备用时多重试几次
|
||||||
|
return await callWithRetries(primary, messages, opts, hasBackup ? [3000] : [3000, 8000, 15000], retries);
|
||||||
|
} catch (e) {
|
||||||
|
if (!(e.busyExhausted || (hasBackup && /empty response/i.test(e.message)))) throw e;
|
||||||
|
if (!hasBackup) {
|
||||||
|
const fe = new Error(`DeepSeek 服务器繁忙(${e.status || '网络错误'}),已自动重试 ${e.busyTries} 次仍失败。这是 DeepSeek 那边高峰期过载,请过几分钟再试;也可以在"设置"里配置备用的阿里云。`);
|
||||||
|
fe.status = e.status;
|
||||||
|
throw fe;
|
||||||
|
}
|
||||||
|
console.warn(`[AI] DeepSeek 不可用(${e.message.slice(0, 80)}),切换到阿里云 ${backup.model}`);
|
||||||
|
try {
|
||||||
|
return await callWithRetries(aliyunProvider(backup), messages, opts, [3000, 8000], retries);
|
||||||
|
} catch (e2) {
|
||||||
|
throw new Error(`DeepSeek 繁忙,备用的阿里云也失败了:${e2.message.slice(0, 200)}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw lastErr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从DeepSeek返回的内容里提取JSON——模型有时会用```json代码块包一层,或者前后带几句解释文字,
|
// 从DeepSeek返回的内容里提取JSON——模型有时会用```json代码块包一层,或者前后带几句解释文字,
|
||||||
|
|
@ -352,7 +443,7 @@ app.put('/api/storage/:key', requireAuth, (req, res) => {
|
||||||
|
|
||||||
app.get('/api/ai-config', requireAuth, (req, res) => {
|
app.get('/api/ai-config', requireAuth, (req, res) => {
|
||||||
const cfg = loadAiConfig();
|
const cfg = loadAiConfig();
|
||||||
res.json({ hasKey: !!cfg.apiKey });
|
res.json({ hasKey: !!cfg.apiKey, backup: { hasKey: !!cfg.backup.apiKey, model: cfg.backup.model, region: cfg.backup.region } });
|
||||||
});
|
});
|
||||||
|
|
||||||
app.put('/api/ai-config', requireAuth, (req, res) => {
|
app.put('/api/ai-config', requireAuth, (req, res) => {
|
||||||
|
|
@ -363,10 +454,43 @@ app.put('/api/ai-config', requireAuth, (req, res) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
app.delete('/api/ai-config', requireAuth, (req, res) => {
|
app.delete('/api/ai-config', requireAuth, (req, res) => {
|
||||||
try { fs.unlinkSync(AI_CONFIG_FILE); } catch (e) {}
|
const raw = readAiConfigRaw();
|
||||||
|
delete raw.apiKeyEnc; // 只清 DeepSeek 的 Key,备用阿里云的配置保留
|
||||||
|
writeAiConfigRaw(raw);
|
||||||
res.json({ ok: true });
|
res.json({ ok: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 备用服务商(阿里云百炼):Key 可留空表示只改模型/地域
|
||||||
|
app.put('/api/ai-config/backup', requireAuth, (req, res) => {
|
||||||
|
const b = req.body || {};
|
||||||
|
const raw = readAiConfigRaw();
|
||||||
|
const key = String(b.apiKey || '').trim();
|
||||||
|
if (key) raw.backupKeyEnc = encrypt(key);
|
||||||
|
if (!raw.backupKeyEnc) return res.status(400).json({ error: 'apiKey is required' });
|
||||||
|
const model = String(b.model || '').trim();
|
||||||
|
if (model && !/^[\w.\-]{1,64}$/.test(model)) return res.status(400).json({ error: 'invalid model' });
|
||||||
|
raw.backupModel = model || ALIYUN_DEFAULT_MODEL;
|
||||||
|
raw.backupRegion = b.region === 'intl' ? 'intl' : 'cn';
|
||||||
|
writeAiConfigRaw(raw);
|
||||||
|
res.json({ ok: true });
|
||||||
|
});
|
||||||
|
app.delete('/api/ai-config/backup', requireAuth, (req, res) => {
|
||||||
|
const raw = readAiConfigRaw();
|
||||||
|
delete raw.backupKeyEnc; delete raw.backupModel; delete raw.backupRegion;
|
||||||
|
writeAiConfigRaw(raw);
|
||||||
|
res.json({ ok: true });
|
||||||
|
});
|
||||||
|
app.post('/api/ai-config/backup/test', requireAuth, async (req, res) => {
|
||||||
|
const { backup } = loadAiConfig();
|
||||||
|
if (!backup.apiKey) return res.status(400).json({ error: 'no backup key configured' });
|
||||||
|
try {
|
||||||
|
const content = await callLLM(aliyunProvider(backup), [{ role: 'user', content: 'Reply with exactly: OK' }], { maxTokens: 20, timeoutMs: 30000 });
|
||||||
|
res.json({ ok: true, reply: content.trim() });
|
||||||
|
} catch (e) {
|
||||||
|
res.status(400).json({ error: e.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
app.post('/api/ai-config/test', requireAuth, async (req, res) => {
|
app.post('/api/ai-config/test', requireAuth, async (req, res) => {
|
||||||
const cfg = loadAiConfig();
|
const cfg = loadAiConfig();
|
||||||
if (!cfg.apiKey) return res.status(400).json({ error: 'no API key configured' });
|
if (!cfg.apiKey) return res.status(400).json({ error: 'no API key configured' });
|
||||||
|
|
@ -633,7 +757,7 @@ app.post('/api/ai/parse-catalog', requireAuth, aiLimiter, (req, res) => {
|
||||||
if (err) return res.status(400).json({ error: err.message });
|
if (err) return res.status(400).json({ error: err.message });
|
||||||
if (!req.files || !req.files.length) return res.status(400).json({ error: 'no file received' });
|
if (!req.files || !req.files.length) return res.status(400).json({ error: 'no file received' });
|
||||||
const cfg = loadAiConfig();
|
const cfg = loadAiConfig();
|
||||||
if (!cfg.apiKey) return res.status(400).json({ error: 'DeepSeek API key not configured' });
|
if (!cfg.apiKey && !cfg.backup.apiKey) return res.status(400).json({ error: 'AI API key not configured(设置里填 DeepSeek 或阿里云的 Key)' });
|
||||||
|
|
||||||
// 用户可以提前告诉我们这批文件大概是什么大类/品牌/细分品类,这样AI不用自己瞎猜这几个字段,
|
// 用户可以提前告诉我们这批文件大概是什么大类/品牌/细分品类,这样AI不用自己瞎猜这几个字段,
|
||||||
// 只需要专心提取每个型号的具体规格参数;提供了的话,最后会直接用这几个值覆盖AI的猜测结果。
|
// 只需要专心提取每个型号的具体规格参数;提供了的话,最后会直接用这几个值覆盖AI的猜测结果。
|
||||||
|
|
@ -740,7 +864,7 @@ app.post('/api/ai/translate-terms', requireAuth, aiLimiter, async (req, res) =>
|
||||||
if (!terms.length || !LANG_NAMES[targetLang]) return res.status(400).json({ error: 'invalid request' });
|
if (!terms.length || !LANG_NAMES[targetLang]) return res.status(400).json({ error: 'invalid request' });
|
||||||
|
|
||||||
const cfg = loadAiConfig();
|
const cfg = loadAiConfig();
|
||||||
if (!cfg.apiKey) return res.status(400).json({ error: 'DeepSeek API key not configured' });
|
if (!cfg.apiKey && !cfg.backup.apiKey) return res.status(400).json({ error: 'AI API key not configured(设置里填 DeepSeek 或阿里云的 Key)' });
|
||||||
|
|
||||||
const systemPrompt = `你是专业的电力设备(UPS/逆变器/储能电池)技术资料翻译。把用户给出的技术/业务词汇逐条翻译成${LANG_NAMES[targetLang]}。
|
const systemPrompt = `你是专业的电力设备(UPS/逆变器/储能电池)技术资料翻译。把用户给出的技术/业务词汇逐条翻译成${LANG_NAMES[targetLang]}。
|
||||||
这些词条不一定是中文——有些是从英文(或其他语言)原始资料里原样提取的参数标签/数值,本身可能已经就是${LANG_NAMES[targetLang]}或者是国际通用的表述(比如型号、单位符号、专有名词)。
|
这些词条不一定是中文——有些是从英文(或其他语言)原始资料里原样提取的参数标签/数值,本身可能已经就是${LANG_NAMES[targetLang]}或者是国际通用的表述(比如型号、单位符号、专有名词)。
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue