Update micUploadRoutes.js via upload script - 2026-09-21 02:39:27

This commit is contained in:
mike 2026-09-21 02:39:36 +08:00
parent 476d0bf256
commit 6e0613ce9d
1 changed files with 29 additions and 24 deletions

View File

@ -32,18 +32,22 @@ const _TITLE_SYSTEM_PROMPT = `You write English product titles for Made-in-China
- 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.`;
You must also report which exact word in your title is the core noun (copy it verbatim, single word, as it literally appears in the title — e.g. "UPS", not "UPS unit").
Output ONLY a JSON object, nothing else, in this exact shape:
{"title": "...", "coreNoun": "..."}`;
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.
- 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, and don't append unrelated trailing words after it.
- NEVER include the specific model number / SKU (e.g. "SR1K") in a keyword. Keywords are generic buyer search phrases, not product-specific identifiers — real buyers search "Rack Mount UPS", never "Rack Mount UPS SR1K".
- 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 or near-duplicate keywords: treat different spellings/spacing of the same modifier as duplicates too (e.g. "Rack Mount" and "Rackmount" count as the same modifier — only use one form).
- Each keyword: 2-4 words.
- Each keyword: 2-4 words, and every single one must actually be a coherent, grammatical buyer search phrase — sanity-check each one before outputting.
Example (official guide's own example, for format reference only):
Title core idea: "Luxury King Size Wooden Frame Headboard Hotel Bed"
Title core idea: "Luxury King Size Wooden Frame Headboard Hotel Bed" (core noun: "Bed")
Good keywords: "Hotel Bed", "Wooden Frame Headboard Bed", "King Size Bed"
(Notice: no model number, no SKU, no brand-specific text — just modifier + core noun.)
@ -57,7 +61,7 @@ function summarizeSpecsForAi(attrList, max = 8) {
function localFallbackTitle(categoryName, model) {
// AI不可用时的兜底——尽量别太离谱,但明确标注这是兜底,提醒人工检查
return `${categoryName || 'Power Equipment'} ${model}`.trim();
return { title: `${categoryName || 'Power Equipment'} ${model}`.trim(), coreNoun: categoryName || 'Product' };
}
function localFallbackKeywords(coreNoun) {
@ -69,6 +73,9 @@ function localFallbackKeywords(coreNoun) {
];
}
// 返回 { title, coreNoun }——中心词直接由生成标题的同一次AI调用给出,不再靠
// 事后从标题字符串里猜(之前那套"for前面最后一个词"的算法碰到标题里"for"
// 前面刚好是别的修饰词时就会猜错,产出一堆带着错误中心词的关键词)。
async function generateTitle(ctx, { categoryName, model, applicationScenarios, specsSummary }, log) {
let apiKey = '';
try {
@ -93,39 +100,37 @@ async function generateTitle(ctx, { categoryName, model, applicationScenarios, s
const content = await ctx.callDeepSeekWithRetry(
apiKey,
[{ role: 'system', content: _TITLE_SYSTEM_PROMPT }, { role: 'user', content: userLines.join('\n') }],
{ temperature: 0.5, maxTokens: 100 },
{ temperature: 0.5, maxTokens: 150 },
2,
);
const title = String(content).trim().replace(/^["']|["']$/g, '');
let raw = String(content).trim();
if (raw.startsWith('```')) {
raw = raw.split('```')[1];
if (raw.startsWith('json')) raw = raw.slice(4);
}
const parsed = JSON.parse(raw.trim());
const title = String(parsed.title || '').trim();
const coreNoun = String(parsed.coreNoun || '').trim();
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;
if (!coreNoun || !title.toLowerCase().includes(coreNoun.toLowerCase())) {
throw new Error(`AI给的中心词"${coreNoun}"没有实际出现在标题里,不可信`);
}
log(`AI生成标题: ${title}(中心词: ${coreNoun})`);
return { title, coreNoun };
} 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) {
async function generateKeywords(ctx, title, coreNoun, model, log) {
let apiKey = '';
try {
const cfg = ctx.loadAiConfig(); // 同步函数,跟server.js里的定义一致
apiKey = cfg && cfg.apiKey;
} catch (e) { /* 没配置DeepSeek key时走本地兜底 */ }
const coreNoun = guessCoreNoun(title);
if (!apiKey) {
log('未配置DeepSeek Key,使用本地关键词兜底', 'WARNING');
return localFallbackKeywords(coreNoun);
@ -349,7 +354,7 @@ function registerMicUpload(app, server, ctx) {
await session.gotoPublishTemplate(templateUrl);
const title = await generateTitle(ctx, {
const { title, coreNoun } = await generateTitle(ctx, {
categoryName: product.type || product.category,
model,
applicationScenarios: product.applicationScenarios,
@ -358,7 +363,7 @@ function registerMicUpload(app, server, ctx) {
await session.fillTitle(title);
await session.checkCenterWord();
const keywords = await generateKeywords(ctx, title, model, (m, l) => session.log(m, l));
const keywords = await generateKeywords(ctx, title, coreNoun, model, (m, l) => session.log(m, l));
await session.fillKeywords(keywords);
await session.fillModel(model);
// "规格"填产品尺寸:普通品类用 dimensions,电池系统品类没有这个字段,