降本三条(10-11 现网取证):外观同请求图片去重 + 货源合规合批 + LLM token 计量落库
取证基线(库侧 biz_user_secret_usage_daily,10-01~10-11 实测 12,704 / 4,134 / 15,135 次): 外观 24h 全量 831 条请求逐条 json 解析,564 条带图共投 2,693 张、去重只剩 1,465 张 ⇒ 45.6% 是同一张图在同一次请求里的副本;重试占比只有 0.24%(attempt 829/2/0), 所以省钱的口子在图片副本,不在重试。货源 4,134 次里 4,096 次集中在 10-10/10-11 两天, 类目已合批(10-02)而合规与图比仍逐行 ⇒ 合规是该模块最后一步逐行 LLM 调用。 1) 外观:每张不同的图只随请求发一份,正文附「图j=序号1、2…」对照表;每个序号仍各自 出一份 status/reason(省副本,不省判定,与 10-10「同组兄弟行也要都检测」同口径)。 归一化 URL 只当去重的键,发出去的还是行里那条原始串。应急回退: llm-appearance-batch-size=1 即退回逐行,无需改码。 2) 货源:新键 llm-conform-batch-size=10(设 1 退回逐行)。前置条件是分类预计算存在, 否则整条不生效——避免同一行的类目被算两遍(两遍=多扣用户一次);缺编号行与整块 失败都逐行补检,并有一支 A/B 差分管住合批与逐行写回行的三字段+状态相等。 3) V157 给 biz_user_secret_usage_daily 加 prompt_tokens/completion_tokens,两个 client 收下 网关 usage。计次与 token 分两条语句:计次发生在发请求之前(含重试,请求没回来也算 一次),token 只在响应回来后有读数;网关没回 usage 就一条不写——0 会被读成"实测为 0"。 护栏记账:新 mapper 语句 addTokens 两处一起点名(SqlLayeringConventionTest 分母 97→98 + MigratedSqlEquivalenceTest.NOT_MIGRATED);批量路径的 catch 不新增一处——把分类那一路的 "整块失败→逐行补检"抽成 batchAttempt/fallbackSingleRows 共用,静默 catch 棘轮四格一格没放宽。 顺带修掉 AppearancePatentResultRowDto 上一句已经过期的 javadoc(还写着兄弟行复用代表行结论)。 证据:后端全量 4288 例 0 红(本批 +24 例:去重 7 / token 4 / 合规合批 7 / 用量服务 6)。 变异自证 3/3 全杀:取消去重、摘掉 usage 落库、断掉合规合批,分别让对应用例判红; 两个受试文件按注入前字节还原,sha 复算一致。
This commit is contained in:
+48
-6
@@ -107,11 +107,13 @@ public class AppearancePatentLlmClient {
|
||||
|
||||
private static final String APPEARANCE_BATCH_ENVELOPE =
|
||||
"\n\n### 批量输入输出协议\n"
|
||||
+ "本次同时提交多件商品:用户消息先给出「序号<TAB>产品描述」清单,随后按同样的序号依次附上各件商品图片。"
|
||||
+ "本次同时提交多件商品:用户消息先给出「序号<TAB>产品描述」清单,随后附上**去重后**的商品图片"
|
||||
+ "(同一张图只出现一次),并在文末「图片对照」里逐张写明它对应哪些序号(一张图可服务多件商品)。"
|
||||
+ "必须逐件独立判定,严禁把某一件的结论写到另一件上。\n"
|
||||
+ "只输出一个 JSON 对象:{\"items\":[{\"i\":1,\"appearance_status\":\"侵权\",\"appearance_reason\":\"...\"}]}\n"
|
||||
+ "* i=该件商品的序号,每件恰好一项,不得漏项、不得凭空增加项。\n"
|
||||
+ "* appearance_status / appearance_reason=按上述规则对该件商品图片给出的判定。\n"
|
||||
+ "* appearance_status / appearance_reason=按上述规则对该件商品**所对应的那张图**给出的判定;"
|
||||
+ "共用同一张图的多个序号仍要各出一项,理由要落到该序号自己的描述上。\n"
|
||||
+ "* 除 JSON 外不要输出任何内容。";
|
||||
|
||||
private static final String NO_KEYWORD = "无";
|
||||
@@ -457,17 +459,34 @@ public class AppearancePatentLlmClient {
|
||||
String prompt,
|
||||
String apiKey) throws Exception {
|
||||
List<String> lineTexts = new ArrayList<>(chunk.size());
|
||||
List<String> images = new ArrayList<>(chunk.size());
|
||||
// 同批里同一张图会被多行共用(兄弟行补空取自同一条主图,2026-10-10 起又是逐行送检),
|
||||
// 现网 24h 全量实测:一次请求内投了 2,693 张、去重只剩 1,465 张,45.6% 是同一张图的副本。
|
||||
// 所以每张不同的图只随请求发一份,行与图的对应关系写在正文「图片对照」里 ——
|
||||
// 省的是图片 token,不是判定:每个序号仍然要各自出一份 status/reason。
|
||||
Map<String, String> rawByKey = new LinkedHashMap<>();
|
||||
Map<String, String> rowsByKey = new LinkedHashMap<>();
|
||||
for (int index : chunk) {
|
||||
AppearancePatentResultRowDto row = rows.get(index);
|
||||
lineTexts.add(singleLine("产品描述:" + nonBlank(row.getTitle(), "") + nonBlank(row.getSku(), "")));
|
||||
images.add(row.getUrl());
|
||||
// 归一化只当去重的键;发出去的仍是行里那条原始 URL(口径不许被 normalize 顺手改掉)。
|
||||
String key = normalize(row.getUrl());
|
||||
rawByKey.putIfAbsent(key, row.getUrl());
|
||||
rowsByKey.merge(key, String.valueOf(positionOf(index, chunk)),
|
||||
(oldValue, newValue) -> oldValue + "、" + newValue);
|
||||
}
|
||||
List<String> images = new ArrayList<>(rawByKey.values());
|
||||
StringBuilder legend = new StringBuilder();
|
||||
int imageIndex = 0;
|
||||
for (String positions : rowsByKey.values()) {
|
||||
legend.append("\n图").append(++imageIndex).append("=序号").append(positions);
|
||||
}
|
||||
String userText = "本次提交 " + chunk.size() + " 件商品,先给出「序号<TAB>产品描述」清单,"
|
||||
+ "随后按同样的序号依次附上各件商品图片:\n" + indexedLines(lineTexts);
|
||||
+ "随后附上本批**去重后**的商品图片共 " + images.size() + " 张(一张图可能被多件商品共用,"
|
||||
+ "共用的序号见文末「图片对照」)。每件商品都必须按它对应的那张图**单独**给出一份判定。\n"
|
||||
+ indexedLines(lineTexts) + "\n图片对照:" + legend;
|
||||
Map<Integer, AppearanceJudgement> judgements = new LinkedHashMap<>();
|
||||
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()) {
|
||||
@@ -484,6 +503,11 @@ public class AppearancePatentLlmClient {
|
||||
return judgements;
|
||||
}
|
||||
|
||||
/** 该行在本批内的序号(1 基,与发给模型的「序号<TAB>…」对齐)。 */
|
||||
private static int positionOf(int index, List<Integer> chunk) {
|
||||
return chunk.indexOf(index) + 1;
|
||||
}
|
||||
|
||||
private String appearanceSystemPrompt(String prompt) {
|
||||
String system = APPEARANCE_SYSTEM_PROMPT;
|
||||
if (prompt != null && !prompt.isBlank()) {
|
||||
@@ -650,6 +674,7 @@ public class AppearancePatentLlmClient {
|
||||
String message = text(errorNode.path("message"));
|
||||
throw new IllegalStateException(firstNonBlank(message, "LLM error"));
|
||||
}
|
||||
recordTokenUsage(root);
|
||||
JsonNode contentNode = root.path("choices").path(0).path("message").path("content");
|
||||
String content = text(contentNode);
|
||||
if (content == null || content.isBlank()) {
|
||||
@@ -1246,6 +1271,23 @@ public class AppearancePatentLlmClient {
|
||||
userSecretUsageService.record(context.userId(), UserSecretModule.APPEARANCE_PATENT.key(), 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 收下网关响应的 token 读数(V157)。计次已在发请求前记过,这里只补读数。
|
||||
* 网关没回 usage 就整条跳过 —— 不许写 0 冒充「实测消耗为 0」,那种行以后会被拿去算成本。
|
||||
*/
|
||||
private void recordTokenUsage(JsonNode root) {
|
||||
JsonNode usage = root.path("usage");
|
||||
if (usage.isMissingNode() || usage.isNull()) {
|
||||
return;
|
||||
}
|
||||
SecretUsageContext.Context context = SecretUsageContext.current();
|
||||
if (context == null) {
|
||||
return;
|
||||
}
|
||||
userSecretUsageService.recordTokens(context.userId(), UserSecretModule.APPEARANCE_PATENT.key(),
|
||||
usage.path("prompt_tokens").asLong(0L), usage.path("completion_tokens").asLong(0L));
|
||||
}
|
||||
|
||||
private void sleepQuietly(long delayMillis) {
|
||||
try {
|
||||
Thread.sleep(delayMillis);
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ public class AppearancePatentResultRowDto {
|
||||
@Schema(description = "主数据分组 key。Python 应从解析结果原样透传,用于把同组子行补回。", example = "uploads/20260426/appearance_patent_17.xlsx::2@2")
|
||||
private String groupKey;
|
||||
|
||||
@Schema(description = "Excel 中的 id。代表行通常是整数 id 或 n_1,例如 2_1;最终生成 xlsx 时,2_2、2_3 会复用同组 2_1 的 LLM 检测结果。", example = "2_1")
|
||||
@Schema(description = "Excel 中的 id。代表行通常是整数 id 或 n_1,例如 2_1;2_2、2_3 这些同组兄弟行也各自送检、各自出结论(2026-10-10 撤掉了「复用代表行结论」的摊派:现网实测它造成两到五成行三列全空)。", example = "2_1")
|
||||
private String id;
|
||||
|
||||
@Schema(description = "亚马逊 ASIN。后端会统一按大写处理和匹配。", example = "B0CJ8SNXXV")
|
||||
|
||||
Reference in New Issue
Block a user