perf(外观专利): 品牌校验整批共用一次查询与一个超时预算
checkTitleText → checkTitleTexts:一批 10 行原先各带一个 total-timeout-millis(90s) 预算串行,上游 16890 劣化时批间隔被拉到分钟级;改成跨行去重一次查询、整批共用 deadline、结论按行归因、返回与入参同序同长。真正的价值是把爆炸半径从"行"收回"批"。 改动比预估大了一圈:三个回退点(标题缺失 / 标题识别失败 / 外观识别失败)原先也在 逐行发品牌查询,而回退行查的是整条原始标题,splitTitleText 能切出十几个词,逐行发 反而最贵。现在回退行只造结论不写行,与正常行汇进同一条 (row, LlmResult) 队列, 出口收敛到 applyLlmRowsInOneBatch 一处;pending.isEmpty() 的早退分支同样要刷, 否则回退行连品牌结论都拿不到。单参 applyResult 重载已删(零调用方 + 编译验证)。 行数不变、LLM 请求次数不变、代理提取次数不变 ⇒ 零成本。语义变化只有当初拍过的那条: 同批同品牌共享一次结论。 护栏 AppearancePatentBrandBatchTest 3 例 + BrandCheckClientTitleBatchTest 4 例, 断言各守一个坑:times(1)+never(checkTitleText) 证明合批真发生、两行结论不同证明 批内归因停在行;变异自证 6/6。后端全量 3631 例 Failures 0 Errors 0。
This commit is contained in:
+74
-17
@@ -23,6 +23,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
@@ -150,17 +151,21 @@ public class AppearancePatentLlmClient {
|
||||
}
|
||||
List<AppearancePatentResultRowDto> results = new ArrayList<>(rows.size());
|
||||
List<Integer> pending = new ArrayList<>(rows.size());
|
||||
List<AppearancePatentResultRowDto> llmRows = new ArrayList<>();
|
||||
List<LlmResult> llmResults = new ArrayList<>();
|
||||
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), "");
|
||||
llmRows.add(row);
|
||||
llmResults.add(fallbackResult(row, rawTitle, firstNonBlank(rawTitle, MISSING_ROW_DATA), ""));
|
||||
} else {
|
||||
pending.add(results.size() - 1);
|
||||
}
|
||||
}
|
||||
if (pending.isEmpty()) {
|
||||
applyLlmRowsInOneBatch(llmRows, llmResults);
|
||||
return results;
|
||||
}
|
||||
Map<Integer, String> keywordsByKey = new LinkedHashMap<>();
|
||||
@@ -176,8 +181,9 @@ public class AppearancePatentLlmClient {
|
||||
}
|
||||
// 标题识别失败时本行必走回退分支(外观结果会被丢弃),再发外观请求只会在劣化中的上游白等一轮重试
|
||||
log.warn("[appearance-patent] 标题识别失败,跳过外观识别(本行同样走回退)asin={}", rows.get(index).getAsin());
|
||||
applyRowFallback(results.get(index), normalize(rows.get(index).getTitle()),
|
||||
firstNonBlank(titleErrors.get(index), "商标识别失败"), "");
|
||||
llmRows.add(results.get(index));
|
||||
llmResults.add(fallbackResult(results.get(index), normalize(rows.get(index).getTitle()),
|
||||
firstNonBlank(titleErrors.get(index), "商标识别失败"), ""));
|
||||
}
|
||||
Map<Integer, AppearanceJudgement> judgements = new LinkedHashMap<>();
|
||||
Map<Integer, String> appearanceErrors = new LinkedHashMap<>();
|
||||
@@ -189,18 +195,33 @@ public class AppearancePatentLlmClient {
|
||||
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), "外观识别失败"));
|
||||
llmRows.add(results.get(index));
|
||||
llmResults.add(fallbackResult(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(
|
||||
llmRows.add(results.get(index));
|
||||
llmResults.add(new LlmResult(
|
||||
source.getGroupKey(), source.getId(), source.getAsin(), source.getCountry(),
|
||||
keywords, judgement.status(), "", "", "", keywords, judgement.reason(), "", ""));
|
||||
}
|
||||
applyLlmRowsInOneBatch(llmRows, llmResults);
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* 整批一次品牌查询后逐行落结论:需要查的行共用一个耗时预算,
|
||||
* 结论仍按行自己的词集合归因(见 brandChecksFor)。
|
||||
*/
|
||||
private void applyLlmRowsInOneBatch(List<AppearancePatentResultRowDto> llmRows, List<LlmResult> llmResults) {
|
||||
List<BrandCheckClient.BrandCheckBatchResult> brandChecks = brandChecksFor(llmResults);
|
||||
for (int k = 0; k < llmRows.size(); k++) {
|
||||
applyResult(llmRows.get(k), llmResults.get(k), brandChecks.get(k));
|
||||
}
|
||||
}
|
||||
|
||||
private BatchOutcome<String> invokeTitleBatchSafely(List<AppearancePatentResultRowDto> rows,
|
||||
List<Integer> chunk,
|
||||
String apiKey) {
|
||||
@@ -340,12 +361,13 @@ public class AppearancePatentLlmClient {
|
||||
/**
|
||||
* 行级回退分支:与原 LLM 工作流"默认值"语义对齐——appearance=外观识别异常、
|
||||
* title 保留原始标题、title_reason/appearance_reason 填充对应错误信息。
|
||||
* 只造结论不写行,交给 applyLlmRowsInOneBatch 与正常行合用同一次品牌查询。
|
||||
*/
|
||||
private AppearancePatentResultRowDto applyRowFallback(AppearancePatentResultRowDto row,
|
||||
String title,
|
||||
String titleReason,
|
||||
String appearanceReason) {
|
||||
LlmResult result = new LlmResult(
|
||||
private LlmResult fallbackResult(AppearancePatentResultRowDto row,
|
||||
String title,
|
||||
String titleReason,
|
||||
String appearanceReason) {
|
||||
return new LlmResult(
|
||||
row.getGroupKey(),
|
||||
row.getId(),
|
||||
row.getAsin(),
|
||||
@@ -359,8 +381,6 @@ public class AppearancePatentLlmClient {
|
||||
appearanceReason,
|
||||
"",
|
||||
"");
|
||||
applyResult(row, result);
|
||||
return row;
|
||||
}
|
||||
|
||||
/** 一批行合并为一次文本请求;返回的行下标 → 关键词,仅包含通过字面校验的行。 */
|
||||
@@ -865,8 +885,9 @@ public class AppearancePatentLlmClient {
|
||||
|
||||
List<AppearancePatentResultRowDto> merged = new ArrayList<>(rows.size());
|
||||
boolean allowIndexFallback = results.size() == rows.size() && results.stream().noneMatch(this::hasIdentity);
|
||||
List<LlmResult> resolved = new ArrayList<>(rows.size());
|
||||
for (int i = 0; i < rows.size(); i++) {
|
||||
AppearancePatentResultRowDto row = copy(rows.get(i));
|
||||
AppearancePatentResultRowDto row = rows.get(i);
|
||||
LlmResult result = resultByGroupKey.get(normalize(row.getGroupKey()));
|
||||
if (result == null) {
|
||||
result = resultByCompositeKey.get(rowKey(row.getId(), row.getAsin(), row.getCountry()));
|
||||
@@ -883,7 +904,12 @@ public class AppearancePatentLlmClient {
|
||||
if (result == null && allowIndexFallback && i < results.size()) {
|
||||
result = results.get(i);
|
||||
}
|
||||
applyResult(row, result);
|
||||
resolved.add(result);
|
||||
}
|
||||
List<BrandCheckClient.BrandCheckBatchResult> brandChecks = brandChecksFor(resolved);
|
||||
for (int i = 0; i < rows.size(); i++) {
|
||||
AppearancePatentResultRowDto row = copy(rows.get(i));
|
||||
applyResult(row, resolved.get(i), brandChecks.get(i));
|
||||
merged.add(row);
|
||||
}
|
||||
return merged;
|
||||
@@ -906,14 +932,45 @@ public class AppearancePatentLlmClient {
|
||||
|| !normalize(result.asin()).isBlank();
|
||||
}
|
||||
|
||||
void applyResult(AppearancePatentResultRowDto row, LlmResult result) {
|
||||
/**
|
||||
* 一批行共用一次品牌查询:整批共用一个耗时预算、同一个词跨行只查一次,
|
||||
* 返回值与入参同序,不需要查的行返回 null。
|
||||
*
|
||||
* 预算从「每行一个」收回「每批一个」的代价是上游劣化时同批一起判查询失败,
|
||||
* 与货源侧 CollectDataBrandBatchFilter 已上线的口径一致;归因仍停在行,
|
||||
* 别行的侵权词不会渗进干净行(BrandCheckClientTitleBatchTest 守这条)。
|
||||
*/
|
||||
private List<BrandCheckClient.BrandCheckBatchResult> brandChecksFor(List<LlmResult> results) {
|
||||
List<Integer> pending = new ArrayList<>();
|
||||
List<String> titles = new ArrayList<>();
|
||||
for (int i = 0; i < results.size(); i++) {
|
||||
LlmResult result = results.get(i);
|
||||
// 与 applyResult 走品牌分支的条件严格一致:LLM 已判「无」的行结论不看品牌,
|
||||
// 替它发一次上游查询就是白烧 16890 请求。
|
||||
if (!isNoTitleRisk(result) && shouldCheckBrand(result)) {
|
||||
pending.add(i);
|
||||
titles.add(result.title());
|
||||
}
|
||||
}
|
||||
BrandCheckClient.BrandCheckBatchResult[] byRowIndex =
|
||||
new BrandCheckClient.BrandCheckBatchResult[results.size()];
|
||||
if (!pending.isEmpty()) {
|
||||
List<BrandCheckClient.BrandCheckBatchResult> batch = brandCheckClient.checkTitleTexts(titles, "Terms");
|
||||
for (int k = 0; k < pending.size(); k++) {
|
||||
byRowIndex[pending.get(k)] = batch.get(k);
|
||||
}
|
||||
}
|
||||
return Arrays.asList(byRowIndex);
|
||||
}
|
||||
|
||||
void applyResult(AppearancePatentResultRowDto row, LlmResult result,
|
||||
BrandCheckClient.BrandCheckBatchResult brandCheck) {
|
||||
if (row == null || result == null) {
|
||||
return;
|
||||
}
|
||||
if (isNoTitleRisk(result)) {
|
||||
row.setTitleRisk(NO_INFRINGEMENT);
|
||||
} else if (shouldCheckBrand(result)) {
|
||||
BrandCheckClient.BrandCheckBatchResult brandCheck = brandCheckClient.checkTitleText(result.title(), "Terms");
|
||||
row.setTitleRisk(buildTitleRisk(result.title(), brandCheck));
|
||||
} else {
|
||||
row.setTitleRisk("");
|
||||
|
||||
Reference in New Issue
Block a user