From 66e2f0bcdf34af2788b9b10a7fae1022f7385283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E6=B5=B7=E6=B4=8B?= Date: Sun, 24 May 2026 14:44:35 +0800 Subject: [PATCH] ai --- routes/ai.js | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/routes/ai.js b/routes/ai.js index 50f45a8..d9ec8bb 100644 --- a/routes/ai.js +++ b/routes/ai.js @@ -57,16 +57,28 @@ router.post('/generate-product', async (ctx) => { temperature: 0.7, max_tokens: 500 }), - timeout: 60000 + timeout: 30000 }); if (!response.ok) { const errorText = await response.text(); console.error('NVIDIA API Error:', response.status, errorText); - ctx.status = 502; + + let errorMsg = 'AI 服务调用失败'; + if (response.status === 401) { + errorMsg = 'API Key 无效,请检查密钥配置'; + } else if (response.status === 403) { + errorMsg = 'API 调用被拒绝,请检查账户权限'; + } else if (response.status === 429) { + errorMsg = 'API 调用次数超限,请稍后重试'; + } else if (response.status === 503) { + errorMsg = 'AI 服务暂时不可用,请稍后重试'; + } + + ctx.status = response.status; ctx.body = { - code: 502, - message: 'AI 服务调用失败,请稍后重试' + code: response.status, + message: errorMsg }; return; } @@ -88,7 +100,7 @@ router.post('/generate-product', async (ctx) => { ctx.status = 500; ctx.body = { code: 500, - message: '无法解析 AI 响应' + message: '无法解析 AI 响应格式' }; return; } @@ -103,10 +115,20 @@ router.post('/generate-product', async (ctx) => { } catch (error) { console.error('生成商品信息失败:', error); - ctx.status = 500; + + let errorMsg = '生成失败,请稍后重试'; + if (error.message.includes('timeout')) { + errorMsg = 'AI 服务响应超时,请检查网络或稍后重试'; + } else if (error.message.includes('ENOTFOUND')) { + errorMsg = '无法连接到 AI 服务,请检查网络设置'; + } else if (error.message.includes('ECONNRESET')) { + errorMsg = 'AI 服务连接中断,请稍后重试'; + } + + ctx.status = 503; ctx.body = { - code: 500, - message: error.message || '生成失败,请稍后重试' + code: 503, + message: errorMsg }; } });