333 lines
14 KiB
JavaScript
333 lines
14 KiB
JavaScript
/**
|
||
* Brain决策逻辑
|
||
* 调用LLM分析产品信息并生成12张图的规划
|
||
*/
|
||
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
const axios = require('axios');
|
||
|
||
// 读取Brain System Prompt
|
||
const brainPromptPath = path.join(__dirname, '../prompts/brain-system.md');
|
||
let brainSystemPrompt = '';
|
||
|
||
try {
|
||
brainSystemPrompt = fs.readFileSync(brainPromptPath, 'utf-8');
|
||
} catch (err) {
|
||
console.error('Failed to load brain system prompt:', err.message);
|
||
}
|
||
|
||
/**
|
||
* 调用LLM生成图片规划
|
||
* @param {object} sku - SKU数据
|
||
* @param {object} options - 选项
|
||
* @param {string} options.apiKey - API密钥
|
||
* @param {string} options.apiUrl - API地址
|
||
* @param {string} options.model - 模型名称
|
||
* @returns {Promise<object>} Brain输出的图片规划
|
||
*/
|
||
async function generateImagePlan(sku, options = {}) {
|
||
const {
|
||
apiKey = process.env.API_KEY,
|
||
apiUrl = 'https://api2img.shubiaobiao.com/v1/chat/completions',
|
||
model = 'gemini-3-pro-preview'
|
||
} = options;
|
||
|
||
if (!apiKey) {
|
||
throw new Error('API_KEY is required');
|
||
}
|
||
|
||
// 构建用户消息
|
||
const userMessage = `请根据以下产品信息,规划12张电商图片(6张主图 + 6张A+图)的内容策略。
|
||
|
||
## 产品信息
|
||
|
||
\`\`\`json
|
||
${JSON.stringify(sku, null, 2)}
|
||
\`\`\`
|
||
|
||
请严格按照System Prompt中的输出格式返回JSON。`;
|
||
|
||
try {
|
||
const response = await axios.post(apiUrl, {
|
||
model: model,
|
||
messages: [
|
||
{ role: 'system', content: brainSystemPrompt },
|
||
{ role: 'user', content: userMessage }
|
||
],
|
||
temperature: 0.7,
|
||
max_tokens: 8000
|
||
}, {
|
||
headers: {
|
||
'Content-Type': 'application/json',
|
||
'Authorization': `Bearer ${apiKey}`
|
||
},
|
||
timeout: 120000 // 2分钟超时
|
||
});
|
||
|
||
// 解析响应
|
||
let content = response.data.choices?.[0]?.message?.content;
|
||
|
||
if (!content) {
|
||
throw new Error('Empty response from LLM');
|
||
}
|
||
|
||
// 清理响应内容
|
||
content = cleanLLMResponse(content);
|
||
|
||
// 解析JSON
|
||
const plan = parseJSONFromResponse(content);
|
||
|
||
// 验证输出格式
|
||
validatePlan(plan);
|
||
|
||
return plan;
|
||
|
||
} catch (error) {
|
||
console.error('Brain generation error:', error.message);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 清理LLM响应
|
||
* @param {string} content - 原始响应内容
|
||
* @returns {string} 清理后的内容
|
||
*/
|
||
function cleanLLMResponse(content) {
|
||
// 移除 <think> 块
|
||
content = content.replace(/<think>[\s\S]*?<\/think>/gi, '').trim();
|
||
|
||
// 移除可能的markdown标记
|
||
content = content.replace(/^```json\s*/i, '');
|
||
content = content.replace(/\s*```$/i, '');
|
||
|
||
return content.trim();
|
||
}
|
||
|
||
/**
|
||
* 从响应中解析JSON
|
||
* @param {string} content - 响应内容
|
||
* @returns {object} 解析后的JSON对象
|
||
*/
|
||
function parseJSONFromResponse(content) {
|
||
// 尝试直接解析
|
||
try {
|
||
return JSON.parse(content);
|
||
} catch (e) {
|
||
// 尝试提取JSON块
|
||
const jsonMatch = content.match(/\{[\s\S]*\}/);
|
||
if (jsonMatch) {
|
||
try {
|
||
return JSON.parse(jsonMatch[0]);
|
||
} catch (e2) {
|
||
// 尝试修复常见问题
|
||
let fixed = jsonMatch[0];
|
||
|
||
// 修复尾部逗号
|
||
fixed = fixed.replace(/,\s*([}\]])/g, '$1');
|
||
|
||
// 修复未闭合的字符串
|
||
fixed = fixed.replace(/:\s*"([^"]*?)(?=\s*[,}\]])/g, ': "$1"');
|
||
|
||
try {
|
||
return JSON.parse(fixed);
|
||
} catch (e3) {
|
||
throw new Error('Failed to parse JSON from LLM response: ' + e.message);
|
||
}
|
||
}
|
||
}
|
||
throw new Error('No valid JSON found in LLM response');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 验证Brain输出的计划
|
||
* @param {object} plan - 图片规划
|
||
*/
|
||
function validatePlan(plan) {
|
||
if (!plan.analysis) {
|
||
throw new Error('Missing analysis in plan');
|
||
}
|
||
|
||
if (!plan.images || !Array.isArray(plan.images)) {
|
||
throw new Error('Missing or invalid images array in plan');
|
||
}
|
||
|
||
if (plan.images.length !== 12) {
|
||
console.warn(`Expected 12 images, got ${plan.images.length}`);
|
||
}
|
||
|
||
// 检查必要字段
|
||
const requiredFields = ['id', 'type', 'ai_prompt'];
|
||
for (const image of plan.images) {
|
||
for (const field of requiredFields) {
|
||
if (!image[field]) {
|
||
throw new Error(`Missing ${field} in image ${image.id || 'unknown'}`);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 检查图片ID是否正确
|
||
const expectedIds = [
|
||
'Main_01', 'Main_02', 'Main_03', 'Main_04', 'Main_05', 'Main_06',
|
||
'APlus_01', 'APlus_02', 'APlus_03', 'APlus_04', 'APlus_05', 'APlus_06'
|
||
];
|
||
|
||
const actualIds = plan.images.map(img => img.id);
|
||
const missingIds = expectedIds.filter(id => !actualIds.includes(id));
|
||
|
||
if (missingIds.length > 0) {
|
||
console.warn(`Missing image IDs: ${missingIds.join(', ')}`);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 生成默认的图片规划(当Brain调用失败时使用)
|
||
* @param {object} sku - SKU数据
|
||
* @returns {object} 默认的图片规划
|
||
*/
|
||
function generateDefaultPlan(sku) {
|
||
const topSellingPoints = sku.selling_points.slice(0, 3).map(sp => sp.key);
|
||
const productColor = sku.color.name;
|
||
const productName = sku.product_name;
|
||
|
||
return {
|
||
analysis: {
|
||
product_category: 'Pet Recovery Cone',
|
||
core_selling_points: topSellingPoints,
|
||
background_strategy: 'light',
|
||
logo_color: 'red',
|
||
visual_style: 'Warm, caring home environment'
|
||
},
|
||
images: [
|
||
{
|
||
id: 'Main_01',
|
||
type: 'Hero Scene + Key Benefits',
|
||
purpose: '首图决定点击率,展示产品使用状态+核心卖点',
|
||
layout_description: `上60%:猫咪佩戴${productColor}${productName}的居家场景。下40%:浅色Banner展示3个核心卖点。`,
|
||
ai_prompt: `Professional Amazon main image, 1:1 square format. A beautiful cat wearing a ${productColor} soft cone collar in a cozy modern home interior. Natural soft lighting, warm atmosphere. Bottom section shows a light blue rounded banner with "DESIGNED FOR COMFORTABLE RECOVERY" text and three small product detail images highlighting key features. Clean, professional pet product photography style. --ar 1:1`,
|
||
logo_placement: { position: 'bottom-right', type: 'combined', color: 'red' }
|
||
},
|
||
{
|
||
id: 'Main_02',
|
||
type: 'Product Detail + Craftsmanship',
|
||
purpose: '展示产品结构和工艺细节',
|
||
layout_description: `产品平铺俯视图,配合2-3个细节放大框展示材质和工艺`,
|
||
ai_prompt: `Professional product flat lay image, 1:1 square format. ${productColor} soft cone collar displayed from above on white background. Two circular detail callouts showing: 1) waterproof PU outer layer texture, 2) soft cotton inner lining. Clean product photography, even lighting, high detail. Brand logo "TOUCHDOG®" visible on right petal. --ar 1:1`,
|
||
logo_placement: { position: 'none', type: 'none', color: 'red' }
|
||
},
|
||
{
|
||
id: 'Main_03',
|
||
type: 'Function Demonstration',
|
||
purpose: '展示可调节绑带等功能特性',
|
||
layout_description: `主场景展示佩戴状态,配合细节图展示调节功能`,
|
||
ai_prompt: `Professional Amazon feature demonstration image, 1:1 square format. Main image shows cat wearing ${productColor} cone, looking comfortable. Detail callouts show: adjustable velcro strap being secured, snug fit around neck. Text overlay "ADJUSTABLE STRAP FOR A SECURE FIT". Light blue background with paw print decorations. --ar 1:1`,
|
||
logo_placement: { position: 'bottom-right', type: 'combined', color: 'red' }
|
||
},
|
||
{
|
||
id: 'Main_04',
|
||
type: 'Use Cases Grid',
|
||
purpose: '展示4种适用场景',
|
||
layout_description: `四宫格布局,展示术后护理、滴药、剪指甲、梳毛四个场景`,
|
||
ai_prompt: `Professional Amazon use case image, 1:1 square format. Four-panel grid showing: 1) cat resting after surgery wearing cone, 2) owner applying eye drops to cat, 3) owner trimming cat's nails, 4) cat being groomed. Each panel labeled: "POSTOPERATIVE CARE", "EYE MEDICATION", "NAIL TRIMMING", "GROOMING". Light blue header banner "APPLICABLE SCENARIOS". --ar 1:1`,
|
||
logo_placement: { position: 'bottom-right', type: 'combined', color: 'red' }
|
||
},
|
||
{
|
||
id: 'Main_05',
|
||
type: 'Size & Specifications',
|
||
purpose: '展示尺寸规格信息',
|
||
layout_description: `产品尺寸标注图配合尺码对照表`,
|
||
ai_prompt: `Professional Amazon size guide image, 1:1 square format. ${productColor} cone collar with dimension annotations showing depth measurement (${sku.specs.depth_cm}cm). Clean size chart showing XS to XL with neck circumference ranges. Professional infographic style, clear readable text. Light background. --ar 1:1`,
|
||
logo_placement: { position: 'bottom-right', type: 'combined', color: 'red' }
|
||
},
|
||
{
|
||
id: 'Main_06',
|
||
type: 'Brand & Benefits Summary',
|
||
purpose: '强化品牌认知和卖点汇总',
|
||
layout_description: `品牌Logo突出展示,配合核心卖点图标`,
|
||
ai_prompt: `Professional Amazon brand summary image, 1:1 square format. Touchdog brand logo prominently displayed. Key benefit icons with text: "65g Ultra Light", "Waterproof PU", "Breathable Cotton", "Adjustable Fit", "Foldable Design". Cat wearing ${productColor} cone as background element. Premium brand aesthetic. --ar 1:1`,
|
||
logo_placement: { position: 'center', type: 'combined', color: 'red' }
|
||
},
|
||
{
|
||
id: 'APlus_01',
|
||
type: 'Brand Banner',
|
||
purpose: '品牌形象首图',
|
||
layout_description: `品牌名+产品名+生活场景大图`,
|
||
ai_prompt: `Professional Amazon A+ banner image, 970x600px landscape. Beautiful cat wearing ${productColor} Touchdog soft cone collar in warm modern home interior. Large "TOUCHDOG" brand text and "CAT SOFT CONE COLLAR" product name overlaid. Lifestyle photography, natural lighting, cozy atmosphere. --ar 3:2`,
|
||
logo_placement: { position: 'top-left', type: 'combined', color: 'red' }
|
||
},
|
||
{
|
||
id: 'APlus_02',
|
||
type: 'Competitor Comparison',
|
||
purpose: '竞品对比展示优势',
|
||
layout_description: `左侧我们的产品(彩色+优点),右侧传统产品(灰色+缺点)`,
|
||
ai_prompt: `Professional Amazon A+ comparison image, 970x600px landscape. Left side (colored): Touchdog ${productColor} soft cone with green checkmark, labeled "OUR" with benefits "CLOUD-LIGHT COMFORT", "WIDER & CLEARER", "FOLDABLE & PORTABLE". Right side (grayscale): Generic plastic cone with red X, labeled "OTHER" with drawbacks "HEAVY & BULKY", "BLOCKS VISION & MOVEMENT", "HARD TO STORE". Center shows cat wearing our product. Warm beige background with paw prints. --ar 3:2`,
|
||
logo_placement: { position: 'bottom-right', type: 'combined', color: 'red' },
|
||
is_comparison: true
|
||
},
|
||
{
|
||
id: 'APlus_03',
|
||
type: 'Benefits Grid',
|
||
purpose: '核心卖点展示',
|
||
layout_description: `三宫格展示3个核心卖点`,
|
||
ai_prompt: `Professional Amazon A+ benefits image, 970x600px landscape. Three-panel layout showing key features: 1) "STURDY AND BREATHABLE" with fabric texture close-up, 2) "EASY TO CLEAN" with dimension annotation ${sku.specs.depth_cm}cm, 3) "REINFORCED STITCHING" with stitching detail. Header "ENGINEERED FOR UNCOMPROMISED COMFORT". Warm beige background. --ar 3:2`,
|
||
logo_placement: { position: 'bottom-right', type: 'combined', color: 'red' }
|
||
},
|
||
{
|
||
id: 'APlus_04',
|
||
type: 'Features Grid',
|
||
purpose: '功能场景展示',
|
||
layout_description: `四宫格展示功能:易清洁、不影响进食、可翻折、360°舒适`,
|
||
ai_prompt: `Professional Amazon A+ features image, 970x600px landscape. Four vertical panels: 1) "HYGIENIC & EASY TO CLEAN" showing water-resistant surface, 2) "UNRESTRICTED EATING/DRINKING" showing cat eating while wearing cone, 3) "REVERSIBLE WEAR" showing flip-over design, 4) "360° COMFORT" showing cat sleeping peacefully. Warm beige background, consistent styling. --ar 3:2`,
|
||
logo_placement: { position: 'bottom-right', type: 'combined', color: 'red' }
|
||
},
|
||
{
|
||
id: 'APlus_05',
|
||
type: 'Material & Craftsmanship',
|
||
purpose: '材质工艺展示',
|
||
layout_description: `材质剖面和工艺细节展示`,
|
||
ai_prompt: `Professional Amazon A+ material image, 970x600px landscape. Close-up details of ${productColor} cone showing: waterproof PU outer layer with water droplets, soft cotton inner lining texture, reinforced stitching seams, embroidered TOUCHDOG logo. Technical infographic style with material callouts. Light background. --ar 3:2`,
|
||
logo_placement: { position: 'bottom-right', type: 'combined', color: 'red' }
|
||
},
|
||
{
|
||
id: 'APlus_06',
|
||
type: 'Size Guide',
|
||
purpose: '尺寸选择指南',
|
||
layout_description: `完整尺码表+测量指南`,
|
||
ai_prompt: `Professional Amazon A+ size guide image, 970x600px landscape. Comprehensive size chart showing all sizes (XS-XL) with neck circumference and depth measurements in both cm and inches. Illustration showing how to measure pet's neck. Clear, readable table format. Helpful sizing tips. Professional infographic design. --ar 3:2`,
|
||
logo_placement: { position: 'bottom-right', type: 'combined', color: 'red' }
|
||
}
|
||
]
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 主入口:生成图片规划
|
||
* @param {object} sku - SKU数据
|
||
* @param {object} options - 选项
|
||
* @returns {Promise<object>} 图片规划
|
||
*/
|
||
async function planImages(sku, options = {}) {
|
||
try {
|
||
// 尝试调用LLM生成计划
|
||
const plan = await generateImagePlan(sku, options);
|
||
console.log('Brain generated plan successfully');
|
||
return plan;
|
||
} catch (error) {
|
||
console.error('Brain failed, using default plan:', error.message);
|
||
// 失败时使用默认计划
|
||
return generateDefaultPlan(sku);
|
||
}
|
||
}
|
||
|
||
module.exports = {
|
||
planImages,
|
||
generateImagePlan,
|
||
generateDefaultPlan,
|
||
validatePlan
|
||
};
|
||
|
||
|
||
|