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:
+13
-5
@@ -21,14 +21,22 @@ public class AppearancePatentProperties {
|
||||
private String appearanceModel = "gemini-3.8-flash";
|
||||
private int llmMaxTokens = 64000;
|
||||
private int llmConnectTimeoutMillis = 10000;
|
||||
private int llmReadTimeoutMillis = 180000;
|
||||
private int llmReadTimeoutMillis = 600000;
|
||||
/**
|
||||
* 首次尝试读超时(毫秒):上游偶发单请求不应答(挂满超时才失败),
|
||||
* 首查用更短预算快速失败并重试;生产实测成功调用 p99≈34s、超 60s 仅 0.02%,
|
||||
* 故默认 60s 既覆盖正常慢响应又能把单次挂死代价从 90s 降到 60s。
|
||||
* 首次尝试读超时(毫秒):上游偶发单请求不应答(挂满超时才失败),首查用更短预算快速失败并重试。
|
||||
* 单图请求实测成功调用 p99≈34s、超 60s 仅 0.02%,但外观检测已改成一批 5 张图、回 5 份判定,
|
||||
* 输出量约 5 倍,60s 会把正常慢批次误杀、连带整批 5 行白等一轮重试,故首查抬到 300s、重试走完整 600s。
|
||||
*/
|
||||
private int llmFirstAttemptReadTimeoutMillis = 60000;
|
||||
private int llmFirstAttemptReadTimeoutMillis = 300000;
|
||||
private int llmBatchSize = 10;
|
||||
/**
|
||||
* 商标关键词提取一批合并的行数(纯文本,一批一次请求)。设为 1 即退回逐行,是批量协议唯一应急开关。
|
||||
*/
|
||||
private int llmTitleBatchSize = 10;
|
||||
/**
|
||||
* 外观检测一批合并的行数,同时是一批的图片数(Gemini 支持多图,一批一次请求)。设为 1 即退回逐行。
|
||||
*/
|
||||
private int llmAppearanceBatchSize = 5;
|
||||
/**
|
||||
* 批内行级并发数。批次串行提交,每行串行发 2 个 LLM 请求,
|
||||
* 故该值≈单任务对 LLM 网关的瞬时并发;默认与品牌检测同为 5,避免多任务并行时成倍放大。
|
||||
|
||||
@@ -172,12 +172,13 @@ public class SimilarAsinProperties {
|
||||
|
||||
private int llmMaxTokens = 64000;
|
||||
private int llmConnectTimeoutMillis = 10000;
|
||||
private int llmReadTimeoutMillis = 180000;
|
||||
private int llmReadTimeoutMillis = 600000;
|
||||
/**
|
||||
* 首次尝试读超时(毫秒):上游偶发单请求不应答,首查用更短预算快速失败后重试;
|
||||
* 生产实测成功调用 p99≈34s、超 60s 仅 0.02%,默认 60s 兼顾慢响应与快速失败。
|
||||
* 首次尝试读超时(毫秒):上游偶发单请求不应答,首查用更短预算快速失败后重试。
|
||||
* 与外观专利同口径:那边一批 5 张图后单次耗时按倍数涨,60s 会误杀正常慢响应,
|
||||
* 故首查 300s、重试走完整 600s 预算(实测单图成功调用 p99≈34s、超 60s 仅 0.02%)。
|
||||
*/
|
||||
private int llmFirstAttemptReadTimeoutMillis = 60000;
|
||||
private int llmFirstAttemptReadTimeoutMillis = 300000;
|
||||
private int llmRetryTimes = 3;
|
||||
|
||||
/** 批内行级并发上限:每行最多 2 次图片对比 + 1 次合规 + 3 次类目匹配。 */
|
||||
|
||||
+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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -317,12 +317,16 @@ aiimage:
|
||||
appearance-model: ${AIIMAGE_APPEARANCE_PATENT_APPEARANCE_MODEL:gemini-3.8-flash}
|
||||
llm-max-tokens: ${AIIMAGE_APPEARANCE_PATENT_LLM_MAX_TOKENS:64000}
|
||||
llm-connect-timeout-millis: ${AIIMAGE_APPEARANCE_PATENT_LLM_CONNECT_TIMEOUT_MILLIS:10000}
|
||||
llm-read-timeout-millis: ${AIIMAGE_APPEARANCE_PATENT_LLM_READ_TIMEOUT_MILLIS:180000}
|
||||
llm-first-attempt-read-timeout-millis: ${AIIMAGE_APPEARANCE_PATENT_LLM_FIRST_ATTEMPT_READ_TIMEOUT_MILLIS:60000}
|
||||
llm-read-timeout-millis: ${AIIMAGE_APPEARANCE_PATENT_LLM_READ_TIMEOUT_MILLIS:600000}
|
||||
# 首查 300s:外观检测一批带 5 张图、回 5 份判定,沿用按单图调的 60s 会误杀正常慢批次并连带整批重等一轮
|
||||
llm-first-attempt-read-timeout-millis: ${AIIMAGE_APPEARANCE_PATENT_LLM_FIRST_ATTEMPT_READ_TIMEOUT_MILLIS:300000}
|
||||
llm-batch-size: ${AIIMAGE_APPEARANCE_PATENT_LLM_BATCH_SIZE:10}
|
||||
# 批内行并发:每行会串行发 2 个 LLM 请求(标题提取 + 外观检测),且批次串行提交,
|
||||
# 故该值≈单任务对 LLM 网关的瞬时并发。原 10 与品牌检测的 8 同一量级,
|
||||
# 多任务并行时会成倍放大、打满用户自己的网关密钥配额(2026-09-14 随品牌一起下调)。
|
||||
# 一次请求合并的行数:商标提取纯文本合并 10 行,外观检测按 Gemini 多图能力合并 5 行(=5 张图)。
|
||||
# 任一设为 1 即退回逐行发请求,是批量协议出问题时唯一的应急开关,无需改代码。
|
||||
llm-title-batch-size: ${AIIMAGE_APPEARANCE_PATENT_LLM_TITLE_BATCH_SIZE:10}
|
||||
llm-appearance-batch-size: ${AIIMAGE_APPEARANCE_PATENT_LLM_APPEARANCE_BATCH_SIZE:5}
|
||||
# 批间并发:合并后「一批 = 一次请求」,故该值≈单任务对 LLM 网关的瞬时并发请求数。
|
||||
# 默认 5 与品牌检测同量级,多任务并行时会成倍放大、打满用户自己的网关密钥配额(2026-09-14 随品牌一起下调)。
|
||||
llm-row-concurrency: ${AIIMAGE_APPEARANCE_PATENT_LLM_ROW_CONCURRENCY:5}
|
||||
max-parse-rows: ${AIIMAGE_APPEARANCE_PATENT_MAX_PARSE_ROWS:50000}
|
||||
# LLM 覆盖率下限:组装结果时拿到结论的行占比低于该值即判任务失败(<=0 关闭)。
|
||||
@@ -358,8 +362,8 @@ aiimage:
|
||||
llm-image-compare-model: ${AIIMAGE_SIMILAR_ASIN_LLM_IMAGE_COMPARE_MODEL:gemini-3.8-flash}
|
||||
llm-max-tokens: ${AIIMAGE_SIMILAR_ASIN_LLM_MAX_TOKENS:64000}
|
||||
llm-connect-timeout-millis: ${AIIMAGE_SIMILAR_ASIN_LLM_CONNECT_TIMEOUT_MILLIS:10000}
|
||||
llm-read-timeout-millis: ${AIIMAGE_SIMILAR_ASIN_LLM_READ_TIMEOUT_MILLIS:180000}
|
||||
llm-first-attempt-read-timeout-millis: ${AIIMAGE_SIMILAR_ASIN_LLM_FIRST_ATTEMPT_READ_TIMEOUT_MILLIS:60000}
|
||||
llm-read-timeout-millis: ${AIIMAGE_SIMILAR_ASIN_LLM_READ_TIMEOUT_MILLIS:600000}
|
||||
llm-first-attempt-read-timeout-millis: ${AIIMAGE_SIMILAR_ASIN_LLM_FIRST_ATTEMPT_READ_TIMEOUT_MILLIS:300000}
|
||||
llm-retry-times: ${AIIMAGE_SIMILAR_ASIN_LLM_RETRY_TIMES:3}
|
||||
llm-row-concurrency: ${AIIMAGE_SIMILAR_ASIN_LLM_ROW_CONCURRENCY:5}
|
||||
llm-image-download-timeout-seconds: ${AIIMAGE_SIMILAR_ASIN_LLM_IMAGE_DOWNLOAD_TIMEOUT_SECONDS:10}
|
||||
|
||||
+304
-15
@@ -30,11 +30,19 @@ class AppearancePatentLlmClientHttpTest {
|
||||
private HttpServer server;
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private final Map<String, AtomicInteger> callCounts = new ConcurrentHashMap<>();
|
||||
private final List<String> capturedBodies = new ArrayList<>();
|
||||
private final List<String> capturedBodies = java.util.Collections.synchronizedList(new ArrayList<>());
|
||||
/** 纯文本(标题提取)请求计数:用于验证重试次数与「跳过外观」行为。 */
|
||||
private final AtomicInteger textRequestCount = new AtomicInteger();
|
||||
/** 带图(外观检测)请求计数:验证多图合批与「失败不逐行重发」。 */
|
||||
private final AtomicInteger imageRequestCount = new AtomicInteger();
|
||||
private volatile boolean failTextRequests = false;
|
||||
private volatile boolean failImageRequests = false;
|
||||
private volatile long firstTextRequestDelayMillis = 0L;
|
||||
/** 批量提取响应改写钩子:模拟漏行、串味、乱序三种模型失真形状。 */
|
||||
private volatile boolean dropLastTitleItem = false;
|
||||
private volatile boolean crossAssignFirstTitleItem = false;
|
||||
private volatile boolean reverseTitleItems = false;
|
||||
private volatile boolean dropLastAppearanceItem = false;
|
||||
|
||||
private AppearancePatentLlmClient client;
|
||||
|
||||
@@ -42,6 +50,7 @@ class AppearancePatentLlmClientHttpTest {
|
||||
void setUp() throws IOException {
|
||||
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/v1/chat/completions", this::handleChat);
|
||||
server.createContext("/brand_check", this::handleBrandCheck);
|
||||
server.start();
|
||||
|
||||
AppearancePatentProperties properties = new AppearancePatentProperties();
|
||||
@@ -51,11 +60,28 @@ class AppearancePatentLlmClientHttpTest {
|
||||
properties,
|
||||
objectMapper,
|
||||
null,
|
||||
new BrandCheckClient(new BrandCheckProperties(), null),
|
||||
localBrandCheckClient(),
|
||||
mock(UserSecretUsageService.class)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 品牌查询也指向本地桩:真实网关在测试里按 10 次重试退避,
|
||||
* 单行走一次回退就能拖走一分钟,批量用例会被放大成十几分钟。
|
||||
*/
|
||||
private BrandCheckClient localBrandCheckClient() {
|
||||
BrandCheckProperties brand = new BrandCheckProperties();
|
||||
brand.setBaseUrl("http://127.0.0.1:" + server.getAddress().getPort());
|
||||
brand.setRetryTimes(1);
|
||||
brand.setRetryIntervalMillis(0);
|
||||
brand.setTotalTimeoutMillis(5000);
|
||||
return new BrandCheckClient(brand, null);
|
||||
}
|
||||
|
||||
private void handleBrandCheck(HttpExchange exchange) throws IOException {
|
||||
respond(exchange, 200, "{\"faild_data\":[],\"query_faild_data\":[]}");
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
server.stop(0);
|
||||
@@ -73,6 +99,12 @@ class AppearancePatentLlmClientHttpTest {
|
||||
respond(exchange, 502, "{\"error\":{\"message\":\"bad gateway\"}}");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
imageRequestCount.incrementAndGet();
|
||||
if (failImageRequests) {
|
||||
respond(exchange, 502, "{\"error\":{\"message\":\"bad gateway\"}}");
|
||||
return;
|
||||
}
|
||||
}
|
||||
try {
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -84,19 +116,10 @@ class AppearancePatentLlmClientHttpTest {
|
||||
// 外观检测消息带 image_url parts,标题商标提取仅纯文本。
|
||||
String content;
|
||||
if (body.contains("\"type\":\"image_url\"")) {
|
||||
// 外观检测:返回 JSON(带 ```json 包裹与换行,模拟脏输出)
|
||||
if (body.contains("原创个性杯")) {
|
||||
content = "```json\n{\"appearance_status\": \"侵权\", \"appearance_reason\": \"【视觉拆解】:特殊造型\\n【判定依据】:高度相似知名设计\"}\n```";
|
||||
} else {
|
||||
content = "{\"appearance_status\": \"无侵权\", \"appearance_reason\": \"【视觉拆解】:普通直筒杯。\\n【对比评估】:行业通用基础形状。\\n【判定依据】:无侵权。\"}";
|
||||
}
|
||||
content = appearanceBatchContent(request);
|
||||
} else {
|
||||
// 商标提取:按标题内容返回品牌词或"无"
|
||||
if (String.valueOf(request.get("messages")).contains("Apple")) {
|
||||
content = "Apple,Apple";
|
||||
} else {
|
||||
content = "无";
|
||||
}
|
||||
// 商标提取:按「序号\t文本」行协议逐行回品牌词
|
||||
content = titleBatchContent(request);
|
||||
}
|
||||
String response = objectMapper.writeValueAsString(Map.of(
|
||||
"model", model,
|
||||
@@ -126,6 +149,109 @@ class AppearancePatentLlmClientHttpTest {
|
||||
}
|
||||
}
|
||||
|
||||
/** 批量商标提取假实现:逐行给词,可按开关模拟漏行、串味、乱序三种失真形状。 */
|
||||
private String titleBatchContent(Map<String, Object> request) throws Exception {
|
||||
List<Integer> indexes = new ArrayList<>();
|
||||
List<String> texts = new ArrayList<>();
|
||||
parseIndexedLines(userTextOf(request), indexes, texts);
|
||||
boolean batch = indexes.size() > 1;
|
||||
List<Map<String, Object>> items = new ArrayList<>();
|
||||
for (int i = 0; i < indexes.size(); i++) {
|
||||
if (batch && dropLastTitleItem && i == indexes.size() - 1) {
|
||||
continue;
|
||||
}
|
||||
String keywords = keywordsFor(texts.get(i));
|
||||
if (batch && crossAssignFirstTitleItem && i == 0) {
|
||||
keywords = keywordsFor(texts.get(1));
|
||||
}
|
||||
items.add(Map.of("i", indexes.get(i), "keywords", keywords));
|
||||
}
|
||||
if (reverseTitleItems) {
|
||||
java.util.Collections.reverse(items);
|
||||
}
|
||||
return objectMapper.writeValueAsString(Map.of("items", items));
|
||||
}
|
||||
|
||||
/**
|
||||
* 外观检测假实现:按「序号\t描述」逐图给判定;单行且命中脏输出样本时用 ```json 包裹,
|
||||
* 保留对解包路径的覆盖。
|
||||
*/
|
||||
private String appearanceBatchContent(Map<String, Object> request) throws Exception {
|
||||
List<Integer> indexes = new ArrayList<>();
|
||||
List<String> texts = new ArrayList<>();
|
||||
parseIndexedLines(appearanceUserTextOf(request), indexes, texts);
|
||||
List<Map<String, Object>> items = new ArrayList<>();
|
||||
for (int i = 0; i < indexes.size(); i++) {
|
||||
if (indexes.size() > 1 && dropLastAppearanceItem && i == indexes.size() - 1) {
|
||||
continue;
|
||||
}
|
||||
items.add(Map.of("i", indexes.get(i),
|
||||
"appearance_status", texts.get(i).contains("原创个性杯") ? "侵权" : "无侵权",
|
||||
"appearance_reason", appearanceReasonFor(texts.get(i))));
|
||||
}
|
||||
String json = objectMapper.writeValueAsString(Map.of("items", items));
|
||||
boolean dirtySingle = indexes.size() <= 1 && !texts.isEmpty() && texts.get(0).contains("原创个性杯");
|
||||
return dirtySingle ? "```json\n" + json + "\n```" : json;
|
||||
}
|
||||
|
||||
private String appearanceReasonFor(String text) {
|
||||
return text.contains("原创个性杯")
|
||||
? "【视觉拆解】:特殊造型\n【判定依据】:高度相似知名设计"
|
||||
: "【视觉拆解】:普通直筒杯。\n【对比评估】:行业通用基础形状。\n【判定依据】:无侵权。";
|
||||
}
|
||||
|
||||
private void parseIndexedLines(String raw, List<Integer> indexes, List<String> texts) {
|
||||
for (String line : raw.split("\n")) {
|
||||
int tab = line.indexOf('\t');
|
||||
if (tab <= 0) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
indexes.add(Integer.parseInt(line.substring(0, tab).trim()));
|
||||
} catch (NumberFormatException ignored) {
|
||||
continue;
|
||||
}
|
||||
texts.add(line.substring(tab + 1));
|
||||
}
|
||||
}
|
||||
|
||||
private String keywordsFor(String text) {
|
||||
if (text.contains("Apple")) {
|
||||
return "Apple";
|
||||
}
|
||||
if (text.contains("Nike")) {
|
||||
return "Nike";
|
||||
}
|
||||
return "无";
|
||||
}
|
||||
|
||||
private String userTextOf(Map<String, Object> request) {
|
||||
Object content = lastMessageContent(request);
|
||||
return content == null ? "" : String.valueOf(content);
|
||||
}
|
||||
|
||||
/** 多模态请求的文本段(其余为 image_url 段)。 */
|
||||
private String appearanceUserTextOf(Map<String, Object> request) {
|
||||
Object content = lastMessageContent(request);
|
||||
if (content instanceof List<?> parts) {
|
||||
for (Object part : parts) {
|
||||
if (part instanceof Map<?, ?> map && "text".equals(map.get("type"))) {
|
||||
return String.valueOf(map.get("text"));
|
||||
}
|
||||
}
|
||||
}
|
||||
return content == null ? "" : String.valueOf(content);
|
||||
}
|
||||
|
||||
private Object lastMessageContent(Map<String, Object> request) {
|
||||
Object messages = request.get("messages");
|
||||
if (messages instanceof List<?> list && !list.isEmpty()
|
||||
&& list.get(list.size() - 1) instanceof Map<?, ?> message) {
|
||||
return message.get("content");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void sleep(long millis) {
|
||||
try {
|
||||
Thread.sleep(millis);
|
||||
@@ -140,7 +266,7 @@ class AppearancePatentLlmClientHttpTest {
|
||||
properties.setLlmHost("http://127.0.0.1:" + server.getAddress().getPort());
|
||||
customizer.accept(properties);
|
||||
return new AppearancePatentLlmClient(properties, objectMapper, null,
|
||||
new BrandCheckClient(new BrandCheckProperties(), null), mock(UserSecretUsageService.class));
|
||||
localBrandCheckClient(), mock(UserSecretUsageService.class));
|
||||
}
|
||||
|
||||
private boolean messagesBodyContains(HttpExchange exchange, String text) {
|
||||
@@ -273,4 +399,167 @@ class AppearancePatentLlmClientHttpTest {
|
||||
assertThat(textRequestCount.get()).isEqualTo(2);
|
||||
assertThat(elapsedMs).isGreaterThanOrEqualTo(400L);
|
||||
}
|
||||
|
||||
/** 10 行商标提取压成 1 次文本请求,且每行品牌词各归各位。 */
|
||||
@Test
|
||||
void titleExtractionBatchesTenRowsIntoOneRequest() {
|
||||
List<AppearancePatentResultRowDto> rows = new ArrayList<>();
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
rows.add(row(String.valueOf(i), "B10" + i,
|
||||
i % 2 == 0 ? "Nike Air 杯" + i : "普通收纳盒" + i, "",
|
||||
"https://img.example.com/1" + i + ".jpg"));
|
||||
}
|
||||
|
||||
List<AppearancePatentResultRowDto> result = client.inspectRows(rows, null, "test-key");
|
||||
|
||||
assertThat(textRequestCount.get()).isEqualTo(1);
|
||||
assertThat(result).hasSize(10);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
assertThat(result.get(i).getTitleReason()).isEqualTo(i % 2 == 1 ? "Nike" : "无");
|
||||
}
|
||||
}
|
||||
|
||||
/** 批量响应乱序:按回显序号对账,不能按数组位置对齐后把词安到别的行上。 */
|
||||
@Test
|
||||
void titleBatchMatchesItemsByIndexNotByPosition() {
|
||||
reverseTitleItems = true;
|
||||
List<AppearancePatentResultRowDto> rows = List.of(
|
||||
row("1", "B201", "Apple Magic Case", "", "https://img.example.com/201.jpg"),
|
||||
row("2", "B202", "Nike Air 鞋", "", "https://img.example.com/202.jpg"),
|
||||
row("3", "B203", "普通收纳盒", "", "https://img.example.com/203.jpg"));
|
||||
|
||||
List<AppearancePatentResultRowDto> result = client.inspectRows(rows, null, "test-key");
|
||||
|
||||
assertThat(textRequestCount.get()).isEqualTo(1);
|
||||
assertThat(result.get(0).getTitleReason()).isEqualTo("Apple");
|
||||
assertThat(result.get(1).getTitleReason()).isEqualTo("Nike");
|
||||
assertThat(result.get(2).getTitleReason()).isEqualTo("无");
|
||||
}
|
||||
|
||||
/** 批量响应漏行:漏掉的行单发补检,补检后照常走外观,不允许静默留空。 */
|
||||
@Test
|
||||
void titleBatchDropsRowThenRedoesItSingly() {
|
||||
dropLastTitleItem = true;
|
||||
List<AppearancePatentResultRowDto> rows = List.of(
|
||||
row("1", "B301", "普通收纳盒A", "", "https://img.example.com/301.jpg"),
|
||||
row("2", "B302", "Nike Air 鞋", "", "https://img.example.com/302.jpg"));
|
||||
|
||||
List<AppearancePatentResultRowDto> result = client.inspectRows(rows, null, "test-key");
|
||||
|
||||
assertThat(textRequestCount.get()).isEqualTo(2);
|
||||
assertThat(result.get(0).getTitleReason()).isEqualTo("无");
|
||||
assertThat(result.get(1).getTitleReason()).isEqualTo("Nike");
|
||||
assertThat(result.get(1).getAppearanceRisk()).isEqualTo("无侵权");
|
||||
}
|
||||
|
||||
/** 批量响应串味(序号对、内容是别行的词):提示词要求只提取原文出现的词,按字面校验拦下。 */
|
||||
@Test
|
||||
void titleBatchCrossContaminatedRowIsRedone() {
|
||||
crossAssignFirstTitleItem = true;
|
||||
List<AppearancePatentResultRowDto> rows = List.of(
|
||||
row("1", "B401", "普通收纳盒A", "", "https://img.example.com/401.jpg"),
|
||||
row("2", "B402", "Nike Air 鞋", "", "https://img.example.com/402.jpg"));
|
||||
|
||||
List<AppearancePatentResultRowDto> result = client.inspectRows(rows, null, "test-key");
|
||||
|
||||
assertThat(textRequestCount.get()).isEqualTo(2);
|
||||
assertThat(result.get(0).getTitleReason()).isEqualTo("无");
|
||||
assertThat(result.get(1).getTitleReason()).isEqualTo("Nike");
|
||||
}
|
||||
|
||||
/** 批量调用整体失败=一次请求带走一批:不得逐行重发放大成本,同批外观请求不发。 */
|
||||
@Test
|
||||
void titleBatchTransportFailureFallsBackWithoutRedoStorm() {
|
||||
failTextRequests = true;
|
||||
AppearancePatentLlmClient singleAttempt = buildClient(p -> {
|
||||
p.setLlmRetryTimes(1);
|
||||
p.setLlmFirstAttemptReadTimeoutMillis(5_000);
|
||||
p.setLlmReadTimeoutMillis(5_000);
|
||||
});
|
||||
List<AppearancePatentResultRowDto> rows = new ArrayList<>();
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
rows.add(row(String.valueOf(i), "B50" + i, "任意标题" + i, "",
|
||||
"https://img.example.com/50" + i + ".jpg"));
|
||||
}
|
||||
|
||||
List<AppearancePatentResultRowDto> result = singleAttempt.inspectRows(rows, null, "test-key");
|
||||
|
||||
assertThat(textRequestCount.get()).isEqualTo(1);
|
||||
assertThat(result).hasSize(5);
|
||||
for (AppearancePatentResultRowDto item : result) {
|
||||
assertThat(item.getAppearanceRisk()).isEqualTo("外观识别异常");
|
||||
}
|
||||
assertThat(capturedBodies).noneMatch(b -> b.contains("\"type\":\"image_url\""));
|
||||
}
|
||||
|
||||
/** 5 行外观检测压成 1 次多图请求:请求体带齐 5 张图,逐行判定各归各位。 */
|
||||
@Test
|
||||
void appearanceBatchesFiveRowsIntoOneMultiImageRequest() {
|
||||
List<AppearancePatentResultRowDto> rows = List.of(
|
||||
row("1", "C001", "普通直筒杯", "", "https://img.example.com/c1.jpg"),
|
||||
row("2", "C002", "原创个性杯", "", "https://img.example.com/c2.jpg"),
|
||||
row("3", "C003", "普通收纳盒", "", "https://img.example.com/c3.jpg"),
|
||||
row("4", "C004", "普通数据线", "", "https://img.example.com/c4.jpg"),
|
||||
row("5", "C005", "普通袜套", "", "https://img.example.com/c5.jpg"));
|
||||
|
||||
List<AppearancePatentResultRowDto> result = client.inspectRows(rows, null, "test-key");
|
||||
|
||||
assertThat(imageRequestCount.get()).isEqualTo(1);
|
||||
String batchBody = capturedBodies.stream()
|
||||
.filter(b -> b.contains("\"type\":\"image_url\""))
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
assertThat(countOccurrences(batchBody, "\"type\":\"image_url\"")).isEqualTo(5);
|
||||
assertThat(result.get(1).getAppearanceRisk()).isEqualTo("侵权");
|
||||
assertThat(result.get(1).getConclusion()).isEqualTo("侵权");
|
||||
assertThat(result.get(0).getAppearanceRisk()).isEqualTo("无侵权");
|
||||
assertThat(result.get(2).getAppearanceRisk()).isEqualTo("无侵权");
|
||||
assertThat(result.get(3).getAppearanceRisk()).isEqualTo("无侵权");
|
||||
assertThat(result.get(4).getAppearanceRisk()).isEqualTo("无侵权");
|
||||
}
|
||||
|
||||
/** 外观批量响应漏一行:该行单发补检,不允许静默留空或整行判异常。 */
|
||||
@Test
|
||||
void appearanceBatchDropsRowThenRedoesItSingly() {
|
||||
dropLastAppearanceItem = true;
|
||||
List<AppearancePatentResultRowDto> rows = List.of(
|
||||
row("1", "D001", "普通直筒杯", "", "https://img.example.com/d1.jpg"),
|
||||
row("2", "D002", "原创个性杯", "", "https://img.example.com/d2.jpg"));
|
||||
|
||||
List<AppearancePatentResultRowDto> result = client.inspectRows(rows, null, "test-key");
|
||||
|
||||
assertThat(imageRequestCount.get()).isEqualTo(2);
|
||||
assertThat(result.get(0).getAppearanceRisk()).isEqualTo("无侵权");
|
||||
assertThat(result.get(1).getAppearanceRisk()).isEqualTo("侵权");
|
||||
}
|
||||
|
||||
/** 外观批量调用失败=一次请求带走一批:不逐行重发,失败行走既有回退语义。 */
|
||||
@Test
|
||||
void appearanceBatchTransportFailureDoesNotRedoPerRow() {
|
||||
failImageRequests = true;
|
||||
AppearancePatentLlmClient singleAttempt = buildClient(p -> {
|
||||
p.setLlmRetryTimes(1);
|
||||
p.setLlmFirstAttemptReadTimeoutMillis(5_000);
|
||||
p.setLlmReadTimeoutMillis(5_000);
|
||||
});
|
||||
List<AppearancePatentResultRowDto> rows = new ArrayList<>();
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
rows.add(row(String.valueOf(i), "E00" + i, "普通直筒杯" + i, "", "https://img.example.com/e" + i + ".jpg"));
|
||||
}
|
||||
|
||||
List<AppearancePatentResultRowDto> result = singleAttempt.inspectRows(rows, null, "test-key");
|
||||
|
||||
assertThat(imageRequestCount.get()).isEqualTo(1);
|
||||
for (AppearancePatentResultRowDto item : result) {
|
||||
assertThat(item.getAppearanceRisk()).isEqualTo("外观识别异常");
|
||||
}
|
||||
}
|
||||
|
||||
private static int countOccurrences(String haystack, String needle) {
|
||||
int count = 0;
|
||||
for (int i = haystack.indexOf(needle); i >= 0; i = haystack.indexOf(needle, i + needle.length())) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
+185
-6
@@ -20,14 +20,21 @@ import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyList;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.mockito.Mockito;
|
||||
@@ -52,10 +59,11 @@ class SimilarAsinLlmServiceTest {
|
||||
}
|
||||
|
||||
private SimilarAsinLlmService service(SimilarAsinLlmClient llmClient) {
|
||||
OssStorageService ossStorage = mock(OssStorageService.class);
|
||||
when(ossStorage.getPublicUrl(anyString())).thenAnswer(invocation -> "https://oss.aishufu.top/nanri-ai-images/" + invocation.getArgument(0));
|
||||
PuzzleImageMerger merger = mock(PuzzleImageMerger.class);
|
||||
when(merger.merge(anyList(), Mockito.<SimilarAsinResultRowDto>any())).thenReturn(jpegBytes());
|
||||
return service(llmClient, chainedCategoryService());
|
||||
}
|
||||
|
||||
/** 单链类目树:类目A(1) → 类目B(2) → 类目C(3),供既有测试用。 */
|
||||
private ProductCategoryService chainedCategoryService() {
|
||||
ProductCategoryService categoryService = mock(ProductCategoryService.class);
|
||||
when(categoryService.children(any(), anyLong(), anyLong()))
|
||||
.thenAnswer(invocation -> {
|
||||
@@ -71,6 +79,16 @@ class SimilarAsinLlmServiceTest {
|
||||
}
|
||||
return emptyCategoryPage();
|
||||
});
|
||||
when(categoryService.list()).thenReturn(pageOf(List.of(
|
||||
cat(1L, null, "类目A"), cat(2L, 1L, "类目B"), cat(3L, 2L, "类目C"))));
|
||||
return categoryService;
|
||||
}
|
||||
|
||||
private SimilarAsinLlmService service(SimilarAsinLlmClient llmClient, ProductCategoryService categoryService) {
|
||||
OssStorageService ossStorage = mock(OssStorageService.class);
|
||||
when(ossStorage.getPublicUrl(anyString())).thenAnswer(invocation -> "https://oss.aishufu.top/nanri-ai-images/" + invocation.getArgument(0));
|
||||
PuzzleImageMerger merger = mock(PuzzleImageMerger.class);
|
||||
when(merger.merge(anyList(), Mockito.<SimilarAsinResultRowDto>any())).thenReturn(jpegBytes());
|
||||
SimilarAsinLlmService svc = new SimilarAsinLlmService(
|
||||
llmClient,
|
||||
properties(),
|
||||
@@ -255,8 +273,8 @@ class SimilarAsinLlmServiceTest {
|
||||
|
||||
@Test
|
||||
void imageCompareStopsOnStockAndFillsFields() {
|
||||
// 前 2 次 chat:一级/二级类目匹配返回名称(Java 按名回查候选取真实 ID);第 3 次:合规检查。
|
||||
SimilarAsinLlmClient client = llmClientStaged("k", List.of("{\"name\":\"类目A\"}", "{\"name\":\"类目B\"}"),
|
||||
// 第 1 次 chat:合并类目调用一次给出一级+二级名称(Java 按名回查取真实 ID);第 2 次:合规检查。
|
||||
SimilarAsinLlmClient client = llmClientStaged("k", List.of("{\"first\":\"类目A\",\"second\":\"类目B\"}"),
|
||||
"{\"asin\":\"B0TEST\",\"is_conform\":\"符合\",\"reason\":\"无\",\"category\":\"类目A->类目B->类目C\"}",
|
||||
"{\"asin\":\"B0TEST\",\"is_stock\":\"有货\",\"similarity\":\"95%\",\"status\":\"成功\",\"is_conform\":\"符合\",\"category\":\"类目A->类目B->类目C\"}");
|
||||
SimilarAsinLlmService service = service(client);
|
||||
@@ -278,4 +296,165 @@ class SimilarAsinLlmServiceTest {
|
||||
assertEquals("95%", out.getSimilarity());
|
||||
assertEquals("符合", out.getIsConform());
|
||||
}
|
||||
|
||||
// --- 合并类目判定(一次调用出一级+二级)---
|
||||
|
||||
private static ProductCategoryItemVo cat(long id, Long parentId, String name) {
|
||||
ProductCategoryItemVo item = new ProductCategoryItemVo();
|
||||
item.setId(id);
|
||||
item.setParentId(parentId);
|
||||
item.setName(name);
|
||||
return item;
|
||||
}
|
||||
|
||||
private static ProductCategoryListVo pageOf(List<ProductCategoryItemVo> items) {
|
||||
ProductCategoryListVo vo = new ProductCategoryListVo();
|
||||
vo.setItems(items);
|
||||
vo.setTree(List.of());
|
||||
vo.setTotal((long) items.size());
|
||||
vo.setPage(1L);
|
||||
vo.setPageSize((long) items.size());
|
||||
vo.setHasMore(false);
|
||||
return vo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 含重名二级的两层树:家具(100)→配件(101)→沙发套(301);数码(200)→配件(201)→手机壳(300)。
|
||||
* 二级同名用来暴露「二级挂到另一分支」的错误——三级候选里会出现另一个分支的叶子。
|
||||
*/
|
||||
private static List<ProductCategoryItemVo> duplicateNameTree() {
|
||||
return List.of(
|
||||
cat(100L, null, "家具"),
|
||||
cat(200L, null, "数码"),
|
||||
cat(101L, 100L, "配件"),
|
||||
cat(201L, 200L, "配件"),
|
||||
cat(301L, 101L, "沙发套"),
|
||||
cat(300L, 201L, "手机壳"));
|
||||
}
|
||||
|
||||
/** 同一棵树同时喂 list()(新链路整树加载)与 children()(旧链路逐层查),保证 RED 阶段可跑。 */
|
||||
private ProductCategoryService duplicateTreeCategoryService() {
|
||||
ProductCategoryService categoryService = mock(ProductCategoryService.class);
|
||||
List<ProductCategoryItemVo> all = duplicateNameTree();
|
||||
when(categoryService.list()).thenReturn(pageOf(all));
|
||||
when(categoryService.children(any(), anyLong(), anyLong()))
|
||||
.thenAnswer(invocation -> {
|
||||
Long parentId = invocation.getArgument(0);
|
||||
return pageOf(all.stream().filter(item -> Objects.equals(item.getParentId(), parentId)).toList());
|
||||
});
|
||||
return categoryService;
|
||||
}
|
||||
|
||||
/** 按 userText 前缀区分类目调用与合规调用,并记录全部 chat 调用供断言。 */
|
||||
private SimilarAsinLlmClient recordingClient(List<String> chatUserTexts, String categoryJson, String conformJson) {
|
||||
SimilarAsinLlmClient client = mock(SimilarAsinLlmClient.class);
|
||||
when(client.resolveApiKey(anyString())).thenReturn("k");
|
||||
when(client.hasApiKey(anyString())).thenReturn(true);
|
||||
when(client.invokeChat(anyString(), anyString(), anyString(), anyString()))
|
||||
.thenAnswer(invocation -> {
|
||||
String userText = invocation.getArgument(2);
|
||||
chatUserTexts.add(userText);
|
||||
return userText.startsWith("产品标题:") ? categoryJson : conformJson;
|
||||
});
|
||||
when(client.invokeChatWithImages(anyString(), anyString(), anyString(), any(), anyString()))
|
||||
.thenReturn("{\"asin\":\"B01\",\"is_stock\":\"没有货\",\"similarity\":\"10%\",\"status\":\"成功\"}");
|
||||
when(client.parseJsonContent(anyString()))
|
||||
.thenAnswer(invocation -> parseJson(invocation.getArgument(0)));
|
||||
return client;
|
||||
}
|
||||
|
||||
private static SimilarAsinResultRowDto rowOf(String asin, String title) {
|
||||
SimilarAsinResultRowDto row = new SimilarAsinResultRowDto();
|
||||
row.setAsin(asin);
|
||||
row.setTitle(title);
|
||||
return row;
|
||||
}
|
||||
|
||||
private static String last(List<String> values) {
|
||||
return values.get(values.size() - 1);
|
||||
}
|
||||
|
||||
/** 合规 userText 里「三级类目:」之后的候选段,独立于标题断言。 */
|
||||
private static String thirdCandidatesOf(String conformText) {
|
||||
int index = conformText.indexOf("三级类目:");
|
||||
assertTrue(index >= 0, "合规入参缺少三级候选段:" + conformText);
|
||||
return conformText.substring(index);
|
||||
}
|
||||
|
||||
@Test
|
||||
void secondLevelResolvesOnlyWithinChosenFirstLevel() {
|
||||
List<String> chats = new CopyOnWriteArrayList<>();
|
||||
SimilarAsinLlmService service = service(
|
||||
recordingClient(chats, "{\"first\":\"家具\",\"second\":\"配件\"}",
|
||||
"{\"asin\":\"B01\",\"is_conform\":\"不符合\",\"reason\":\"命中违规类目\",\"category\":\"家具->配件->沙发套\"}"),
|
||||
duplicateTreeCategoryService());
|
||||
|
||||
service.inspectRows(List.of(rowOf("B01", "布质防滑罩")), "", "k", false, true);
|
||||
|
||||
String thirdSegment = thirdCandidatesOf(last(chats));
|
||||
assertTrue(thirdSegment.contains("沙发套"), "三级候选应取所选一级之下的二级,实际:" + thirdSegment);
|
||||
assertFalse(thirdSegment.contains("手机壳"), "禁止挂到另一分支的同名二级,实际:" + thirdSegment);
|
||||
}
|
||||
|
||||
@Test
|
||||
void categoryResolutionUsesSingleChatCall() {
|
||||
List<String> chats = new CopyOnWriteArrayList<>();
|
||||
// 双形状 JSON:旧契约读 name、新契约读 first/second,两边都能解析出「家具」,只比调用次数。
|
||||
SimilarAsinLlmService service = service(
|
||||
recordingClient(chats, "{\"name\":\"家具\",\"first\":\"家具\",\"second\":\"配件\"}",
|
||||
"{\"asin\":\"B01\",\"is_conform\":\"不符合\",\"reason\":\"无\",\"category\":\"家具->配件->沙发套\"}"),
|
||||
duplicateTreeCategoryService());
|
||||
|
||||
service.inspectRows(List.of(rowOf("B01", "亚麻沙发套")), "", "k", false, true);
|
||||
|
||||
assertEquals(2, chats.size(), "类目+合规应各 1 次,实际调用:" + chats);
|
||||
}
|
||||
|
||||
@Test
|
||||
void secondLevelNoMatchKeepsFirstLevel() {
|
||||
List<String> chats = new CopyOnWriteArrayList<>();
|
||||
SimilarAsinLlmService service = service(
|
||||
recordingClient(chats, "{\"first\":\"数码\",\"second\":\"无\"}",
|
||||
"{\"asin\":\"B01\",\"is_conform\":\"不符合\",\"reason\":\"无\",\"category\":\"数码\"}"),
|
||||
duplicateTreeCategoryService());
|
||||
|
||||
service.inspectRows(List.of(rowOf("B01", "手机支架")), "", "k", false, true);
|
||||
|
||||
String conformText = last(chats);
|
||||
assertTrue(conformText.contains("一级类目:数码"), "二级回「无」不得连带丢掉一级,实际:" + conformText);
|
||||
assertTrue(conformText.contains("二级类目:无"), "二级应为「无」,实际:" + conformText);
|
||||
assertFalse(conformText.contains("手机壳"), "二级为「无」时不应给出三级候选,实际:" + conformText);
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownSecondLevelDegradesToNoneWithoutFailingRow() {
|
||||
List<String> chats = new CopyOnWriteArrayList<>();
|
||||
SimilarAsinLlmService service = service(
|
||||
recordingClient(chats, "{\"first\":\"家具\",\"second\":\"不存在的类目\"}",
|
||||
"{\"asin\":\"B01\",\"is_conform\":\"不符合\",\"reason\":\"无\",\"category\":\"家具\"}"),
|
||||
duplicateTreeCategoryService());
|
||||
|
||||
List<SimilarAsinResultRowDto> result = service.inspectRows(List.of(rowOf("B01", "实木餐桌")), "", "k", false, true);
|
||||
|
||||
assertEquals("不符合", result.get(0).getIsConform());
|
||||
assertEquals("成功", result.get(0).getStatus());
|
||||
String conformText = last(chats);
|
||||
assertTrue(conformText.contains("一级类目:家具"), "编造的二级不得连带丢掉一级,实际:" + conformText);
|
||||
assertTrue(conformText.contains("二级类目:无"), "实际:" + conformText);
|
||||
}
|
||||
|
||||
@Test
|
||||
void categoryTreeIsLoadedOncePerBatch() {
|
||||
List<String> chats = new CopyOnWriteArrayList<>();
|
||||
ProductCategoryService categoryService = duplicateTreeCategoryService();
|
||||
SimilarAsinLlmService service = service(
|
||||
recordingClient(chats, "{\"first\":\"家具\",\"second\":\"配件\"}",
|
||||
"{\"asin\":\"B01\",\"is_conform\":\"不符合\",\"reason\":\"无\",\"category\":\"家具->配件->沙发套\"}"),
|
||||
categoryService);
|
||||
|
||||
service.inspectRows(List.of(rowOf("B01", "A"), rowOf("B02", "B"), rowOf("B03", "C")), "", "k", false, true);
|
||||
|
||||
verify(categoryService, times(1)).list();
|
||||
verify(categoryService, never()).children(any(), anyLong(), anyLong());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user