Update server.js via upload script - 2026-10-15 10:20:14
This commit is contained in:
parent
53370386e2
commit
4ef9398e47
38
server.js
38
server.js
|
|
@ -108,8 +108,16 @@ function saveAiConfig(apiKey) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function callDeepSeek(apiKey, messages, opts) {
|
async function callDeepSeek(apiKey, messages, opts) {
|
||||||
const { maxTokens = 600, temperature = 0.7, model = 'deepseek-v4-flash' } = opts || {};
|
const { maxTokens = 600, temperature = 0.7, model = 'deepseek-v4-flash', timeoutMs = 120000 } = opts || {};
|
||||||
const resp = await fetch(`${DEEPSEEK_API_BASE}/chat/completions`, {
|
// 超时保护:没有这个的话,DeepSeek 一旦卡住或输出失控(一直生成到 max_tokens 上限),请求会无限期挂着,
|
||||||
|
// 前端就一直转圈。超时后直接报错,用户可以重试。
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
||||||
|
const startedAt = Date.now();
|
||||||
|
let resp;
|
||||||
|
try {
|
||||||
|
resp = await fetch(`${DEEPSEEK_API_BASE}/chat/completions`, {
|
||||||
|
signal: controller.signal,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
|
|
@ -126,12 +134,25 @@ async function callDeepSeek(apiKey, messages, opts) {
|
||||||
thinking: { type: 'disabled' },
|
thinking: { type: 'disabled' },
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
} catch (e) {
|
||||||
|
clearTimeout(timer);
|
||||||
|
if (e.name === 'AbortError') throw new Error(`DeepSeek 超过 ${Math.round(timeoutMs / 1000)} 秒没有返回结果,请重试`);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
if (!resp.ok) {
|
if (!resp.ok) {
|
||||||
|
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)}`);
|
throw new Error(`DeepSeek API error ${resp.status}: ${errText.slice(0, 200)}`);
|
||||||
}
|
}
|
||||||
const data = await resp.json();
|
let data;
|
||||||
|
try { data = await resp.json(); }
|
||||||
|
catch (e) {
|
||||||
|
if (e.name === 'AbortError') throw new Error(`DeepSeek 超过 ${Math.round(timeoutMs / 1000)} 秒没有返回结果,请重试`);
|
||||||
|
throw e;
|
||||||
|
} finally { clearTimeout(timer); }
|
||||||
const choice = data.choices && data.choices[0];
|
const choice = data.choices && data.choices[0];
|
||||||
|
// 记录耗时和 token 用量,以后再遇到"很慢"可以直接在日志里看到原因
|
||||||
|
console.log(`[DeepSeek] ${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})` : '';
|
||||||
|
|
@ -602,6 +623,9 @@ moduleDimensions/moduleWeight这组字段里就行,cabinetXxx那组(cabinetM
|
||||||
"notes": "其他有用但上面字段装不下的信息,一两句话,没有就填空字符串"
|
"notes": "其他有用但上面字段装不下的信息,一两句话,没有就填空字符串"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
【输出精简】上面的 schema 只是字段说明:每条产品记录里,值为空字符串或空数组的字段一律省略不写,只输出原文里真正有值的字段(name 必须输出)。
|
||||||
|
这样输出更短、更快,也不会因为输出太长被截断。
|
||||||
|
|
||||||
只返回一个JSON数组,不要有任何解释文字、不要用markdown代码块包裹、不要省略任何一个识别到的型号。`;
|
只返回一个JSON数组,不要有任何解释文字、不要用markdown代码块包裹、不要省略任何一个识别到的型号。`;
|
||||||
|
|
||||||
app.post('/api/ai/parse-catalog', requireAuth, aiLimiter, (req, res) => {
|
app.post('/api/ai/parse-catalog', requireAuth, aiLimiter, (req, res) => {
|
||||||
|
|
@ -670,7 +694,13 @@ app.post('/api/ai/parse-catalog', requireAuth, aiLimiter, (req, res) => {
|
||||||
const content = await callDeepSeekWithRetry(cfg.apiKey, [
|
const content = await callDeepSeekWithRetry(cfg.apiKey, [
|
||||||
{ role: 'system', content: CATALOG_PARSE_SYSTEM_PROMPT },
|
{ role: 'system', content: CATALOG_PARSE_SYSTEM_PROMPT },
|
||||||
{ role: 'user', content: userContent },
|
{ role: 'user', content: userContent },
|
||||||
], { maxTokens: 64000, temperature: 0.2 });
|
], {
|
||||||
|
// 按文档长度给输出预算:短文档(比如一页HTML规格表)不需要64000这么大的上限,
|
||||||
|
// 上限太大时一旦模型输出失控,会一直生成十几分钟才结束
|
||||||
|
maxTokens: Math.min(64000, Math.max(12000, Math.round(clippedText.length * 1.5))),
|
||||||
|
temperature: 0.2,
|
||||||
|
timeoutMs: 300000, // 5分钟
|
||||||
|
});
|
||||||
const { data: products, truncated } = extractJson(content);
|
const { data: products, truncated } = extractJson(content);
|
||||||
if (!Array.isArray(products)) throw new Error('AI没有返回预期的数组格式,请重试');
|
if (!Array.isArray(products)) throw new Error('AI没有返回预期的数组格式,请重试');
|
||||||
// 双保险:即使AI没完全听话,这里直接强制覆盖,保证和用户预选的一致
|
// 双保险:即使AI没完全听话,这里直接强制覆盖,保证和用户预选的一致
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue