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:
2026-09-27 19:47:51 +08:00
parent ace9a784be
commit ed7dd9381b
7 changed files with 988 additions and 191 deletions
@@ -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,