Update micUploadRoutes.js via upload script - 2026-09-21 01:30:52
This commit is contained in:
parent
6a440c996f
commit
471b55f84e
|
|
@ -23,34 +23,91 @@ const WebSocket = require('ws');
|
|||
const { MicBrowserSession } = require('./micBrowserSession');
|
||||
const { buildAttributeList } = require('./micFieldMapping');
|
||||
|
||||
const _KW_SYSTEM_PROMPT = `You are a B2B export keyword expert for energy products on Made-in-China.com.
|
||||
Generate exactly 10 unique English search keywords for the given product.
|
||||
Rules:
|
||||
1. Return ONLY a JSON array of exactly 10 strings. No explanation, no markdown.
|
||||
2. All 10 keywords must be mutually unique.
|
||||
3. Each keyword: 2-6 words, phrased as a buyer search query.
|
||||
4. Cover varied angles: capacity+type, application, technology, buyer intent.
|
||||
Output format: ["keyword one", ..., "keyword ten"]`;
|
||||
// 标题/关键词规则来自MIC官方说明(2026-09抓取):
|
||||
// 标题 https://service.made-in-china.com/help/promotionskill/product/3243.htm
|
||||
// 关键词 https://service.made-in-china.com/help/promotionskill/product/473.htm
|
||||
const _TITLE_SYSTEM_PROMPT = `You write English product titles for Made-in-China.com B2B listings, following the platform's official rules:
|
||||
- 5 to 12 words total.
|
||||
- Suggested structure: [modifier(s)] + [ONE core noun] + for [application scope] + with [certifications/key feature] + (model/standard/feature). Omit the "for"/"with" parts if there's nothing relevant to fill them with — never leave a literal "for"/"with" with nothing after it.
|
||||
- The whole title must revolve around a SINGLE core noun (the main product category term, e.g. "UPS", "Inverter", "Battery"). Do not stack multiple synonyms for the same thing.
|
||||
- Do not write an overly bare/generic title with no modifiers — add descriptive modifiers (capacity, technology, application) so it matches varied buyer search phrasing.
|
||||
- Precision matters more than keyword stuffing.
|
||||
Output ONLY the title text itself. No quotes, no markdown, no explanation.`;
|
||||
|
||||
function localFallbackKeywords(title) {
|
||||
// 原脚本里AI失败时的本地兜底逻辑,直接搬过来,保底不至于完全没有关键词
|
||||
const low = (title || '').toLowerCase();
|
||||
const capMatch = low.match(/(\d+)\s*kva/);
|
||||
const kwhMatch = low.match(/(\d+)\s*kwh/);
|
||||
if (kwhMatch) {
|
||||
const _KW_SYSTEM_PROMPT_TMPL = (coreNoun) => `You write English search keywords for a Made-in-China.com product listing, following the platform's official rules:
|
||||
- Format: [modifier] + [core noun]. The core noun in EVERY keyword must be "${coreNoun}" (the same core noun used in the product title) — do not switch to a different core noun.
|
||||
- Return exactly 10 keywords as a JSON array of strings, ordered by importance: the first 3 are the most important/representative and should reflect the product's most defining traits.
|
||||
- Keywords 4-10 should echo the title's modifiers with light variation (different buyer phrasing/synonyms for the same modifiers), not introduce unrelated product types.
|
||||
- No duplicate keywords (case/plural-insensitive).
|
||||
- Each keyword: 2-4 words.
|
||||
Output ONLY a JSON array of exactly 10 strings, nothing else.`;
|
||||
|
||||
// 把 buildAttributeList 生成的属性列表挑几条最有代表性的,拼成一段给AI看的
|
||||
// 产品规格摘要——不用全部20条塞进去,模型上下文不需要那么细,挑前几条即可。
|
||||
function summarizeSpecsForAi(attrList, max = 8) {
|
||||
return attrList.slice(0, max).map((a) => `${a.name}: ${a.value}`).join('; ');
|
||||
}
|
||||
|
||||
function localFallbackTitle(categoryName, model) {
|
||||
// AI不可用时的兜底——尽量别太离谱,但明确标注这是兜底,提醒人工检查
|
||||
return `${categoryName || 'Power Equipment'} ${model}`.trim();
|
||||
}
|
||||
|
||||
function localFallbackKeywords(coreNoun) {
|
||||
const noun = coreNoun || 'UPS';
|
||||
return [
|
||||
`${kwhMatch[1]}kWh LiFePO4 Battery`, 'Rack Mount Lithium Battery', 'Solar Energy Storage Battery',
|
||||
'LiFePO4 Battery Pack', 'Rack Battery Pack', 'ESS Battery Module',
|
||||
'Lithium Iron Phosphate Battery', 'Off-grid Storage Battery', 'RS485 CAN Battery Pack',
|
||||
'Energy Storage Battery Supplier',
|
||||
`${noun}`, `Industrial ${noun}`, `${noun} Supplier`, `${noun} Manufacturer`,
|
||||
`High Efficiency ${noun}`, `${noun} for Sale`, `Wholesale ${noun}`, `${noun} Factory`,
|
||||
`Custom ${noun}`, `${noun} Price`,
|
||||
];
|
||||
}
|
||||
const pfx = capMatch ? `${capMatch[1]}KVA ` : '';
|
||||
return [
|
||||
`${pfx}Online UPS`, 'Double Conversion UPS', 'UPS for Data Centre', 'Industrial Online UPS',
|
||||
'High Efficiency UPS', 'Wide Input Voltage UPS', 'Generator Compatible UPS', 'SNMP Managed UPS',
|
||||
'Uninterruptible Power Supply', 'Online UPS Supplier',
|
||||
];
|
||||
|
||||
async function generateTitle(ctx, { categoryName, model, applicationScenarios, specsSummary }, log) {
|
||||
let apiKey = '';
|
||||
try {
|
||||
const cfg = ctx.loadAiConfig();
|
||||
apiKey = cfg && cfg.apiKey;
|
||||
} catch (e) { /* 走兜底 */ }
|
||||
|
||||
if (!apiKey) {
|
||||
log('未配置DeepSeek Key,标题使用简单兜底,建议人工检查/补充', 'WARNING');
|
||||
return localFallbackTitle(categoryName, model);
|
||||
}
|
||||
|
||||
const userLines = [
|
||||
`Product category: ${categoryName || '(unknown)'}`,
|
||||
`Model: ${model}`,
|
||||
applicationScenarios ? `Application scenarios: ${applicationScenarios}` : null,
|
||||
specsSummary ? `Key specs: ${specsSummary}` : null,
|
||||
'Write the product title now.',
|
||||
].filter(Boolean);
|
||||
|
||||
try {
|
||||
const content = await ctx.callDeepSeekWithRetry(
|
||||
apiKey,
|
||||
[{ role: 'system', content: _TITLE_SYSTEM_PROMPT }, { role: 'user', content: userLines.join('\n') }],
|
||||
{ temperature: 0.5, maxTokens: 100 },
|
||||
2,
|
||||
);
|
||||
const title = String(content).trim().replace(/^["']|["']$/g, '');
|
||||
const wordCount = title.split(/\s+/).filter(Boolean).length;
|
||||
if (!title || wordCount < 3 || wordCount > 20) throw new Error(`AI标题长度异常(${wordCount}词): ${title}`);
|
||||
log(`AI生成标题: ${title}`);
|
||||
return title;
|
||||
} catch (e) {
|
||||
log(`AI标题生成失败,使用简单兜底,建议人工检查/补充: ${e.message}`, 'WARNING');
|
||||
return localFallbackTitle(categoryName, model);
|
||||
}
|
||||
}
|
||||
|
||||
// coreNounHint:从生成好的标题里粗略摘一个核心名词给关键词prompt用(简单取标题
|
||||
// 里最后一个大写开头的名词性单词,不追求完美,AI会自己判断,这里只是提示)
|
||||
function guessCoreNoun(title) {
|
||||
const words = (title || '').split(/\s+/).filter(Boolean);
|
||||
const stop = new Set(['for', 'with', 'and', 'the', 'a', 'an', 'of']);
|
||||
const forIdx = words.findIndex((w) => w.toLowerCase() === 'for');
|
||||
const candidates = (forIdx > 0 ? words.slice(0, forIdx) : words).filter((w) => !stop.has(w.toLowerCase()));
|
||||
return candidates.length ? candidates[candidates.length - 1] : (words[words.length - 1] || 'Product');
|
||||
}
|
||||
|
||||
async function generateKeywords(ctx, title, model, log) {
|
||||
|
|
@ -60,14 +117,16 @@ async function generateKeywords(ctx, title, model, log) {
|
|||
apiKey = cfg && cfg.apiKey;
|
||||
} catch (e) { /* 没配置DeepSeek key时走本地兜底 */ }
|
||||
|
||||
const coreNoun = guessCoreNoun(title);
|
||||
|
||||
if (!apiKey) {
|
||||
log('未配置DeepSeek Key,使用本地关键词兜底', 'WARNING');
|
||||
return localFallbackKeywords(title);
|
||||
return localFallbackKeywords(coreNoun);
|
||||
}
|
||||
|
||||
const messages = [
|
||||
{ role: 'system', content: _KW_SYSTEM_PROMPT },
|
||||
{ role: 'user', content: `Product: ${title}\nModel: ${model}\nGenerate 10 unique English search keywords.` },
|
||||
{ role: 'system', content: _KW_SYSTEM_PROMPT_TMPL(coreNoun) },
|
||||
{ role: 'user', content: `Product title: ${title}\nModel: ${model}\nGenerate 10 keywords now.` },
|
||||
];
|
||||
try {
|
||||
const content = await ctx.callDeepSeekWithRetry(apiKey, messages, { temperature: 0.7, maxTokens: 400 }, 2);
|
||||
|
|
@ -89,7 +148,7 @@ async function generateKeywords(ctx, title, model, log) {
|
|||
return unique.slice(0, 10);
|
||||
} catch (e) {
|
||||
log(`AI关键词生成失败,使用本地兜底: ${e.message}`, 'WARNING');
|
||||
return localFallbackKeywords(title);
|
||||
return localFallbackKeywords(coreNoun);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -270,10 +329,18 @@ function registerMicUpload(app, server, ctx) {
|
|||
});
|
||||
}
|
||||
|
||||
const { title, model, attrList, warnings } = buildAttributeList(product);
|
||||
const { model, attrList, warnings } = buildAttributeList(product);
|
||||
for (const w of warnings) session.log(w, 'WARNING');
|
||||
|
||||
await session.gotoPublishTemplate(templateUrl);
|
||||
|
||||
const title = await generateTitle(ctx, {
|
||||
categoryName: product.type || product.category,
|
||||
model,
|
||||
applicationScenarios: product.applicationScenarios,
|
||||
specsSummary: summarizeSpecsForAi(attrList),
|
||||
}, (m, l) => session.log(m, l));
|
||||
|
||||
await session.fillTitle(title);
|
||||
await session.checkCenterWord();
|
||||
const keywords = await generateKeywords(ctx, title, model, (m, l) => session.log(m, l));
|
||||
|
|
@ -281,7 +348,7 @@ function registerMicUpload(app, server, ctx) {
|
|||
await session.fillModel(model);
|
||||
await session.fillAttributes(attrList);
|
||||
|
||||
res.json({ ok: true, warnings });
|
||||
res.json({ ok: true, warnings, title, keywords });
|
||||
} catch (e) {
|
||||
session.log(`填表流程出错: ${e.message}`, 'ERROR');
|
||||
res.status(500).json({ error: e.message });
|
||||
|
|
|
|||
Loading…
Reference in New Issue