feat(LLM): 外观专利/货源多行合批 + 读超时抬到 600s,砍请求次数
- 外观专利:商标提取 10 行一批、外观检测 5 行(=5 张图)一批,一个 10 行批次
从 20 次请求降到 3 次。Gemini 支持多图,无需再逐行单独发图。
- 批量协议:用户文本用「序号<TAB>行」,响应 {"items":[{"i":n,…}]} 一律按序号回填,
不信数组位置。三道校验门(序号在批内、关键词逐字属于本行原文、状态枚举且理由非空),
缺行或失真行单发补检一次;传输失败按整批记失败、绝不逐行重发,避免劣化窗口放大成 N 倍。
- 应急开关:llm-title-batch-size / llm-appearance-batch-size 任一设为 1 即退回逐行,无需改代码。
- 货源查询:类目一级/二级合并为一次调用,两级树每批只加载一次(原来每行各问一次、共 2 次)。
- 超时:读 600s、首查 300s(外观专利与货源同步调整)。合批后单请求输出量约为原来 5 倍,
沿用按单图调出的 60s 会误杀正常慢批次、连带整批白等一轮重试。
- 口径影响:后台 LLM 使用按「次」统计,上线后次数约降到 1/6.7,与历史数据不可横向比较。
This commit is contained in:
+336
-86
@@ -31,12 +31,14 @@ import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 外观专利检测:直连 LLM(OpenAI 兼容 /v1/chat/completions),
|
||||
* 每行并发跑"商标关键词提取"与"外观侵权检测"两个请求,结果语义与原 LLM 工作流对齐。
|
||||
* 商标关键词提取与外观侵权检测各自「一批行 = 一次请求」(外观一批带多张图),
|
||||
* 结果按行序号回传,语义与原 LLM 工作流对齐。
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@@ -88,6 +90,30 @@ public class AppearancePatentLlmClient {
|
||||
+ "【判定依据】:基于上述分析,给出最终判定理由。\"\n"
|
||||
+ "}";
|
||||
|
||||
/**
|
||||
* 批量信封:追加在各自系统提示词之后,只改「一行一次」为「一批一次 + 序号回显」,
|
||||
* 提取规则与判定规则原样保留。
|
||||
*/
|
||||
private static final String TITLE_BATCH_ENVELOPE =
|
||||
"\n\n### 批量输入输出协议\n"
|
||||
+ "用户输入是若干行商品文本,每行格式为「序号<TAB>商品文本」。必须逐行独立处理,"
|
||||
+ "严禁把某一行提取到的词写到另一行上。\n"
|
||||
+ "只输出一个 JSON 对象:{\"items\":[{\"i\":1,\"keywords\":\"关键词1,关键词2\"}]}\n"
|
||||
+ "* i=该行开头的序号,每行恰好一项,不得漏行、不得凭空增加行。\n"
|
||||
+ "* keywords=按上述规则从该行文本提取的结果,多词用中文逗号拼接;该行确实没有词时输出 \"无\"。\n"
|
||||
+ "* 除 JSON 外不要输出任何内容。";
|
||||
|
||||
private static final String APPEARANCE_BATCH_ENVELOPE =
|
||||
"\n\n### 批量输入输出协议\n"
|
||||
+ "本次同时提交多件商品:用户消息先给出「序号<TAB>产品描述」清单,随后按同样的序号依次附上各件商品图片。"
|
||||
+ "必须逐件独立判定,严禁把某一件的结论写到另一件上。\n"
|
||||
+ "只输出一个 JSON 对象:{\"items\":[{\"i\":1,\"appearance_status\":\"侵权\",\"appearance_reason\":\"...\"}]}\n"
|
||||
+ "* i=该件商品的序号,每件恰好一项,不得漏项、不得凭空增加项。\n"
|
||||
+ "* appearance_status / appearance_reason=按上述规则对该件商品图片给出的判定。\n"
|
||||
+ "* 除 JSON 外不要输出任何内容。";
|
||||
|
||||
private static final String NO_KEYWORD = "无";
|
||||
|
||||
private static final Pattern STATUS_REGEX =
|
||||
Pattern.compile("\"appearance_status\"\\s*:\\s*\"([^\"]+)\"");
|
||||
private static final Pattern STATUS_ESCAPED_REGEX =
|
||||
@@ -109,8 +135,8 @@ public class AppearancePatentLlmClient {
|
||||
private volatile ExecutorService rowExecutor;
|
||||
|
||||
/**
|
||||
* 批内行级并发检测:每行并行调用商标提取与外观检测两个 LLM 请求,
|
||||
* 结果按行合并后返回(顺序与入参一致)。
|
||||
* 两阶段批量检测:先按批提取商标关键词,再只对提取成功的行按批(一批多张图)判定外观,
|
||||
* 一批 = 一次请求,结果按行序号回传(顺序与入参一致)。
|
||||
*/
|
||||
public List<AppearancePatentResultRowDto> inspectRows(List<AppearancePatentResultRowDto> rows,
|
||||
String prompt,
|
||||
@@ -122,37 +148,179 @@ public class AppearancePatentLlmClient {
|
||||
log.warn("[appearance-patent] llm api key not configured, keep raw rows size={}", rows.size());
|
||||
return rows.stream().map(this::copy).toList();
|
||||
}
|
||||
List<AppearancePatentResultRowDto> results = new ArrayList<>(rows.size());
|
||||
List<Integer> pending = new ArrayList<>(rows.size());
|
||||
for (AppearancePatentResultRowDto source : rows) {
|
||||
AppearancePatentResultRowDto row = copy(source);
|
||||
results.add(row);
|
||||
String rawTitle = normalize(row.getTitle());
|
||||
if (rawTitle.isBlank() || normalize(row.getUrl()).isBlank()) {
|
||||
applyRowFallback(row, rawTitle, firstNonBlank(rawTitle, MISSING_ROW_DATA), "");
|
||||
} else {
|
||||
pending.add(results.size() - 1);
|
||||
}
|
||||
}
|
||||
if (pending.isEmpty()) {
|
||||
return results;
|
||||
}
|
||||
Map<Integer, String> keywordsByKey = new LinkedHashMap<>();
|
||||
Map<Integer, String> titleErrors = new LinkedHashMap<>();
|
||||
pass(pending, Math.max(1, properties.getLlmTitleBatchSize()),
|
||||
chunk -> invokeTitleBatchSafely(rows, chunk, apiKey),
|
||||
keywordsByKey, titleErrors, "商标识别结果缺失", true);
|
||||
List<Integer> appearancePending = new ArrayList<>(pending.size());
|
||||
for (int index : pending) {
|
||||
if (keywordsByKey.containsKey(index)) {
|
||||
appearancePending.add(index);
|
||||
continue;
|
||||
}
|
||||
// 标题识别失败时本行必走回退分支(外观结果会被丢弃),再发外观请求只会在劣化中的上游白等一轮重试
|
||||
log.warn("[appearance-patent] 标题识别失败,跳过外观识别(本行同样走回退)asin={}", rows.get(index).getAsin());
|
||||
applyRowFallback(results.get(index), normalize(rows.get(index).getTitle()),
|
||||
firstNonBlank(titleErrors.get(index), "商标识别失败"), "");
|
||||
}
|
||||
Map<Integer, AppearanceJudgement> judgements = new LinkedHashMap<>();
|
||||
Map<Integer, String> appearanceErrors = new LinkedHashMap<>();
|
||||
pass(appearancePending, Math.max(1, properties.getLlmAppearanceBatchSize()),
|
||||
chunk -> invokeAppearanceBatchSafely(rows, chunk, prompt, apiKey),
|
||||
judgements, appearanceErrors, "外观识别结果缺失", true);
|
||||
for (int index : appearancePending) {
|
||||
AppearancePatentResultRowDto source = rows.get(index);
|
||||
String rawTitle = normalize(source.getTitle());
|
||||
AppearanceJudgement judgement = judgements.get(index);
|
||||
if (judgement == null) {
|
||||
applyRowFallback(results.get(index), rawTitle, firstNonBlank(rawTitle, MISSING_ROW_DATA),
|
||||
firstNonBlank(appearanceErrors.get(index), "外观识别失败"));
|
||||
continue;
|
||||
}
|
||||
String keywords = keywordsByKey.get(index);
|
||||
applyResult(results.get(index), new LlmResult(
|
||||
source.getGroupKey(), source.getId(), source.getAsin(), source.getCountry(),
|
||||
keywords, judgement.status(), "", "", "", keywords, judgement.reason(), "", ""));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private BatchOutcome<String> invokeTitleBatchSafely(List<AppearancePatentResultRowDto> rows,
|
||||
List<Integer> chunk,
|
||||
String apiKey) {
|
||||
try {
|
||||
return new BatchOutcome<>(invokeTitleBatch(rows, chunk, apiKey), null);
|
||||
} catch (Exception ex) {
|
||||
String message = failureMessage(ex);
|
||||
log.warn("[appearance-patent] title batch failed rows={} firstAsin={} err={}",
|
||||
chunk.size(), rows.get(chunk.get(0)).getAsin(), message);
|
||||
return new BatchOutcome<>(Map.of(), message);
|
||||
}
|
||||
}
|
||||
|
||||
private BatchOutcome<AppearanceJudgement> invokeAppearanceBatchSafely(List<AppearancePatentResultRowDto> rows,
|
||||
List<Integer> chunk,
|
||||
String prompt,
|
||||
String apiKey) {
|
||||
try {
|
||||
return new BatchOutcome<>(invokeAppearanceBatch(rows, chunk, prompt, apiKey), null);
|
||||
} catch (Exception ex) {
|
||||
String message = failureMessage(ex);
|
||||
log.warn("[appearance-patent] appearance batch failed rows={} firstAsin={} err={}",
|
||||
chunk.size(), rows.get(chunk.get(0)).getAsin(), message);
|
||||
return new BatchOutcome<>(Map.of(), message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 阶段调度:切批并发发请求 → 漏行/失真行单发补检一次 → 仍缺失的行记失败。
|
||||
* 传输失败按整批记失败,绝不逐行重发(一次请求带走一批,劣化窗口下不会放大成 N 倍);
|
||||
* 补检请求不再套校验门,既保留今天对模型自身过度提取的容忍,也杜绝无限重做。
|
||||
*/
|
||||
private <T> void pass(List<Integer> indexes, int batchSize, BatchInvoker<T> invoker,
|
||||
Map<Integer, T> resolved, Map<Integer, String> errors,
|
||||
String missingMessage, boolean allowRedo) {
|
||||
if (indexes.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
List<List<Integer>> chunks = partition(indexes, batchSize);
|
||||
List<BatchOutcome<T>> outcomes = runBatches(chunks, invoker);
|
||||
List<Integer> missing = new ArrayList<>();
|
||||
for (int c = 0; c < chunks.size(); c++) {
|
||||
List<Integer> chunk = chunks.get(c);
|
||||
BatchOutcome<T> outcome = outcomes.get(c);
|
||||
if (outcome.transportError() != null) {
|
||||
for (int index : chunk) {
|
||||
errors.put(index, outcome.transportError());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
for (int index : chunk) {
|
||||
T value = outcome.byIndex().get(index);
|
||||
if (value == null) {
|
||||
missing.add(index);
|
||||
} else {
|
||||
resolved.put(index, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (missing.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (allowRedo) {
|
||||
pass(missing, 1, invoker, resolved, errors, missingMessage, false);
|
||||
}
|
||||
for (int index : missing) {
|
||||
if (!resolved.containsKey(index)) {
|
||||
errors.put(index, firstNonBlank(errors.get(index), missingMessage));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private interface BatchInvoker<T> {
|
||||
BatchOutcome<T> invoke(List<Integer> chunk);
|
||||
}
|
||||
|
||||
private record BatchOutcome<T>(Map<Integer, T> byIndex, String transportError) {
|
||||
}
|
||||
|
||||
private static List<List<Integer>> partition(List<Integer> indexes, int batchSize) {
|
||||
List<List<Integer>> chunks = new ArrayList<>();
|
||||
for (int from = 0; from < indexes.size(); from += batchSize) {
|
||||
chunks.add(new ArrayList<>(indexes.subList(from, Math.min(from + batchSize, indexes.size()))));
|
||||
}
|
||||
return chunks;
|
||||
}
|
||||
|
||||
private <T> List<BatchOutcome<T>> runBatches(List<List<Integer>> chunks, BatchInvoker<T> invoker) {
|
||||
Semaphore concurrency = new Semaphore(Math.max(1, properties.getLlmRowConcurrency()));
|
||||
ExecutorService executor = rowExecutor();
|
||||
// 行级处理跑在 worker 线程:把「当前任务归属用户」快照带过去,供 LLM 请求计次
|
||||
// 批次跑在 worker 线程:把「当前任务归属用户」快照带过去,供 LLM 请求计次
|
||||
SecretUsageContext.Context usageContext = SecretUsageContext.snapshot();
|
||||
List<CompletableFuture<AppearancePatentResultRowDto>> futures = new ArrayList<>(rows.size());
|
||||
for (AppearancePatentResultRowDto row : rows) {
|
||||
futures.add(CompletableFuture.supplyAsync(SecretUsageContext.wrap(() -> {
|
||||
List<CompletableFuture<BatchOutcome<T>>> futures = new ArrayList<>(chunks.size());
|
||||
for (List<Integer> chunk : chunks) {
|
||||
Supplier<BatchOutcome<T>> task = () -> {
|
||||
try {
|
||||
concurrency.acquire();
|
||||
try {
|
||||
return inspectRow(row, prompt, apiKey);
|
||||
return invoker.invoke(chunk);
|
||||
} finally {
|
||||
concurrency.release();
|
||||
}
|
||||
} catch (InterruptedException interruptedException) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("LLM row inspect interrupted");
|
||||
return new BatchOutcome<T>(Map.of(), "LLM 批次等待被中断");
|
||||
}
|
||||
}, usageContext), executor));
|
||||
};
|
||||
futures.add(CompletableFuture.supplyAsync(SecretUsageContext.wrap(task, usageContext), executor));
|
||||
}
|
||||
List<AppearancePatentResultRowDto> merged = new ArrayList<>(rows.size());
|
||||
List<BatchOutcome<T>> outcomes = new ArrayList<>(chunks.size());
|
||||
for (int i = 0; i < futures.size(); i++) {
|
||||
try {
|
||||
merged.add(futures.get(i).join());
|
||||
outcomes.add(futures.get(i).join());
|
||||
} catch (Exception ex) {
|
||||
log.warn("[appearance-patent] llm row failed index={} asin={} err={}",
|
||||
i, rows.get(i).getAsin(), failureMessage(ex));
|
||||
merged.add(markFailed(copy(rows.get(i)), failureMessage(ex)));
|
||||
log.warn("[appearance-patent] llm batch crashed rows={} err={}",
|
||||
chunks.get(i).size(), failureMessage(ex));
|
||||
outcomes.add(new BatchOutcome<T>(Map.of(), failureMessage(ex)));
|
||||
}
|
||||
}
|
||||
return merged;
|
||||
return outcomes;
|
||||
}
|
||||
|
||||
public List<AppearancePatentResultRowDto> markRowsFailed(List<AppearancePatentResultRowDto> rows, String failureMessage) {
|
||||
@@ -170,68 +338,9 @@ public class AppearancePatentLlmClient {
|
||||
}
|
||||
|
||||
/**
|
||||
* 单行检测:与原 LLM 工作流语义对齐。
|
||||
* 任一 LLM 失败或数据缺失时走工作流"默认值"分支:appearance=外观识别异常、
|
||||
* 行级回退分支:与原 LLM 工作流"默认值"语义对齐——appearance=外观识别异常、
|
||||
* title 保留原始标题、title_reason/appearance_reason 填充对应错误信息。
|
||||
*/
|
||||
private AppearancePatentResultRowDto inspectRow(AppearancePatentResultRowDto row, String prompt, String apiKey) {
|
||||
AppearancePatentResultRowDto resultRow = copy(row);
|
||||
String rawTitle = normalize(row.getTitle());
|
||||
if (rawTitle.isBlank() || normalize(row.getUrl()).isBlank()) {
|
||||
return applyRowFallback(resultRow, rawTitle,
|
||||
firstNonBlank(rawTitle, MISSING_ROW_DATA), "");
|
||||
}
|
||||
String titleKeywords = "";
|
||||
String titleError = "";
|
||||
boolean titleFailed = false;
|
||||
try {
|
||||
titleKeywords = invokeTitle(row, apiKey);
|
||||
} catch (Exception ex) {
|
||||
titleFailed = true;
|
||||
titleError = firstNonBlank(ex.getMessage(), "商标识别失败");
|
||||
log.warn("[appearance-patent] title llm failed asin={} title={} err={}",
|
||||
row.getAsin(), abbreviate(row.getTitle(), 120), failureMessage(ex));
|
||||
}
|
||||
String appearanceStatus = "";
|
||||
String appearanceReason = "";
|
||||
boolean appearanceFailed = false;
|
||||
if (titleFailed) {
|
||||
// 标题识别失败时本行必走回退分支(外观结果会被丢弃),再发外观请求只会在劣化中的上游白等一轮重试
|
||||
log.warn("[appearance-patent] 标题识别失败,跳过外观识别(本行同样走回退)asin={}", row.getAsin());
|
||||
} else {
|
||||
try {
|
||||
AppearanceJudgement judgement = invokeAppearance(row, prompt, apiKey);
|
||||
appearanceStatus = judgement.status();
|
||||
appearanceReason = judgement.reason();
|
||||
} catch (Exception ex) {
|
||||
appearanceFailed = true;
|
||||
appearanceReason = firstNonBlank(ex.getMessage(), "外观识别失败");
|
||||
log.warn("[appearance-patent] appearance llm failed asin={} title={} url={} err={}",
|
||||
row.getAsin(), abbreviate(row.getTitle(), 120), abbreviate(row.getUrl(), 120), failureMessage(ex));
|
||||
}
|
||||
}
|
||||
if (titleFailed || appearanceFailed) {
|
||||
String titleReason = titleFailed ? titleError : firstNonBlank(rawTitle, MISSING_ROW_DATA);
|
||||
return applyRowFallback(resultRow, rawTitle, titleReason, appearanceFailed ? appearanceReason : "");
|
||||
}
|
||||
LlmResult result = new LlmResult(
|
||||
row.getGroupKey(),
|
||||
row.getId(),
|
||||
row.getAsin(),
|
||||
row.getCountry(),
|
||||
titleKeywords,
|
||||
appearanceStatus,
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
titleKeywords,
|
||||
appearanceReason,
|
||||
"",
|
||||
"");
|
||||
applyResult(resultRow, result);
|
||||
return resultRow;
|
||||
}
|
||||
|
||||
private AppearancePatentResultRowDto applyRowFallback(AppearancePatentResultRowDto row,
|
||||
String title,
|
||||
String titleReason,
|
||||
@@ -254,22 +363,163 @@ public class AppearancePatentLlmClient {
|
||||
return row;
|
||||
}
|
||||
|
||||
private String invokeTitle(AppearancePatentResultRowDto row, String apiKey) throws Exception {
|
||||
String userText = joinNonBlank(row.getTitle(), sanitizeSku(row.getSku()), ",");
|
||||
String content = invokeChat(properties.getTitleModel(), TITLE_SYSTEM_PROMPT, userText,
|
||||
List.of(), apiKey, "text");
|
||||
return cleanTitle(content);
|
||||
/** 一批行合并为一次文本请求;返回的行下标 → 关键词,仅包含通过字面校验的行。 */
|
||||
private Map<Integer, String> invokeTitleBatch(List<AppearancePatentResultRowDto> rows,
|
||||
List<Integer> chunk,
|
||||
String apiKey) throws Exception {
|
||||
List<String> lineTexts = new ArrayList<>(chunk.size());
|
||||
for (int index : chunk) {
|
||||
lineTexts.add(singleLine(joinNonBlank(rows.get(index).getTitle(), sanitizeSku(rows.get(index).getSku()), ",")));
|
||||
}
|
||||
String userText = "下面是 " + chunk.size() + " 行商品文本,每行格式为「序号<TAB>商品文本」,逐行提取:\n"
|
||||
+ indexedLines(lineTexts);
|
||||
String content = invokeChat(properties.getTitleModel(), TITLE_SYSTEM_PROMPT + TITLE_BATCH_ENVELOPE,
|
||||
userText, List.of(), apiKey, "json_object");
|
||||
Map<Integer, String> keywordsByKey = new LinkedHashMap<>();
|
||||
for (Map.Entry<Integer, JsonNode> entry : parseIndexedItems(content).entrySet()) {
|
||||
int position = entry.getKey() - 1;
|
||||
if (position < 0 || position >= chunk.size()) {
|
||||
continue;
|
||||
}
|
||||
String keywords = text(entry.getValue().path("keywords"));
|
||||
if (keywords == null) {
|
||||
continue;
|
||||
}
|
||||
keywords = cleanTitle(keywords);
|
||||
if (chunk.size() > 1 && !keywordsBelongToLine(keywords, lineTexts.get(position))) {
|
||||
log.warn("[appearance-patent] 批量提取结果与该行文本不符,按缺失处理并单行补检 asin={} keywords={}",
|
||||
rows.get(chunk.get(position)).getAsin(), abbreviate(keywords, 80));
|
||||
continue;
|
||||
}
|
||||
keywordsByKey.put(chunk.get(position), keywords);
|
||||
}
|
||||
return keywordsByKey;
|
||||
}
|
||||
|
||||
private AppearanceJudgement invokeAppearance(AppearancePatentResultRowDto row, String prompt, String apiKey) throws Exception {
|
||||
String userText = "产品描述:" + nonBlank(row.getTitle(), "") + nonBlank(row.getSku(), "");
|
||||
/** 一批行合并为一次多图请求;返回的行下标 → 判定,仅包含状态合法且理由非空的行。 */
|
||||
private Map<Integer, AppearanceJudgement> invokeAppearanceBatch(List<AppearancePatentResultRowDto> rows,
|
||||
List<Integer> chunk,
|
||||
String prompt,
|
||||
String apiKey) throws Exception {
|
||||
List<String> lineTexts = new ArrayList<>(chunk.size());
|
||||
List<String> images = new ArrayList<>(chunk.size());
|
||||
for (int index : chunk) {
|
||||
AppearancePatentResultRowDto row = rows.get(index);
|
||||
lineTexts.add(singleLine("产品描述:" + nonBlank(row.getTitle(), "") + nonBlank(row.getSku(), "")));
|
||||
images.add(row.getUrl());
|
||||
}
|
||||
String userText = "本次提交 " + chunk.size() + " 件商品,先给出「序号<TAB>产品描述」清单,"
|
||||
+ "随后按同样的序号依次附上各件商品图片:\n" + indexedLines(lineTexts);
|
||||
String content = invokeChat(properties.getAppearanceModel(), appearanceSystemPrompt(prompt),
|
||||
userText, images, apiKey, "json_object");
|
||||
Map<Integer, AppearanceJudgement> judgements = new LinkedHashMap<>();
|
||||
for (Map.Entry<Integer, JsonNode> entry : parseIndexedItems(content).entrySet()) {
|
||||
int position = entry.getKey() - 1;
|
||||
if (position < 0 || position >= chunk.size()) {
|
||||
continue;
|
||||
}
|
||||
AppearanceJudgement judgement = itemJudgement(entry.getValue());
|
||||
if (chunk.size() > 1 && !isUsableJudgement(judgement)) {
|
||||
log.warn("[appearance-patent] 批量外观结果缺状态或理由,按缺失处理并单行补检 asin={} status={}",
|
||||
rows.get(chunk.get(position)).getAsin(), abbreviate(judgement.status(), 40));
|
||||
continue;
|
||||
}
|
||||
judgements.put(chunk.get(position), judgement);
|
||||
}
|
||||
return judgements;
|
||||
}
|
||||
|
||||
private String appearanceSystemPrompt(String prompt) {
|
||||
String system = APPEARANCE_SYSTEM_PROMPT;
|
||||
if (prompt != null && !prompt.isBlank()) {
|
||||
system = APPEARANCE_SYSTEM_PROMPT + "\n\n额外要求:" + prompt.trim();
|
||||
}
|
||||
String content = invokeChat(properties.getAppearanceModel(), system, userText,
|
||||
List.of(row.getUrl()), apiKey, "json_object");
|
||||
return parseAppearanceJudgement(content);
|
||||
return system + APPEARANCE_BATCH_ENVELOPE;
|
||||
}
|
||||
|
||||
private String indexedLines(List<String> lineTexts) {
|
||||
StringBuilder text = new StringBuilder();
|
||||
for (int position = 0; position < lineTexts.size(); position++) {
|
||||
text.append(position + 1).append('\t').append(lineTexts.get(position)).append('\n');
|
||||
}
|
||||
return text.toString();
|
||||
}
|
||||
|
||||
/** 行协议靠换行分隔,行内不能再有换行或制表符。 */
|
||||
private String singleLine(String value) {
|
||||
return normalize(value).replaceAll("[\\r\\n\\t]+", " ").trim();
|
||||
}
|
||||
|
||||
/** 关键词必须逐字出现在该行原文里:批量协议下最怕的是把别行的词安到本行。 */
|
||||
private boolean keywordsBelongToLine(String keywords, String lineText) {
|
||||
String normalized = normalize(keywords);
|
||||
if (normalized.isBlank() || NO_KEYWORD.equals(normalized)) {
|
||||
return true;
|
||||
}
|
||||
String haystack = lineText.toLowerCase(Locale.ROOT);
|
||||
for (String token : normalized.split("[,,、;;\\s]+")) {
|
||||
String word = token.toLowerCase(Locale.ROOT);
|
||||
if (word.isBlank() || NO_KEYWORD.equals(word)) {
|
||||
continue;
|
||||
}
|
||||
if (!haystack.contains(word)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isUsableJudgement(AppearanceJudgement judgement) {
|
||||
String status = normalize(judgement.status());
|
||||
return (INFRINGEMENT.equals(status) || NO_INFRINGEMENT.equals(status))
|
||||
&& !normalize(judgement.reason()).isBlank();
|
||||
}
|
||||
|
||||
/** 批量响应解包:序号 → 条目,按序号对账而不是按数组位置(模型可能漏项或乱序)。 */
|
||||
private Map<Integer, JsonNode> parseIndexedItems(String content) {
|
||||
JsonNode root = readBatchRoot(content);
|
||||
JsonNode items = root.path("items");
|
||||
if (!items.isArray()) {
|
||||
throw new IllegalStateException("LLM 批量响应缺少 items 数组");
|
||||
}
|
||||
Map<Integer, JsonNode> byIndex = new LinkedHashMap<>();
|
||||
for (JsonNode item : items) {
|
||||
int position = item.path("i").asInt(0);
|
||||
if (position > 0) {
|
||||
byIndex.putIfAbsent(position, item);
|
||||
}
|
||||
}
|
||||
return byIndex;
|
||||
}
|
||||
|
||||
private JsonNode readBatchRoot(String content) {
|
||||
String cleaned = content == null ? "" : content.trim();
|
||||
if (cleaned.startsWith("```")) {
|
||||
int bodyStart = cleaned.indexOf('\n');
|
||||
if (bodyStart > 0) {
|
||||
cleaned = cleaned.substring(bodyStart + 1);
|
||||
}
|
||||
int fenceEnd = cleaned.lastIndexOf("```");
|
||||
if (fenceEnd >= 0) {
|
||||
cleaned = cleaned.substring(0, fenceEnd);
|
||||
}
|
||||
cleaned = cleaned.trim();
|
||||
}
|
||||
try {
|
||||
return objectMapper.readTree(cleaned);
|
||||
} catch (Exception ignored) {
|
||||
return parseJsonOrThrow(cleaned.replace("\n", "\\n").replace("\r", "\\r"));
|
||||
}
|
||||
}
|
||||
|
||||
/** 单个条目的判定:字段齐则直取,缺字段或为空时退回既有的整串解包 + 正则兜底。 */
|
||||
private AppearanceJudgement itemJudgement(JsonNode item) {
|
||||
String status = text(item.path("appearance_status"));
|
||||
String reason = text(item.path("appearance_reason"));
|
||||
if (normalize(status).isBlank() || normalize(reason).isBlank()) {
|
||||
return parseAppearanceJudgement(writeJson(item));
|
||||
}
|
||||
return new AppearanceJudgement(status, reason);
|
||||
}
|
||||
|
||||
private String invokeChat(String model,
|
||||
|
||||
+134
-68
@@ -22,7 +22,9 @@ import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
@@ -39,8 +41,8 @@ import java.util.concurrent.Semaphore;
|
||||
* (upload_file 插件,失败即行失败);
|
||||
* 2. category_switch=false:不调 LLM,输出行仅 asin + 三图,主图空时 status=不存在
|
||||
* (127486/1857081);
|
||||
* 3. category_switch=true:一级匹配(141783)→ 二级匹配(1548215)→ 三级候选查询
|
||||
* (1288150),任一匹配为"无"则违规候选为空;
|
||||
* 3. category_switch=true:整批加载一次类目树,一次调用同时判定一级+二级(合并 141783/1548215),
|
||||
* 三级违规候选由 Java 按二级 ID 从内存树直接取(1288150),任一层为"无"则候选为空;
|
||||
* 4. 合规检查(1570640,总是执行):is_conform/reason/category;
|
||||
* 5. 不符合或 alibaba 空或 img_switch=false:只回填合规结果,status=成功(139255/1398750);
|
||||
* 6. 否则图片对比循环(181250,最多 2 个候选):主图(原始url) vs 拼图 → LLM
|
||||
@@ -57,19 +59,22 @@ public class SimilarAsinLlmService {
|
||||
private static final String NO_MATCH = "无";
|
||||
|
||||
/**
|
||||
* 一级/二级类目匹配系统提示词(工作流 109409/1544733 原文,ID 字段移除:
|
||||
* LLM 会编造 slug ID,Java 按名称回查类目表取真实数字 ID)。
|
||||
* 一级/二级类目合并匹配系统提示词(工作流 109409/1544733 两次调用合一):
|
||||
* 候选以「一级 → 其下二级」的两层清单一次给出,ID 字段仍不进输出契约
|
||||
* (LLM 会编造 slug ID,Java 按名称在候选内回查真实数字 ID)。
|
||||
*/
|
||||
private static final String SYSTEM_CATEGORY =
|
||||
"# Role\n你是一个电商数据专家,擅长根据商品的标题(Title)和属性(Attributes)进行精准的类目归类。\n\n"
|
||||
+ "# Task\n请分析用户提供的\"商品标题\"和\"商品属性\",从给定的\"备选类目列表\"中筛选出最匹配的一个类目。\n\n"
|
||||
+ "# Task\n从给定的\"备选类目列表\"(一级类目 + 其下二级类目)中,一次判定出该商品最匹配的**一级**与**二级**类目。\n\n"
|
||||
+ "# Rules\n1. **语义匹配**:不仅要考虑关键词匹配,还要考虑商品的实际用途、材质和适用人群。\n"
|
||||
+ "2. **属性优先**:如果标题模糊,请重点参考属性中的关键信息(如:材质、功能、品牌)。\n"
|
||||
+ "3. **唯一输出**:只输出 JSON 格式的结果,不要包含任何解释、开场白或修饰词。\n"
|
||||
+ "4. **回退机制**:\n"
|
||||
+ " - 如果能匹配到类目,输出:{\"name\": \"类目名称\"}\n"
|
||||
+ " - 如果没有任何类目符合商品特征,输出:{\"name\": \"无\"}\n\n"
|
||||
+ "# Constraint\n严禁伪造类目名称。输出必须严格遵循 JSON 语法。";
|
||||
+ "3. **层级约束**:second 必须是 first 那一行下面列出的二级类目,禁止跨一级借用同名二级。\n"
|
||||
+ "4. **唯一输出**:只输出 JSON 格式的结果,不要包含任何解释、开场白或修饰词。\n"
|
||||
+ "5. **回退机制**(两层各自独立判定):\n"
|
||||
+ " - 两层都能匹配:{\"first\": \"一级名称\", \"second\": \"二级名称\"}\n"
|
||||
+ " - 只匹配到一级:{\"first\": \"一级名称\", \"second\": \"无\"}\n"
|
||||
+ " - 一级都没有:{\"first\": \"无\", \"second\": \"无\"}\n\n"
|
||||
+ "# Constraint\n严禁伪造类目名称,first/second 必须逐字来自候选列表。输出必须严格遵循 JSON 语法。";
|
||||
|
||||
/** 合规检查系统提示词(工作流 1380531 原文)。 */
|
||||
private static final String SYSTEM_CONFORM =
|
||||
@@ -188,6 +193,8 @@ public class SimilarAsinLlmService {
|
||||
}
|
||||
Semaphore concurrency = new Semaphore(Math.max(1, properties.getLlmRowConcurrency()));
|
||||
ExecutorService executor = rowExecutor();
|
||||
// 类目树整批加载一次供本批所有行复用:逐行现拼两层要 27 次 SQL/行,比省下的那次 LLM 调用更贵。
|
||||
CategoryTree categoryTree = categorySwitch ? CategoryTree.load(productCategoryService.list()) : null;
|
||||
// 行级处理跑在 worker 线程:把「当前任务归属用户」快照带过去,供 LLM 请求计次
|
||||
SecretUsageContext.Context usageContext = SecretUsageContext.snapshot();
|
||||
List<CompletableFuture<SimilarAsinResultRowDto>> futures = new ArrayList<>(rows.size());
|
||||
@@ -196,7 +203,7 @@ public class SimilarAsinLlmService {
|
||||
try {
|
||||
concurrency.acquire();
|
||||
try {
|
||||
return inspectRow(row, prompt, apiKey, imgSwitch, categorySwitch);
|
||||
return inspectRow(row, prompt, apiKey, imgSwitch, categorySwitch, categoryTree);
|
||||
} finally {
|
||||
concurrency.release();
|
||||
}
|
||||
@@ -223,10 +230,11 @@ public class SimilarAsinLlmService {
|
||||
String prompt,
|
||||
String apiKey,
|
||||
boolean imgSwitch,
|
||||
boolean categorySwitch) {
|
||||
boolean categorySwitch,
|
||||
CategoryTree categoryTree) {
|
||||
SimilarAsinResultRowDto row = copyRow(source);
|
||||
try {
|
||||
inspectRowInternal(row, prompt, apiKey, imgSwitch, categorySwitch);
|
||||
inspectRowInternal(row, prompt, apiKey, imgSwitch, categorySwitch, categoryTree);
|
||||
return row;
|
||||
} catch (Exception ex) {
|
||||
log.warn("[similar-asin][llm] row failed asin={} title={} err={}",
|
||||
@@ -239,7 +247,8 @@ public class SimilarAsinLlmService {
|
||||
String prompt,
|
||||
String apiKey,
|
||||
boolean imgSwitch,
|
||||
boolean categorySwitch) throws Exception {
|
||||
boolean categorySwitch,
|
||||
CategoryTree categoryTree) throws Exception {
|
||||
// --- 1. 图片准备(工作流 103226:无条件执行)---
|
||||
prepareImages(row, apiKey);
|
||||
|
||||
@@ -251,21 +260,13 @@ public class SimilarAsinLlmService {
|
||||
return;
|
||||
}
|
||||
|
||||
// --- 2. 类目匹配:一级 → 二级 → 三级违规候选(141783/1548215/1288150)---
|
||||
CategoryLevel first = matchCategoryLevel(row, null, apiKey);
|
||||
List<CategoryLevel> thirdCandidates;
|
||||
CategoryLevel second;
|
||||
if (isNoMatch(first)) {
|
||||
second = CategoryLevel.none();
|
||||
thirdCandidates = List.of();
|
||||
} else {
|
||||
second = matchCategoryLevel(row, first, apiKey);
|
||||
if (isNoMatch(second)) {
|
||||
thirdCandidates = List.of();
|
||||
} else {
|
||||
thirdCandidates = childrenOf(second.id());
|
||||
}
|
||||
}
|
||||
// --- 2. 类目匹配:一次调用出一级+二级,三级候选走内存树(141783/1548215/1288150)---
|
||||
CategoryPath path = matchCategoryPath(row, categoryTree, apiKey);
|
||||
CategoryLevel first = path.first();
|
||||
CategoryLevel second = path.second();
|
||||
List<CategoryLevel> thirdCandidates = isNoMatch(second)
|
||||
? List.of()
|
||||
: categoryTree.childrenOf(second.id());
|
||||
|
||||
// --- 3. 合规检查(1570640:总是执行)---
|
||||
boolean conform = checkConform(row, first, second, thirdCandidates, prompt, apiKey);
|
||||
@@ -341,28 +342,32 @@ public class SimilarAsinLlmService {
|
||||
}
|
||||
}
|
||||
|
||||
private CategoryLevel matchCategoryLevel(SimilarAsinResultRowDto row,
|
||||
CategoryLevel parent,
|
||||
String apiKey) {
|
||||
List<CategoryLevel> candidates = parent == null
|
||||
? childrenOf(null)
|
||||
: childrenOf(parent.id());
|
||||
private CategoryPath matchCategoryPath(SimilarAsinResultRowDto row, CategoryTree tree, String apiKey) {
|
||||
String userText = String.format(USER_TEXT_CATEGORY,
|
||||
firstNonBlank(row.getTitle(), row.getAsin()),
|
||||
normalize(row.getSku()),
|
||||
formatCandidates(candidates));
|
||||
tree.formatTwoLevels());
|
||||
String content = llmClient.invokeChat(properties.getLlmCategoryModel(), SYSTEM_CATEGORY, userText, apiKey);
|
||||
JsonNode root = llmClient.parseJsonContent(content);
|
||||
String name = textOrEmpty(root, "name");
|
||||
if (isNoMatch(new CategoryLevel("", name))) {
|
||||
return CategoryLevel.none();
|
||||
CategoryLevel first = resolveLevel(tree.roots(), textOrEmpty(root, "first"), "一级");
|
||||
if (first == null) {
|
||||
return CategoryPath.none();
|
||||
}
|
||||
// LLM 只输出名称,ID 按名称在候选列表内回查(防 LLM 编造不存在的 ID)。
|
||||
CategoryLevel matched = findByName(candidates, name);
|
||||
// 二级只在所选一级的 children 内回查:跨分支同名二级会挂错父节点,三级候选随之错位。
|
||||
CategoryLevel second = resolveLevel(tree.childrenOf(first.id()), textOrEmpty(root, "second"), "二级");
|
||||
return new CategoryPath(first, second == null ? CategoryLevel.none() : second);
|
||||
}
|
||||
|
||||
/** 按名称在给定候选内回查层级;"无"/空/编造名称一律返回 null,交由上层降级。 */
|
||||
private CategoryLevel resolveLevel(List<CategoryLevel> candidates, String name, String levelLabel) {
|
||||
String normalized = normalize(name);
|
||||
if (normalized.isBlank() || NO_MATCH.equals(normalized)) {
|
||||
return null;
|
||||
}
|
||||
CategoryLevel matched = findByName(candidates, normalized);
|
||||
if (matched == null) {
|
||||
log.warn("[similar-asin][llm] category name not found in candidates name={} candidates={}",
|
||||
name, candidates.size());
|
||||
return CategoryLevel.none();
|
||||
log.warn("[similar-asin][llm] category {} name not found in candidates name={} candidates={}",
|
||||
levelLabel, abbreviate(normalized, 80), candidates.size());
|
||||
}
|
||||
return matched;
|
||||
}
|
||||
@@ -469,30 +474,6 @@ public class SimilarAsinLlmService {
|
||||
}
|
||||
}
|
||||
|
||||
private List<CategoryLevel> childrenOf(String parentId) {
|
||||
Long parsedId = null;
|
||||
if (parentId != null && !parentId.isBlank()) {
|
||||
try {
|
||||
parsedId = Long.parseLong(parentId.trim());
|
||||
} catch (NumberFormatException ex) {
|
||||
log.warn("[similar-asin][llm] category parent id not a number id={}", parentId);
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
ProductCategoryListVo vo = productCategoryService.children(parsedId, 1, 100);
|
||||
List<CategoryLevel> result = new ArrayList<>();
|
||||
if (vo == null || vo.getItems() == null) {
|
||||
return result;
|
||||
}
|
||||
for (ProductCategoryItemVo item : vo.getItems()) {
|
||||
if (item == null || item.getId() == null) {
|
||||
continue;
|
||||
}
|
||||
result.add(new CategoryLevel(String.valueOf(item.getId()), firstNonBlank(item.getName(), "")));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private String formatCandidates(List<CategoryLevel> candidates) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (CategoryLevel candidate : candidates) {
|
||||
@@ -698,4 +679,89 @@ public class SimilarAsinLlmService {
|
||||
return new CategoryLevel("", NO_MATCH);
|
||||
}
|
||||
}
|
||||
|
||||
/** 合并类目调用的判定结果:任一层的名称回查失败即降级为"无",不连带影响另一层。 */
|
||||
private record CategoryPath(CategoryLevel first, CategoryLevel second) {
|
||||
static CategoryPath none() {
|
||||
return new CategoryPath(CategoryLevel.none(), CategoryLevel.none());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 每批加载一次的类目树(list() 一次全表,按 parentId 建索引):
|
||||
* 合并调用要一次看到两层候选,三级也要按二级 ID 取,逐行现拼会退化成每行几十条 SQL。
|
||||
*/
|
||||
private static final class CategoryTree {
|
||||
private final Map<Long, List<CategoryLevel>> byParent = new HashMap<>();
|
||||
|
||||
static CategoryTree load(ProductCategoryListVo vo) {
|
||||
CategoryTree tree = new CategoryTree();
|
||||
if (vo == null || vo.getItems() == null) {
|
||||
return tree;
|
||||
}
|
||||
for (ProductCategoryItemVo item : vo.getItems()) {
|
||||
if (item == null || item.getId() == null) {
|
||||
continue;
|
||||
}
|
||||
tree.byParent.computeIfAbsent(item.getParentId(), key -> new ArrayList<>())
|
||||
.add(new CategoryLevel(String.valueOf(item.getId()),
|
||||
item.getName() == null ? "" : item.getName().trim()));
|
||||
}
|
||||
return tree;
|
||||
}
|
||||
|
||||
List<CategoryLevel> roots() {
|
||||
return childrenOf((Long) null);
|
||||
}
|
||||
|
||||
/** parentId 空取一级;未登记返回空列表。list() 已按 sortOrder 排序,入树即保持展示顺序。 */
|
||||
List<CategoryLevel> childrenOf(String parentId) {
|
||||
if (parentId == null || parentId.isBlank()) {
|
||||
return childrenOf((Long) null);
|
||||
}
|
||||
try {
|
||||
return childrenOf(Long.parseLong(parentId.trim()));
|
||||
} catch (NumberFormatException ex) {
|
||||
log.warn("[similar-asin][llm] category parent id not a number id={}", parentId);
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
private List<CategoryLevel> childrenOf(Long parentId) {
|
||||
return byParent.getOrDefault(parentId, List.of());
|
||||
}
|
||||
|
||||
/** 两层候选清单:一级带 ID 单独成行,其下二级只列名称(输出契约按名回查,ID 不进 prompt)。 */
|
||||
String formatTwoLevels() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (CategoryLevel first : childrenOf((Long) null)) {
|
||||
if (first.name().isBlank()) {
|
||||
continue;
|
||||
}
|
||||
if (sb.length() > 0) {
|
||||
sb.append('\n');
|
||||
}
|
||||
sb.append(first.name()).append("(ID:").append(first.id()).append(")");
|
||||
String seconds = joinNames(childrenOf(first.id()));
|
||||
if (!seconds.isEmpty()) {
|
||||
sb.append(":").append(seconds);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static String joinNames(List<CategoryLevel> levels) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (CategoryLevel level : levels) {
|
||||
if (level.name().isBlank()) {
|
||||
continue;
|
||||
}
|
||||
if (sb.length() > 0) {
|
||||
sb.append('、');
|
||||
}
|
||||
sb.append(level.name());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user