ai
This commit is contained in:
+67
-59
@@ -7,6 +7,10 @@ const router = new Router();
|
||||
const NVIDIA_API_KEY = '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';
|
||||
const NVIDIA_API_URL = 'https://integrate.api.nvidia.com/v1/chat/completions';
|
||||
|
||||
|
||||
// 启用/禁用 AI API(当网络不可用时设为 false)
|
||||
const ENABLE_AI_API = false; // 由于服务器网络无法访问 NVIDIA,暂时禁用
|
||||
|
||||
// Mock 数据(用于测试或网络不可用时)
|
||||
const mockProducts = [
|
||||
{ name: '可口可乐', category: '饮料', description: '经典碳酸饮料,清爽解渴,330ml罐装', suggestedPrice: 3.5 },
|
||||
@@ -15,6 +19,10 @@ const mockProducts = [
|
||||
{ name: '康师傅方便面', category: '食品', description: '红烧牛肉面口味,12桶整箱装', suggestedPrice: 39.9 },
|
||||
{ name: '农夫山泉', category: '饮料', description: '天然矿泉水,550ml瓶装,12瓶/箱', suggestedPrice: 24.0 },
|
||||
{ name: '奥利奥饼干', category: '零食', description: '夹心饼干,巧克力口味,388g家庭装', suggestedPrice: 18.9 },
|
||||
{ name: '旺仔牛奶', category: '饮料', description: '香甜可口,儿童喜爱,245ml罐装', suggestedPrice: 5.5 },
|
||||
{ name: '益达口香糖', category: '零食', description: '清新口气,薄荷口味,40粒装', suggestedPrice: 12.9 },
|
||||
{ name: '舒肤佳香皂', category: '日用品', description: '有效除菌,纯白清香,125g', suggestedPrice: 6.9 },
|
||||
{ name: '金龙鱼食用油', category: '食品', description: '调和油,健康烹饪,1.8L', suggestedPrice: 45.9 },
|
||||
];
|
||||
|
||||
// 获取随机 Mock 数据
|
||||
@@ -47,18 +55,22 @@ router.post('/generate-product', async (ctx) => {
|
||||
return;
|
||||
}
|
||||
|
||||
// 构建提示词
|
||||
let prompt = '你是一个专业的便利店商品管理助手。';
|
||||
let productInfo;
|
||||
let useMock = !ENABLE_AI_API; // 如果禁用 API,直接使用 Mock
|
||||
|
||||
if (imageUrl) {
|
||||
prompt += `\n请分析这张商品图片:${imageUrl}`;
|
||||
}
|
||||
if (!useMock) {
|
||||
// 构建提示词
|
||||
let prompt = '你是一个专业的便利店商品管理助手。';
|
||||
|
||||
if (keywords) {
|
||||
prompt += `\n关键词:${keywords}`;
|
||||
}
|
||||
if (imageUrl) {
|
||||
prompt += `\n请分析这张商品图片:${imageUrl}`;
|
||||
}
|
||||
|
||||
prompt += `
|
||||
if (keywords) {
|
||||
prompt += `\n关键词:${keywords}`;
|
||||
}
|
||||
|
||||
prompt += `
|
||||
请生成商品的详细信息,返回JSON格式,不要包含其他内容:
|
||||
{
|
||||
"name": "商品名称(简洁明了,2-10字)",
|
||||
@@ -67,61 +79,57 @@ router.post('/generate-product', async (ctx) => {
|
||||
"suggestedPrice": 建议售价(数字)
|
||||
}`;
|
||||
|
||||
let productInfo;
|
||||
let useMock = false;
|
||||
try {
|
||||
// 调用 NVIDIA NVCF 云 API(OpenAI 兼容格式)
|
||||
const response = await fetch(NVIDIA_API_URL, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${NVIDIA_API_KEY}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: 'deepseek-ai/deepseek-v4-pro',
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: prompt
|
||||
}
|
||||
],
|
||||
temperature: 0.7,
|
||||
max_tokens: 500
|
||||
}),
|
||||
timeout: 30000 // 30秒超时
|
||||
});
|
||||
|
||||
try {
|
||||
// 调用 NVIDIA NVCF 云 API(OpenAI 兼容格式)
|
||||
const response = await fetch(NVIDIA_API_URL, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${NVIDIA_API_KEY}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: 'deepseek-ai/deepseek-v4-pro',
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: prompt
|
||||
}
|
||||
],
|
||||
temperature: 0.7,
|
||||
max_tokens: 500
|
||||
}),
|
||||
timeout: 60000 // 60秒超时
|
||||
});
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
console.error('NVIDIA API Error:', response.status, errorText);
|
||||
throw new Error(`API 调用失败: ${response.status}`);
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
console.error('NVIDIA API Error:', response.status, errorText);
|
||||
throw new Error(`API 调用失败: ${response.status}`);
|
||||
const data = await response.json();
|
||||
const aiResponse = data.choices?.[0]?.message?.content;
|
||||
|
||||
if (!aiResponse) {
|
||||
throw new Error('API 返回为空');
|
||||
}
|
||||
|
||||
// 解析 JSON 响应
|
||||
const jsonMatch = aiResponse.match(/\{[\s\S]*\}/);
|
||||
if (jsonMatch) {
|
||||
productInfo = JSON.parse(jsonMatch[0]);
|
||||
} else {
|
||||
throw new Error('无法解析 AI 响应');
|
||||
}
|
||||
|
||||
} catch (apiError) {
|
||||
console.error('NVIDIA API 调用异常:', apiError.message);
|
||||
console.log('切换到 Mock 模式');
|
||||
useMock = true;
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const aiResponse = data.choices?.[0]?.message?.content;
|
||||
|
||||
if (!aiResponse) {
|
||||
console.log('API 返回为空');
|
||||
throw new Error('API 返回为空');
|
||||
}
|
||||
|
||||
// 解析 JSON 响应
|
||||
const jsonMatch = aiResponse.match(/\{[\s\S]*\}/);
|
||||
if (jsonMatch) {
|
||||
productInfo = JSON.parse(jsonMatch[0]);
|
||||
} else {
|
||||
console.log('无法解析 JSON');
|
||||
throw new Error('无法解析 AI 响应');
|
||||
}
|
||||
|
||||
} catch (apiError) {
|
||||
console.error('NVIDIA API 调用异常:', apiError.message);
|
||||
console.log('切换到 Mock 模式');
|
||||
useMock = true;
|
||||
}
|
||||
|
||||
// 使用 Mock 数据作为备用
|
||||
// 使用 Mock 数据
|
||||
if (useMock) {
|
||||
productInfo = getMockProduct(keywords);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user