perf(货源): 分类匹配批量分块,类目树文本整批只随一次请求发送

分类判定按 llmCategoryBatchSize(默认10)分块、每块一次 LLM 调用:formatTwoLevels 的两层类目树此前随每行请求重复发送,是分类链路最大的 token 开销(10 行/块省约九成)。块内缺编号/坏编号的行、整块调用失败的行回退逐行单调用,行级失败语义不变。env: AIIMAGE_SIMILAR_ASIN_LLM_CATEGORY_BATCH_SIZE,设 0/1 回退旧行为。
This commit is contained in:
2026-10-02 23:41:28 +08:00
parent 486c5c1d38
commit 68b19f22b3
4 changed files with 344 additions and 5 deletions
@@ -170,6 +170,14 @@ public class SimilarAsinProperties {
private int llmMaxTokens = 64000;
private int llmConnectTimeoutMillis = 10000;
private int llmReadTimeoutMillis = 600000;
/**
* 批量分类的行数(2026-10-02):>1 且批内多行时,分类判定按该行数分块、每块一次
* LLM 调用 —— 类目树文本(formatTwoLevels,数千字符量级)整块只随请求发送一次,
* 不再随每行重复发送(这是分类链路最大的 token 开销,10 行/块可省约九成)。
* 缺编号/解析失败的行、整块调用失败的行回退逐行单调用,行级失败语义不变。
* 设 0/1 回退逐行旧行为。
*/
private int llmCategoryBatchSize = 10;
/**
* 首次尝试读超时(毫秒):上游偶发单请求不应答,首查用更短预算快速失败后重试。
* 与外观专利同口径:那边一批 5 张图后单次耗时按倍数涨,60s 会误杀正常慢响应,
@@ -28,6 +28,7 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
@@ -158,6 +159,19 @@ public class SimilarAsinLlmService {
+ "}";
private static final String USER_TEXT_CATEGORY = "产品标题:%s\n产品属性:%s\n类目:%s";
/**
* 批量分类的输出契约追加段(2026-10-02):一次请求判定整块行,类目树文本
* (formatTwoLevels,数千字符量级)只随请求发送一次 —— 逐行请求把同一棵树
* 重复发送 N 遍,是分类链路里最大的 token 开销。判定语义与单件模式完全一致。
*/
private static final String CATEGORY_BATCH_ENVELOPE =
"\n\n# 批量模式(本次输入包含多件待分类商品)\n"
+ "输入按编号 [1]、[2]… 列出多件商品,备选类目列表对全部商品共用。"
+ "对每件商品**分别**按上述规则判定一级与二级类目,禁止遗漏编号、禁止把多件商品合并成一条。\n"
+ "只输出一个 JSON 对象:{\"items\":[{\"i\":1,\"first\":\"一级名称\",\"second\":\"二级名称或无\"}]}\n"
+ "* i=商品编号;first/second=按上述规则对该件商品的判定,回退机制与单件模式一致(某层匹配不到填\"无\")。\n"
+ "* 严禁伪造类目名称,first/second 必须逐字来自候选列表。输出必须严格遵循 JSON 语法。";
private static final String USER_TEXT_CONFORM =
"标题:%s\nSKU:%s\n违规类目产品:一级类目:%s ,二级类目:%s,三级类目:%s";
private static final String USER_TEXT_COMPARE = "ASIN:%s\n所属类目:%s\n产品信息:%s";
@@ -199,15 +213,20 @@ public class SimilarAsinLlmService {
ExecutorService executor = rowExecutor();
// 类目树整批加载一次供本批所有行复用:逐行现拼两层要 27 次 SQL/行,比省下的那次 LLM 调用更贵。
CategoryTree categoryTree = categorySwitch ? CategoryTree.load(productCategoryService.list()) : null;
// 批量分类预计算:树文本整批只随一次请求发送(llmCategoryBatchSize>1 且多行时启用),
// 关闭或单行时为 null,走逐行调用旧路径。见 matchCategoryPathsBatched。
CategoryPrepass prepass = categorySwitch ? matchCategoryPathsBatched(rows, categoryTree, apiKey) : null;
// 行级处理跑在 worker 线程:把「当前任务归属用户」快照带过去,供 LLM 请求计次
SecretUsageContext.Context usageContext = SecretUsageContext.snapshot();
List<CompletableFuture<SimilarAsinResultRowDto>> futures = new ArrayList<>(rows.size());
for (SimilarAsinResultRowDto row : rows) {
for (int i = 0; i < rows.size(); i++) {
SimilarAsinResultRowDto row = rows.get(i);
int rowIndex = i;
futures.add(CompletableFuture.supplyAsync(SecretUsageContext.wrap(() -> {
try {
concurrency.acquire();
try {
return inspectRow(row, prompt, apiKey, imgSwitch, categorySwitch, categoryTree);
return inspectRow(row, rowIndex, prepass, prompt, apiKey, imgSwitch, categorySwitch, categoryTree);
} finally {
concurrency.release();
}
@@ -231,6 +250,8 @@ public class SimilarAsinLlmService {
}
private SimilarAsinResultRowDto inspectRow(SimilarAsinResultRowDto source,
int rowIndex,
CategoryPrepass prepass,
String prompt,
String apiKey,
boolean imgSwitch,
@@ -238,7 +259,7 @@ public class SimilarAsinLlmService {
CategoryTree categoryTree) {
SimilarAsinResultRowDto row = copyRow(source);
try {
inspectRowInternal(row, prompt, apiKey, imgSwitch, categorySwitch, categoryTree);
inspectRowInternal(row, rowIndex, prepass, prompt, apiKey, imgSwitch, categorySwitch, categoryTree);
return row;
} catch (Exception ex) {
log.warn("[similar-asin][llm] row failed asin={} title={} err={}",
@@ -248,6 +269,8 @@ public class SimilarAsinLlmService {
}
private void inspectRowInternal(SimilarAsinResultRowDto row,
int rowIndex,
CategoryPrepass prepass,
String prompt,
String apiKey,
boolean imgSwitch,
@@ -265,7 +288,8 @@ public class SimilarAsinLlmService {
}
// --- 2. 类目匹配:一次调用出一级+二级,三级候选走内存树(141783/1548215/1288150)---
CategoryPath path = matchCategoryPath(row, categoryTree, apiKey);
// 批量启用时直接用预计算结果(树文本已随批量请求发送);未启用走逐行单调用。
CategoryPath path = categoryPathForRow(rowIndex, prepass, row, categoryTree, apiKey);
CategoryLevel first = path.first();
CategoryLevel second = path.second();
List<CategoryLevel> thirdCandidates = isNoMatch(second)
@@ -368,6 +392,163 @@ public class SimilarAsinLlmService {
return new CategoryPath(first, second == null ? CategoryLevel.none() : second);
}
// --- 批量分类(2026-10-02):类目树文本随每行请求重复发送是分类链路最大的 token 开销 ---
/** 批量分类预计算的产物:paths 覆盖整批行(含降级为"无"的判定),errors 记录回退调用也失败的行。 */
private record CategoryPrepass(Map<Integer, CategoryPath> paths, Map<Integer, Exception> errors) {
}
/**
* 批内分类预计算:按 {@code llmCategoryBatchSize} 分块,每块一次 LLM 调用,
* 类目树文本整块只随请求发送一次。块内缺编号/坏编号的行、以及整块调用失败的行,
* 回退到逐行单调用(对齐旧语义:分类抛异常 = 行失败,而不是静默降级成"无")。
* 返回 null 表示批量未启用(阈值 &le;1 或行数 &lt;2),调用方走逐行旧路径。
*/
private CategoryPrepass matchCategoryPathsBatched(List<SimilarAsinResultRowDto> rows,
CategoryTree categoryTree, String apiKey) {
int batchSize = Math.max(1, properties.getLlmCategoryBatchSize());
if (batchSize <= 1 || rows.size() < 2) {
return null;
}
Semaphore concurrency = new Semaphore(Math.max(1, properties.getLlmRowConcurrency()));
ExecutorService executor = rowExecutor();
SecretUsageContext.Context usageContext = SecretUsageContext.snapshot();
Map<Integer, CategoryPath> paths = new ConcurrentHashMap<>();
Map<Integer, Exception> errors = new ConcurrentHashMap<>();
List<CompletableFuture<Void>> futures = new ArrayList<>();
for (int from = 0; from < rows.size(); from += batchSize) {
List<Integer> chunk = new ArrayList<>(Math.min(batchSize, rows.size() - from));
for (int i = from; i < Math.min(from + batchSize, rows.size()); i++) {
chunk.add(i);
}
futures.add(CompletableFuture.supplyAsync(SecretUsageContext.wrap(() -> {
try {
concurrency.acquire();
try {
matchCategoryChunk(rows, chunk, categoryTree, apiKey, paths, errors);
} finally {
concurrency.release();
}
} catch (InterruptedException interruptedException) {
Thread.currentThread().interrupt();
throw new IllegalStateException("LLM category batch interrupted", interruptedException);
}
return null;
}, usageContext), executor));
}
for (CompletableFuture<Void> future : futures) {
future.join(); // matchCategoryChunk 内部自捕获,join 只会因中断失败
}
return new CategoryPrepass(paths, errors);
}
private void matchCategoryChunk(List<SimilarAsinResultRowDto> rows, List<Integer> chunk,
CategoryTree tree, String apiKey,
Map<Integer, CategoryPath> paths, Map<Integer, Exception> errors) {
try {
String content = llmClient.invokeChat(properties.getLlmCategoryModel(),
SYSTEM_CATEGORY + CATEGORY_BATCH_ENVELOPE,
buildCategoryBatchUserText(rows, chunk, tree), apiKey);
JsonNode root = llmClient.parseJsonContent(content);
resolveCategoryBatchItems(rows, chunk, root, tree, apiKey, paths, errors);
} catch (Exception ex) {
log.warn("[similar-asin][llm] category batch failed rows={} firstAsin={} err={} → 整块回退单行",
chunk.size(), abbreviate(rows.get(chunk.get(0)).getAsin(), 40), failureMessage(ex));
fallbackCategorySingleRows(rows, chunk, tree, apiKey, paths, errors);
}
}
/**
* 批量结果逐项回查类目树:判定语义与单行 {@link #matchCategoryPath} 完全一致
* (一级失配=整行"无"、二级失配=保留一级);缺失/坏编号的行回退单行调用补齐。
*/
private void resolveCategoryBatchItems(List<SimilarAsinResultRowDto> rows, List<Integer> chunk,
JsonNode root, CategoryTree tree, String apiKey,
Map<Integer, CategoryPath> paths, Map<Integer, Exception> errors) {
Map<Integer, CategoryPath> parsed = new HashMap<>();
JsonNode items = root.path("items");
if (items.isArray()) {
for (JsonNode item : items) {
int i = item.path("i").asInt(-1);
if (i < 1 || i > chunk.size()) {
continue; // 坏编号:交给缺失回退
}
int rowIndex = chunk.get(i - 1);
if (parsed.containsKey(rowIndex)) {
continue; // 重复编号取首个
}
parsed.put(rowIndex, categoryPathFromNames(tree,
textOrEmpty(item, "first"), textOrEmpty(item, "second")));
}
}
for (Integer rowIndex : chunk) {
CategoryPath path = parsed.get(rowIndex);
if (path != null) {
paths.put(rowIndex, path);
} else {
fallbackCategorySingleRows(rows, List.of(rowIndex), tree, apiKey, paths, errors);
}
}
}
private CategoryPath categoryPathFromNames(CategoryTree tree, String firstName, String secondName) {
CategoryLevel first = resolveLevel(tree.roots(), firstName, "一级");
if (first == null) {
return CategoryPath.none();
}
CategoryLevel second = resolveLevel(tree.childrenOf(first.id()), secondName, "二级");
return new CategoryPath(first, second == null ? CategoryLevel.none() : second);
}
/** 批量失败的行回退逐行单调用;分类调用本身抛异常时记入 errors(行级失败语义不变)。 */
private void fallbackCategorySingleRows(List<SimilarAsinResultRowDto> rows, List<Integer> chunk,
CategoryTree tree, String apiKey,
Map<Integer, CategoryPath> paths, Map<Integer, Exception> errors) {
for (Integer rowIndex : chunk) {
if (paths.containsKey(rowIndex) || errors.containsKey(rowIndex)) {
continue;
}
try {
paths.put(rowIndex, matchCategoryPath(rows.get(rowIndex), tree, apiKey));
} catch (Exception ex) {
errors.put(rowIndex, ex);
}
}
}
/** 每行分类结果取用:优先预计算;回退也失败的行按单行语义抛出 → 行失败。 */
private CategoryPath categoryPathForRow(int rowIndex, CategoryPrepass prepass,
SimilarAsinResultRowDto row, CategoryTree tree,
String apiKey) throws Exception {
if (prepass == null) {
return matchCategoryPath(row, tree, apiKey);
}
Exception error = prepass.errors().get(rowIndex);
if (error != null) {
throw error instanceof RuntimeException runtimeException ? runtimeException
: new IllegalStateException(error.getMessage(), error);
}
CategoryPath path = prepass.paths().get(rowIndex);
if (path == null) {
throw new IllegalStateException("批量分类结果缺失 rowIndex=" + rowIndex);
}
return path;
}
private String buildCategoryBatchUserText(List<SimilarAsinResultRowDto> rows, List<Integer> chunk,
CategoryTree tree) {
StringBuilder sb = new StringBuilder();
sb.append("备选类目列表:\n").append(tree.formatTwoLevels()).append("\n\n");
sb.append("待分类商品(共 ").append(chunk.size()).append(" 件,按编号 [i] 逐件判定):\n");
for (int position = 0; position < chunk.size(); position++) {
SimilarAsinResultRowDto row = rows.get(chunk.get(position));
sb.append('[').append(position + 1).append("] 产品标题:")
.append(firstNonBlank(row.getTitle(), row.getAsin())).append('\n');
sb.append(" 产品属性:").append(normalize(row.getSku())).append('\n');
}
return sb.toString();
}
/** 按名称在给定候选内回查层级;"无"/空/编造名称一律返回 null,交由上层降级。 */
private CategoryLevel resolveLevel(List<CategoryLevel> candidates, String name, String levelLabel) {
String normalized = normalize(name);