feat(product-suite): add category targets and prompt compatibility

This commit is contained in:
chengma
2026-07-17 15:35:28 +08:00
parent 7426c181a7
commit f6a1ad9ffc
9 changed files with 120 additions and 26 deletions
+58 -10
View File
@@ -6,14 +6,29 @@ from collections import OrderedDict
import re
FIXED_CATEGORIES = ("白底图", "场景图", "卖点图")
FIXED_CATEGORIES = ("白底图", "场景图", "模特场景图", "细节说明图", "卖点图")
FIXED_CATEGORY_HELPERS = {
"白底图": "白底主图,多角度呈现商品细节",
"场景图": "生活化场景展示商品使用方式",
"模特场景图": "模特或手持场景展示商品真实使用",
"细节说明图": "突出材质、做工和关键细节",
"卖点图": "突出核心卖点和差异化优势",
}
FIXED_CATEGORY_DESCRIPTIONS = {
"白底图": "生成 Shopee 台灣商品白底主圖,純白背景,商品清晰置中,不添加多餘文字。",
"场景图": "生成生活化使用場景圖,展示商品在真實情境中的用途與氛圍,畫面自然可信。",
"模特场景图": "生成模特或手持使用情境圖,畫面自然可信,商品為主角。",
"细节说明图": "生成商品細節特寫說明圖,突出材質、做工、接口、紋理或關鍵細節。",
"卖点图": "生成賣點詳解圖,使用繁體中文短文案呈現核心優勢,版面乾淨。",
}
DEFAULT_CATEGORY_COUNTS = OrderedDict(
(("白底图", 1), ("场景图", 2), ("卖点图", 2))
(
("白底图", 1),
("场景图", 2),
("模特场景图", 0),
("细节说明图", 0),
("卖点图", 2),
)
)
PLATFORMS = ("Shopee", "Lazada", "TikTok Shop", "Amazon")
COUNTRIES = ("中国台湾", "新加坡", "马来西亚", "菲律宾", "泰国", "越南")
@@ -23,6 +38,7 @@ LAST_SETTING_KEYS = ("platform", "country", "language", "ratio")
MAX_CATEGORY_NAME_LENGTH = 10
MAX_GENERATION_COUNT_WITHOUT_CONFIRM = 16
PRODUCT_SUITE_PLACEHOLDERS = (
"生成目标",
"套图名称",
"补充描述",
"平台",
@@ -38,9 +54,7 @@ PRODUCT_SUITE_PLACEHOLDERS = (
"价格信息规则",
"尺码信息规则",
)
PRODUCT_SUITE_REQUIRED_PLACEHOLDERS = (
"套图名称",
"补充描述",
PRODUCT_SUITE_COMMON_REQUIRED_PLACEHOLDERS = (
"图片比例",
"参考图规则",
"商品卖点与要求",
@@ -49,6 +63,15 @@ PRODUCT_SUITE_REQUIRED_PLACEHOLDERS = (
"价格信息规则",
"尺码信息规则",
)
PRODUCT_SUITE_REQUIRED_PLACEHOLDERS = (
"生成目标",
*PRODUCT_SUITE_COMMON_REQUIRED_PLACEHOLDERS,
)
PRODUCT_SUITE_LEGACY_REQUIRED_PLACEHOLDERS = (
"套图名称",
"补充描述",
*PRODUCT_SUITE_COMMON_REQUIRED_PLACEHOLDERS,
)
PRODUCT_SUITE_READ_ONLY_RULE_PLACEHOLDERS = (
"尺寸与长图规则",
"禁用内容规则",
@@ -154,8 +177,13 @@ def category_helper(category):
def category_description(category):
helper = category_helper(category)
return "," + helper if helper else ""
name = str(category or "").strip()
fixed_description = FIXED_CATEGORY_DESCRIPTIONS.get(name)
if fixed_description:
return fixed_description
if name:
return "生成自定义分类图片:%s。" % name
return "生成自定义分类图片。"
def suite_total_count(settings, image_count):
@@ -186,10 +214,20 @@ def product_suite_prompt_errors(template_text):
errors.append("模板包含未知变量:%s" % "、".join("{%s}" % name for name in unknown))
missing = [
name for name in PRODUCT_SUITE_REQUIRED_PLACEHOLDERS if name not in names
name
for name in PRODUCT_SUITE_COMMON_REQUIRED_PLACEHOLDERS
if name not in names
]
if missing:
errors.append("模板缺少必需变量:%s" % "、".join("{%s}" % name for name in missing))
has_new_target = "生成目标" in names
has_legacy_target = all(
name in names for name in ("套图名称", "补充描述")
)
if not has_new_target and not has_legacy_target:
errors.append(
"模板缺少必需变量:{生成目标}(旧模板需同时包含{套图名称}和{补充描述})"
)
invalid_rule_lines = []
for line in text.splitlines():
@@ -228,8 +266,9 @@ def product_suite_prompt_context(
item_text = "未绑定商品"
reference_index = max(1, int(source_index or 1))
return {
"生成目标": category_description(category),
"套图名称": str(category or ""),
"补充描述": category_description(category),
"补充描述": "," + category_description(category),
"平台": normalized["platform"],
"国家地区": normalized["country"],
"输出语言": normalized["language"],
@@ -255,9 +294,18 @@ def render_product_suite_prompt(template_text, context):
name: str((context or {}).get(name, ""))
for name in PRODUCT_SUITE_PLACEHOLDERS
}
context = context or {}
missing_context = [
name for name in PRODUCT_SUITE_REQUIRED_PLACEHOLDERS if name not in (context or {})
name
for name in PRODUCT_SUITE_COMMON_REQUIRED_PLACEHOLDERS
if name not in context
]
has_new_target = "生成目标" in context
has_legacy_target = all(
name in context for name in ("套图名称", "补充描述")
)
if not has_new_target and not has_legacy_target:
missing_context.append("生成目标")
if missing_context:
raise ProductSuitePromptError(
"提示词上下文缺少变量:%s"