Blame view

indexer/product_enrich.py 52.3 KB
6f7840cf   tangwang   refactor: rename ...
1
2
3
4
5
6
7
8
9
10
11
  #!/usr/bin/env python3
  """
  商品内容理解与属性补充模块(product_enrich
  
  提供基于 LLM 的商品锚文本 / 语义属性 / 标签等分析能力,
   indexer  API 在内存中调用(不再负责 CSV 读写)。
  """
  
  import os
  import json
  import logging
a47416ec   tangwang   把融合逻辑改成乘法公式,并把 ES...
12
  import re
6f7840cf   tangwang   refactor: rename ...
13
14
  import time
  import hashlib
41f0b2e9   tangwang   product_enrich支持并发
15
16
  import uuid
  import threading
36516857   tangwang   feat(product_enri...
17
  from dataclasses import dataclass, field
a73a751f   tangwang   enrich
18
  from collections import OrderedDict
6f7840cf   tangwang   refactor: rename ...
19
  from datetime import datetime
41f0b2e9   tangwang   product_enrich支持并发
20
  from concurrent.futures import ThreadPoolExecutor
6f7840cf   tangwang   refactor: rename ...
21
22
23
24
25
26
  from typing import List, Dict, Tuple, Any, Optional
  
  import redis
  import requests
  from pathlib import Path
  
86d8358b   tangwang   config optimize
27
  from config.loader import get_app_config
6f7840cf   tangwang   refactor: rename ...
28
  from config.tenant_config_loader import SOURCE_LANG_CODE_MAP
a73a751f   tangwang   enrich
29
30
31
32
33
  from indexer.product_enrich_prompts import (
      SYSTEM_MESSAGE,
      USER_INSTRUCTION_TEMPLATE,
      LANGUAGE_MARKDOWN_TABLE_HEADERS,
      SHARED_ANALYSIS_INSTRUCTION,
dabd52a5   tangwang   feat(indexer): 支持...
34
      CATEGORY_TAXONOMY_PROFILES,
a73a751f   tangwang   enrich
35
  )
6f7840cf   tangwang   refactor: rename ...
36
37
38
  
  # 配置
  BATCH_SIZE = 20
41f0b2e9   tangwang   product_enrich支持并发
39
40
41
  # enrich-content LLM 批次并发 worker 上限(线程池;仅对 uncached batch 并发)
  _APP_CONFIG = get_app_config()
  CONTENT_UNDERSTANDING_MAX_WORKERS = int(_APP_CONFIG.product_enrich.max_workers)
6f7840cf   tangwang   refactor: rename ...
42
43
44
45
46
47
48
49
50
  # 华北2(北京):https://dashscope.aliyuncs.com/compatible-mode/v1
  # 新加坡:https://dashscope-intl.aliyuncs.com/compatible-mode/v1
  # 美国(弗吉尼亚):https://dashscope-us.aliyuncs.com/compatible-mode/v1
  API_BASE_URL = "https://dashscope-us.aliyuncs.com/compatible-mode/v1"
  MODEL_NAME = "qwen-flash"
  API_KEY = os.environ.get("DASHSCOPE_API_KEY")
  MAX_RETRIES = 3
  RETRY_DELAY = 5  # 秒
  REQUEST_TIMEOUT = 180  # 秒
a73a751f   tangwang   enrich
51
  LOGGED_SHARED_CONTEXT_CACHE_SIZE = 256
a47416ec   tangwang   把融合逻辑改成乘法公式,并把 ES...
52
53
54
55
  PROMPT_INPUT_MIN_ZH_CHARS = 20
  PROMPT_INPUT_MAX_ZH_CHARS = 100
  PROMPT_INPUT_MIN_WORDS = 16
  PROMPT_INPUT_MAX_WORDS = 80
6f7840cf   tangwang   refactor: rename ...
56
57
58
59
60
61
62
63
64
65
  
  # 日志路径
  OUTPUT_DIR = Path("output_logs")
  LOG_DIR = OUTPUT_DIR / "logs"
  
  # 设置独立日志(不影响全局 indexer.log)
  LOG_DIR.mkdir(parents=True, exist_ok=True)
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  log_file = LOG_DIR / f"product_enrich_{timestamp}.log"
  verbose_log_file = LOG_DIR / "product_enrich_verbose.log"
a73a751f   tangwang   enrich
66
  _logged_shared_context_keys: "OrderedDict[str, None]" = OrderedDict()
41f0b2e9   tangwang   product_enrich支持并发
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  _logged_shared_context_lock = threading.Lock()
  
  _content_understanding_executor: Optional[ThreadPoolExecutor] = None
  _content_understanding_executor_lock = threading.Lock()
  
  
  def _get_content_understanding_executor() -> ThreadPoolExecutor:
      """
      使用模块级单例线程池,避免同一进程内多次请求叠加创建线程池导致并发失控。
      """
      global _content_understanding_executor
      with _content_understanding_executor_lock:
          if _content_understanding_executor is None:
              _content_understanding_executor = ThreadPoolExecutor(
                  max_workers=CONTENT_UNDERSTANDING_MAX_WORKERS,
                  thread_name_prefix="product-enrich-llm",
              )
          return _content_understanding_executor
6f7840cf   tangwang   refactor: rename ...
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  
  # 主日志 logger:执行流程、批次信息等
  logger = logging.getLogger("product_enrich")
  logger.setLevel(logging.INFO)
  
  if not logger.handlers:
      formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
  
      file_handler = logging.FileHandler(log_file, encoding="utf-8")
      file_handler.setFormatter(formatter)
  
      stream_handler = logging.StreamHandler()
      stream_handler.setFormatter(formatter)
  
      logger.addHandler(file_handler)
      logger.addHandler(stream_handler)
  
      # 避免日志向根 logger 传播,防止写入 logs/indexer.log 等其他文件
      logger.propagate = False
  
  # 详尽日志 logger:专门记录 LLM 请求与响应
  verbose_logger = logging.getLogger("product_enrich_verbose")
  verbose_logger.setLevel(logging.INFO)
  
  if not verbose_logger.handlers:
      verbose_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
      verbose_file_handler = logging.FileHandler(verbose_log_file, encoding="utf-8")
      verbose_file_handler.setFormatter(verbose_formatter)
      verbose_logger.addHandler(verbose_file_handler)
      verbose_logger.propagate = False
  
a47416ec   tangwang   把融合逻辑改成乘法公式,并把 ES...
116
117
  logger.info("Verbose LLM logs are written to: %s", verbose_log_file)
  
6f7840cf   tangwang   refactor: rename ...
118
119
  
  # Redis 缓存(用于 anchors / 语义属性)
41f0b2e9   tangwang   product_enrich支持并发
120
  _REDIS_CONFIG = _APP_CONFIG.infrastructure.redis
86d8358b   tangwang   config optimize
121
122
  ANCHOR_CACHE_PREFIX = _REDIS_CONFIG.anchor_cache_prefix
  ANCHOR_CACHE_EXPIRE_DAYS = int(_REDIS_CONFIG.anchor_cache_expire_days)
6f7840cf   tangwang   refactor: rename ...
123
124
125
126
  _anchor_redis: Optional[redis.Redis] = None
  
  try:
      _anchor_redis = redis.Redis(
86d8358b   tangwang   config optimize
127
128
129
          host=_REDIS_CONFIG.host,
          port=_REDIS_CONFIG.port,
          password=_REDIS_CONFIG.password,
6f7840cf   tangwang   refactor: rename ...
130
          decode_responses=True,
86d8358b   tangwang   config optimize
131
132
133
          socket_timeout=_REDIS_CONFIG.socket_timeout,
          socket_connect_timeout=_REDIS_CONFIG.socket_connect_timeout,
          retry_on_timeout=_REDIS_CONFIG.retry_on_timeout,
6f7840cf   tangwang   refactor: rename ...
134
135
136
137
138
139
140
141
          health_check_interval=10,
      )
      _anchor_redis.ping()
      logger.info("Redis cache initialized for product anchors and semantic attributes")
  except Exception as e:
      logger.warning(f"Failed to initialize Redis for anchors cache: {e}")
      _anchor_redis = None
  
a73a751f   tangwang   enrich
142
143
144
145
146
  _missing_prompt_langs = sorted(set(SOURCE_LANG_CODE_MAP) - set(LANGUAGE_MARKDOWN_TABLE_HEADERS))
  if _missing_prompt_langs:
      raise RuntimeError(
          f"Missing product_enrich prompt config for languages: {_missing_prompt_langs}"
      )
6f7840cf   tangwang   refactor: rename ...
147
148
  
  
69881ecb   tangwang   相关性调参、enrich内容解析优化
149
150
  # 多值字段分隔:英文逗号、中文逗号、顿号,及历史约定的 ; | / 与空白
  _MULTI_VALUE_FIELD_SPLIT_RE = re.compile(r"[,、,;|/\n\t]+")
d350861f   tangwang   索引结构修改
151
  _CORE_INDEX_LANGUAGES = ("zh", "en")
2703b6ea   tangwang   refactor(indexer)...
152
153
  _DEFAULT_ENRICHMENT_SCOPES = ("generic", "category_taxonomy")
  _DEFAULT_CATEGORY_TAXONOMY_PROFILE = "apparel"
36516857   tangwang   feat(product_enri...
154
  _CONTENT_ANALYSIS_ATTRIBUTE_FIELD_MAP = (
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
155
156
157
158
159
160
161
162
      ("tags", "enriched_tags"),
      ("target_audience", "target_audience"),
      ("usage_scene", "usage_scene"),
      ("season", "season"),
      ("key_attributes", "key_attributes"),
      ("material", "material"),
      ("features", "features"),
  )
36516857   tangwang   feat(product_enri...
163
  _CONTENT_ANALYSIS_RESULT_FIELDS = (
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
164
165
166
167
168
169
170
171
172
173
174
      "title",
      "category_path",
      "tags",
      "target_audience",
      "usage_scene",
      "season",
      "key_attributes",
      "material",
      "features",
      "anchor_text",
  )
36516857   tangwang   feat(product_enri...
175
  _CONTENT_ANALYSIS_MEANINGFUL_FIELDS = (
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
176
      "tags",
d350861f   tangwang   索引结构修改
177
178
179
180
181
182
      "target_audience",
      "usage_scene",
      "season",
      "key_attributes",
      "material",
      "features",
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
183
      "anchor_text",
d350861f   tangwang   索引结构修改
184
  )
36516857   tangwang   feat(product_enri...
185
  _CONTENT_ANALYSIS_FIELD_ALIASES = {
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
186
187
      "tags": ("tags", "enriched_tags"),
  }
36516857   tangwang   feat(product_enri...
188
  _CONTENT_ANALYSIS_QUALITY_FIELDS = ("title", "category_path", "anchor_text")
36516857   tangwang   feat(product_enri...
189
190
191
192
193
194
195
196
197
  
  
  @dataclass(frozen=True)
  class AnalysisSchema:
      name: str
      shared_instruction: str
      markdown_table_headers: Dict[str, List[str]]
      result_fields: Tuple[str, ...]
      meaningful_fields: Tuple[str, ...]
dabd52a5   tangwang   feat(indexer): 支持...
198
      output_languages: Tuple[str, ...] = ("zh", "en")
5aaf0c7d   tangwang   feat(indexer): 完善...
199
      cache_version: str = "v1"
36516857   tangwang   feat(product_enri...
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
      field_aliases: Dict[str, Tuple[str, ...]] = field(default_factory=dict)
      fallback_headers: Optional[List[str]] = None
      quality_fields: Tuple[str, ...] = ()
  
      def get_headers(self, target_lang: str) -> Optional[List[str]]:
          headers = self.markdown_table_headers.get(target_lang)
          if headers:
              return headers
          if self.fallback_headers:
              return self.fallback_headers
          return None
  
  
  _ANALYSIS_SCHEMAS: Dict[str, AnalysisSchema] = {
      "content": AnalysisSchema(
          name="content",
          shared_instruction=SHARED_ANALYSIS_INSTRUCTION,
          markdown_table_headers=LANGUAGE_MARKDOWN_TABLE_HEADERS,
          result_fields=_CONTENT_ANALYSIS_RESULT_FIELDS,
          meaningful_fields=_CONTENT_ANALYSIS_MEANINGFUL_FIELDS,
dabd52a5   tangwang   feat(indexer): 支持...
220
          output_languages=_CORE_INDEX_LANGUAGES,
5aaf0c7d   tangwang   feat(indexer): 完善...
221
          cache_version="v2",
36516857   tangwang   feat(product_enri...
222
223
224
          field_aliases=_CONTENT_ANALYSIS_FIELD_ALIASES,
          quality_fields=_CONTENT_ANALYSIS_QUALITY_FIELDS,
      ),
2703b6ea   tangwang   refactor(indexer)...
225
226
  }
  
dabd52a5   tangwang   feat(indexer): 支持...
227
228
229
230
231
232
233
234
235
236
  def _build_taxonomy_profile_schema(profile: str, config: Dict[str, Any]) -> AnalysisSchema:
      result_fields = tuple(field["key"] for field in config["fields"])
      headers = config["markdown_table_headers"]
      return AnalysisSchema(
          name=f"taxonomy:{profile}",
          shared_instruction=config["shared_instruction"],
          markdown_table_headers=headers,
          result_fields=result_fields,
          meaningful_fields=result_fields,
          output_languages=tuple(config["output_languages"]),
5aaf0c7d   tangwang   feat(indexer): 完善...
237
          cache_version="v1",
dabd52a5   tangwang   feat(indexer): 支持...
238
239
240
241
242
243
244
          fallback_headers=headers.get("en") if len(headers) > 1 else None,
      )
  
  
  _CATEGORY_TAXONOMY_PROFILE_SCHEMAS: Dict[str, AnalysisSchema] = {
      profile: _build_taxonomy_profile_schema(profile, config)
      for profile, config in CATEGORY_TAXONOMY_PROFILES.items()
36516857   tangwang   feat(product_enri...
245
246
  }
  
2703b6ea   tangwang   refactor(indexer)...
247
  _CATEGORY_TAXONOMY_PROFILE_ATTRIBUTE_FIELD_MAPS: Dict[str, Tuple[Tuple[str, str], ...]] = {
dabd52a5   tangwang   feat(indexer): 支持...
248
249
      profile: tuple((field["key"], field["label"]) for field in config["fields"])
      for profile, config in CATEGORY_TAXONOMY_PROFILES.items()
2703b6ea   tangwang   refactor(indexer)...
250
251
  }
  
36516857   tangwang   feat(product_enri...
252
  
dabd52a5   tangwang   feat(indexer): 支持...
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
  def get_supported_category_taxonomy_profiles() -> Tuple[str, ...]:
      return tuple(_CATEGORY_TAXONOMY_PROFILE_SCHEMAS.keys())
  
  
  def _normalize_category_hint(text: Any) -> str:
      value = str(text or "").strip().lower()
      if not value:
          return ""
      value = value.replace("_", " ").replace(">", " ").replace("/", " ")
      value = re.sub(r"\s+", " ", value)
      return value
  
  
  _CATEGORY_TAXONOMY_PROFILE_ALIAS_MATCHERS: Tuple[Tuple[str, str], ...] = tuple(
      sorted(
          (
              (_normalize_category_hint(alias), profile)
              for profile, config in CATEGORY_TAXONOMY_PROFILES.items()
              for alias in (profile, *tuple(config.get("aliases") or ()))
              if _normalize_category_hint(alias)
          ),
          key=lambda item: len(item[0]),
          reverse=True,
      )
  )
  
  
2703b6ea   tangwang   refactor(indexer)...
280
281
282
  def _normalize_category_taxonomy_profile(category_taxonomy_profile: Optional[str] = None) -> str:
      profile = str(category_taxonomy_profile or _DEFAULT_CATEGORY_TAXONOMY_PROFILE).strip()
      if profile not in _CATEGORY_TAXONOMY_PROFILE_SCHEMAS:
dabd52a5   tangwang   feat(indexer): 支持...
283
284
285
286
          supported = ", ".join(get_supported_category_taxonomy_profiles())
          raise ValueError(
              f"Unsupported category_taxonomy_profile: {profile}. Supported profiles: {supported}"
          )
2703b6ea   tangwang   refactor(indexer)...
287
      return profile
69881ecb   tangwang   相关性调参、enrich内容解析优化
288
289
  
  
dabd52a5   tangwang   feat(indexer): 支持...
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
  def detect_category_taxonomy_profile(item: Dict[str, Any]) -> Optional[str]:
      """
      根据商品已有类目信息猜测 taxonomy profile
      未命中时返回 None,由上层决定是否回退到默认 profile
      """
      category_hints = (
          item.get("category_taxonomy_profile"),
          item.get("category1_name"),
          item.get("category_name_text"),
          item.get("category"),
          item.get("category_path"),
      )
      for hint in category_hints:
          normalized_hint = _normalize_category_hint(hint)
          if not normalized_hint:
              continue
          for alias, profile in _CATEGORY_TAXONOMY_PROFILE_ALIAS_MATCHERS:
              if alias and alias in normalized_hint:
                  return profile
      return None
  
  
  def _resolve_category_taxonomy_profile(
      item: Dict[str, Any],
      fallback_profile: Optional[str] = None,
  ) -> str:
      explicit_profile = str(item.get("category_taxonomy_profile") or "").strip()
      if explicit_profile:
          return _normalize_category_taxonomy_profile(explicit_profile)
      detected_profile = detect_category_taxonomy_profile(item)
      if detected_profile:
          return detected_profile
      return _normalize_category_taxonomy_profile(fallback_profile)
  
  
2703b6ea   tangwang   refactor(indexer)...
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
  def _get_analysis_schema(
      analysis_kind: str,
      *,
      category_taxonomy_profile: Optional[str] = None,
  ) -> AnalysisSchema:
      if analysis_kind == "content":
          return _ANALYSIS_SCHEMAS["content"]
      if analysis_kind == "taxonomy":
          profile = _normalize_category_taxonomy_profile(category_taxonomy_profile)
          return _CATEGORY_TAXONOMY_PROFILE_SCHEMAS[profile]
      raise ValueError(f"Unsupported analysis_kind: {analysis_kind}")
  
  
  def _get_taxonomy_attribute_field_map(
      category_taxonomy_profile: Optional[str] = None,
  ) -> Tuple[Tuple[str, str], ...]:
      profile = _normalize_category_taxonomy_profile(category_taxonomy_profile)
      return _CATEGORY_TAXONOMY_PROFILE_ATTRIBUTE_FIELD_MAPS[profile]
  
  
dabd52a5   tangwang   feat(indexer): 支持...
345
346
347
348
349
350
351
352
353
354
355
  def _get_analysis_output_languages(
      analysis_kind: str,
      *,
      category_taxonomy_profile: Optional[str] = None,
  ) -> Tuple[str, ...]:
      return _get_analysis_schema(
          analysis_kind,
          category_taxonomy_profile=category_taxonomy_profile,
      ).output_languages
  
  
2703b6ea   tangwang   refactor(indexer)...
356
357
  def _normalize_enrichment_scopes(
      enrichment_scopes: Optional[List[str]] = None,
5aaf0c7d   tangwang   feat(indexer): 完善...
358
  ) -> Tuple[str, ...]:
2703b6ea   tangwang   refactor(indexer)...
359
      requested = _DEFAULT_ENRICHMENT_SCOPES if not enrichment_scopes else tuple(enrichment_scopes)
5aaf0c7d   tangwang   feat(indexer): 完善...
360
361
      normalized: List[str] = []
      seen = set()
2703b6ea   tangwang   refactor(indexer)...
362
363
364
365
366
      for enrichment_scope in requested:
          scope = str(enrichment_scope).strip()
          if scope not in {"generic", "category_taxonomy"}:
              raise ValueError(f"Unsupported enrichment_scope: {scope}")
          if scope in seen:
5aaf0c7d   tangwang   feat(indexer): 完善...
367
              continue
2703b6ea   tangwang   refactor(indexer)...
368
369
          seen.add(scope)
          normalized.append(scope)
5aaf0c7d   tangwang   feat(indexer): 完善...
370
371
372
      return tuple(normalized)
  
  
69881ecb   tangwang   相关性调参、enrich内容解析优化
373
374
375
376
377
378
379
380
381
382
  def split_multi_value_field(text: Optional[str]) -> List[str]:
      """将 LLM/业务中的多值字符串拆成短语列表(strip 后去空)。"""
      if text is None:
          return []
      s = str(text).strip()
      if not s:
          return []
      return [p.strip() for p in _MULTI_VALUE_FIELD_SPLIT_RE.split(s) if p.strip()]
  
  
d350861f   tangwang   索引结构修改
383
384
385
386
387
388
389
390
391
392
  def _append_lang_phrase_map(target: Dict[str, List[str]], lang: str, raw_value: Any) -> None:
      parts = split_multi_value_field(raw_value)
      if not parts:
          return
      existing = target.get(lang) or []
      merged = list(dict.fromkeys([str(x).strip() for x in existing if str(x).strip()] + parts))
      if merged:
          target[lang] = merged
  
  
80f1e036   tangwang   enriched_attribut...
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
  def _get_or_create_named_value_entry(
      target: List[Dict[str, Any]],
      name: str,
      *,
      default_value: Optional[Dict[str, Any]] = None,
  ) -> Dict[str, Any]:
      for item in target:
          if item.get("name") == name:
              value = item.get("value")
              if isinstance(value, dict):
                  return item
              break
  
      entry = {"name": name, "value": default_value or {}}
      target.append(entry)
      return entry
  
  
  def _append_named_lang_phrase_map(
d350861f   tangwang   索引结构修改
412
413
414
415
416
      target: List[Dict[str, Any]],
      name: str,
      lang: str,
      raw_value: Any,
  ) -> None:
80f1e036   tangwang   enriched_attribut...
417
418
      entry = _get_or_create_named_value_entry(target, name=name, default_value={})
      _append_lang_phrase_map(entry["value"], lang=lang, raw_value=raw_value)
d350861f   tangwang   索引结构修改
419
420
  
  
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
421
422
423
424
  def _get_product_id(product: Dict[str, Any]) -> str:
      return str(product.get("id") or product.get("spu_id") or "").strip()
  
  
36516857   tangwang   feat(product_enri...
425
426
  def _get_analysis_field_aliases(field_name: str, schema: AnalysisSchema) -> Tuple[str, ...]:
      return schema.field_aliases.get(field_name, (field_name,))
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
427
428
  
  
36516857   tangwang   feat(product_enri...
429
430
  def _get_analysis_field_value(row: Dict[str, Any], field_name: str, schema: AnalysisSchema) -> Any:
      for alias in _get_analysis_field_aliases(field_name, schema):
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
          if alias in row:
              return row.get(alias)
      return None
  
  
  def _has_meaningful_value(value: Any) -> bool:
      if value is None:
          return False
      if isinstance(value, str):
          return bool(value.strip())
      if isinstance(value, dict):
          return any(_has_meaningful_value(v) for v in value.values())
      if isinstance(value, list):
          return any(_has_meaningful_value(v) for v in value)
      return bool(value)
  
  
  def _make_empty_analysis_result(
      product: Dict[str, Any],
      target_lang: str,
36516857   tangwang   feat(product_enri...
451
      schema: AnalysisSchema,
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
452
453
454
455
456
457
458
      error: Optional[str] = None,
  ) -> Dict[str, Any]:
      result = {
          "id": _get_product_id(product),
          "lang": target_lang,
          "title_input": str(product.get("title") or "").strip(),
      }
36516857   tangwang   feat(product_enri...
459
      for field in schema.result_fields:
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
460
461
462
463
464
465
466
467
468
469
          result[field] = ""
      if error:
          result["error"] = error
      return result
  
  
  def _normalize_analysis_result(
      result: Dict[str, Any],
      product: Dict[str, Any],
      target_lang: str,
36516857   tangwang   feat(product_enri...
470
      schema: AnalysisSchema,
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
471
  ) -> Dict[str, Any]:
36516857   tangwang   feat(product_enri...
472
      normalized = _make_empty_analysis_result(product, target_lang, schema)
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
473
474
475
476
      if not isinstance(result, dict):
          return normalized
  
      normalized["lang"] = str(result.get("lang") or target_lang).strip() or target_lang
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
477
478
479
480
      normalized["title_input"] = str(
          product.get("title") or result.get("title_input") or ""
      ).strip()
  
36516857   tangwang   feat(product_enri...
481
482
      for field in schema.result_fields:
          normalized[field] = str(_get_analysis_field_value(result, field, schema) or "").strip()
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
483
484
485
486
487
488
  
      if result.get("error"):
          normalized["error"] = str(result.get("error"))
      return normalized
  
  
36516857   tangwang   feat(product_enri...
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
  def _has_meaningful_analysis_content(result: Dict[str, Any], schema: AnalysisSchema) -> bool:
      return any(_has_meaningful_value(result.get(field)) for field in schema.meaningful_fields)
  
  
  def _append_analysis_attributes(
      target: List[Dict[str, Any]],
      row: Dict[str, Any],
      lang: str,
      schema: AnalysisSchema,
      field_map: Tuple[Tuple[str, str], ...],
  ) -> None:
      for source_name, output_name in field_map:
          raw = _get_analysis_field_value(row, source_name, schema)
          if not raw:
              continue
          _append_named_lang_phrase_map(
              target,
              name=output_name,
              lang=lang,
              raw_value=raw,
          )
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
510
511
  
  
d350861f   tangwang   索引结构修改
512
513
514
515
  def _apply_index_content_row(result: Dict[str, Any], row: Dict[str, Any], lang: str) -> None:
      if not row or row.get("error"):
          return
  
36516857   tangwang   feat(product_enri...
516
517
      content_schema = _get_analysis_schema("content")
      anchor_text = str(_get_analysis_field_value(row, "anchor_text", content_schema) or "").strip()
d350861f   tangwang   索引结构修改
518
519
520
      if anchor_text:
          _append_lang_phrase_map(result["qanchors"], lang=lang, raw_value=anchor_text)
  
36516857   tangwang   feat(product_enri...
521
522
      for source_name, output_name in _CONTENT_ANALYSIS_ATTRIBUTE_FIELD_MAP:
          raw = _get_analysis_field_value(row, source_name, content_schema)
d350861f   tangwang   索引结构修改
523
524
          if not raw:
              continue
80f1e036   tangwang   enriched_attribut...
525
          _append_named_lang_phrase_map(
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
526
527
528
529
530
531
              result["enriched_attributes"],
              name=output_name,
              lang=lang,
              raw_value=raw,
          )
          if output_name == "enriched_tags":
d350861f   tangwang   索引结构修改
532
533
534
              _append_lang_phrase_map(result["enriched_tags"], lang=lang, raw_value=raw)
  
  
2703b6ea   tangwang   refactor(indexer)...
535
536
537
538
539
540
541
  def _apply_index_taxonomy_row(
      result: Dict[str, Any],
      row: Dict[str, Any],
      lang: str,
      *,
      category_taxonomy_profile: Optional[str] = None,
  ) -> None:
36516857   tangwang   feat(product_enri...
542
543
544
545
546
547
548
      if not row or row.get("error"):
          return
  
      _append_analysis_attributes(
          result["enriched_taxonomy_attributes"],
          row=row,
          lang=lang,
2703b6ea   tangwang   refactor(indexer)...
549
550
551
552
553
          schema=_get_analysis_schema(
              "taxonomy",
              category_taxonomy_profile=category_taxonomy_profile,
          ),
          field_map=_get_taxonomy_attribute_field_map(category_taxonomy_profile),
36516857   tangwang   feat(product_enri...
554
555
556
      )
  
  
d350861f   tangwang   索引结构修改
557
  def _normalize_index_content_item(item: Dict[str, Any]) -> Dict[str, str]:
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
558
      item_id = _get_product_id(item)
d350861f   tangwang   索引结构修改
559
560
561
562
563
564
      return {
          "id": item_id,
          "title": str(item.get("title") or "").strip(),
          "brief": str(item.get("brief") or "").strip(),
          "description": str(item.get("description") or "").strip(),
          "image_url": str(item.get("image_url") or "").strip(),
dabd52a5   tangwang   feat(indexer): 支持...
565
566
567
568
569
          "category": str(item.get("category") or "").strip(),
          "category_path": str(item.get("category_path") or "").strip(),
          "category_name_text": str(item.get("category_name_text") or "").strip(),
          "category1_name": str(item.get("category1_name") or "").strip(),
          "category_taxonomy_profile": str(item.get("category_taxonomy_profile") or "").strip(),
d350861f   tangwang   索引结构修改
570
571
572
573
574
575
      }
  
  
  def build_index_content_fields(
      items: List[Dict[str, Any]],
      tenant_id: Optional[str] = None,
2703b6ea   tangwang   refactor(indexer)...
576
577
      enrichment_scopes: Optional[List[str]] = None,
      category_taxonomy_profile: Optional[str] = None,
d350861f   tangwang   索引结构修改
578
579
580
581
582
583
584
585
  ) -> List[Dict[str, Any]]:
      """
      高层入口:生成与 ES mapping 对齐的内容理解字段。
  
      输入项需包含:
      - `id`  `spu_id`
      - `title`
      - 可选 `brief` / `description` / `image_url`
2703b6ea   tangwang   refactor(indexer)...
586
      - 可选 `enrichment_scopes`,默认同时执行 `generic`  `category_taxonomy`
dabd52a5   tangwang   feat(indexer): 支持...
587
588
      - 可选 `category_taxonomy_profile`;若不传,则优先根据 item 自带的类目字段推断,否则回退到默认 `apparel`
      - 可选类目提示字段:`category` / `category_path` / `category_name_text` / `category1_name`
d350861f   tangwang   索引结构修改
589
590
591
592
593
594
  
      返回项结构:
      - `id`
      - `qanchors`
      - `enriched_tags`
      - `enriched_attributes`
36516857   tangwang   feat(product_enri...
595
      - `enriched_taxonomy_attributes`
d350861f   tangwang   索引结构修改
596
597
598
599
600
601
      - 可选 `error`
  
      其中:
      - `qanchors.{lang}` 为短语数组
      - `enriched_tags.{lang}` 为标签数组
      """
2703b6ea   tangwang   refactor(indexer)...
602
      requested_enrichment_scopes = _normalize_enrichment_scopes(enrichment_scopes)
dabd52a5   tangwang   feat(indexer): 支持...
603
604
605
606
607
      fallback_taxonomy_profile = (
          _normalize_category_taxonomy_profile(category_taxonomy_profile)
          if category_taxonomy_profile
          else None
      )
d350861f   tangwang   索引结构修改
608
609
610
      normalized_items = [_normalize_index_content_item(item) for item in items]
      if not normalized_items:
          return []
dabd52a5   tangwang   feat(indexer): 支持...
611
612
613
614
615
616
617
      taxonomy_profile_by_id = {
          item["id"]: _resolve_category_taxonomy_profile(
              item,
              fallback_profile=fallback_taxonomy_profile,
          )
          for item in normalized_items
      }
d350861f   tangwang   索引结构修改
618
619
620
621
622
623
624
  
      results_by_id: Dict[str, Dict[str, Any]] = {
          item["id"]: {
              "id": item["id"],
              "qanchors": {},
              "enriched_tags": {},
              "enriched_attributes": [],
36516857   tangwang   feat(product_enri...
625
              "enriched_taxonomy_attributes": [],
d350861f   tangwang   索引结构修改
626
627
628
629
          }
          for item in normalized_items
      }
  
dabd52a5   tangwang   feat(indexer): 支持...
630
      for lang in _get_analysis_output_languages("content"):
2703b6ea   tangwang   refactor(indexer)...
631
          if "generic" in requested_enrichment_scopes:
5aaf0c7d   tangwang   feat(indexer): 完善...
632
633
634
635
636
637
638
              try:
                  rows = analyze_products(
                      products=normalized_items,
                      target_lang=lang,
                      batch_size=BATCH_SIZE,
                      tenant_id=tenant_id,
                      analysis_kind="content",
dabd52a5   tangwang   feat(indexer): 支持...
639
                      category_taxonomy_profile=fallback_taxonomy_profile,
5aaf0c7d   tangwang   feat(indexer): 完善...
640
641
642
643
644
                  )
              except Exception as e:
                  logger.warning("build_index_content_fields content enrichment failed for lang=%s: %s", lang, e)
                  for item in normalized_items:
                      results_by_id[item["id"]].setdefault("error", str(e))
d350861f   tangwang   索引结构修改
645
                  continue
36516857   tangwang   feat(product_enri...
646
  
5aaf0c7d   tangwang   feat(indexer): 完善...
647
648
649
650
651
652
653
654
655
              for row in rows or []:
                  item_id = str(row.get("id") or "").strip()
                  if not item_id or item_id not in results_by_id:
                      continue
                  if row.get("error"):
                      results_by_id[item_id].setdefault("error", row["error"])
                      continue
                  _apply_index_content_row(results_by_id[item_id], row=row, lang=lang)
  
dabd52a5   tangwang   feat(indexer): 支持...
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
      if "category_taxonomy" in requested_enrichment_scopes:
          items_by_profile: Dict[str, List[Dict[str, str]]] = {}
          for item in normalized_items:
              items_by_profile.setdefault(taxonomy_profile_by_id[item["id"]], []).append(item)
  
          for taxonomy_profile, profile_items in items_by_profile.items():
              for lang in _get_analysis_output_languages(
                  "taxonomy",
                  category_taxonomy_profile=taxonomy_profile,
              ):
                  try:
                      taxonomy_rows = analyze_products(
                          products=profile_items,
                          target_lang=lang,
                          batch_size=BATCH_SIZE,
                          tenant_id=tenant_id,
                          analysis_kind="taxonomy",
                          category_taxonomy_profile=taxonomy_profile,
                      )
                  except Exception as e:
                      logger.warning(
                          "build_index_content_fields taxonomy enrichment failed for profile=%s lang=%s: %s",
                          taxonomy_profile,
                          lang,
                          e,
                      )
                      for item in profile_items:
                          results_by_id[item["id"]].setdefault("error", str(e))
5aaf0c7d   tangwang   feat(indexer): 完善...
684
                      continue
dabd52a5   tangwang   feat(indexer): 支持...
685
686
687
688
689
690
691
692
693
694
695
696
697
698
  
                  for row in taxonomy_rows or []:
                      item_id = str(row.get("id") or "").strip()
                      if not item_id or item_id not in results_by_id:
                          continue
                      if row.get("error"):
                          results_by_id[item_id].setdefault("error", row["error"])
                          continue
                      _apply_index_taxonomy_row(
                          results_by_id[item_id],
                          row=row,
                          lang=lang,
                          category_taxonomy_profile=taxonomy_profile,
                      )
36516857   tangwang   feat(product_enri...
699
  
d350861f   tangwang   索引结构修改
700
701
702
      return [results_by_id[item["id"]] for item in normalized_items]
  
  
a47416ec   tangwang   把融合逻辑改成乘法公式,并把 ES...
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
  def _normalize_space(text: str) -> str:
      return re.sub(r"\s+", " ", (text or "").strip())
  
  
  def _contains_cjk(text: str) -> bool:
      return bool(re.search(r"[\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]", text or ""))
  
  
  def _truncate_by_chars(text: str, max_chars: int) -> str:
      return text[:max_chars].strip()
  
  
  def _truncate_by_words(text: str, max_words: int) -> str:
      words = re.findall(r"\S+", text or "")
      return " ".join(words[:max_words]).strip()
  
  
  def _detect_prompt_input_lang(text: str) -> str:
      # 简化处理:包含 CJK 时按中文类文本处理,否则统一按空格分词类语言处理。
      return "zh" if _contains_cjk(text) else "en"
  
  
  def _build_prompt_input_text(product: Dict[str, Any]) -> str:
      """
      生成真正送入 prompt 的商品文本。
  
      规则:
      - 默认使用 title
      - 若文本过短,则依次补 brief / description
      - 若文本过长,则按语言粗粒度截断
      """
      fields = [
          _normalize_space(str(product.get("title") or "")),
          _normalize_space(str(product.get("brief") or "")),
          _normalize_space(str(product.get("description") or "")),
      ]
      parts: List[str] = []
  
      def join_parts() -> str:
          return " | ".join(part for part in parts if part).strip()
  
      for field in fields:
          if not field:
              continue
          if field not in parts:
              parts.append(field)
          candidate = join_parts()
          if _detect_prompt_input_lang(candidate) == "zh":
              if len(candidate) >= PROMPT_INPUT_MIN_ZH_CHARS:
                  return _truncate_by_chars(candidate, PROMPT_INPUT_MAX_ZH_CHARS)
          else:
              if len(re.findall(r"\S+", candidate)) >= PROMPT_INPUT_MIN_WORDS:
                  return _truncate_by_words(candidate, PROMPT_INPUT_MAX_WORDS)
  
      candidate = join_parts()
      if not candidate:
          return ""
      if _detect_prompt_input_lang(candidate) == "zh":
          return _truncate_by_chars(candidate, PROMPT_INPUT_MAX_ZH_CHARS)
      return _truncate_by_words(candidate, PROMPT_INPUT_MAX_WORDS)
  
  
36516857   tangwang   feat(product_enri...
765
  def _make_analysis_cache_key(
a47416ec   tangwang   把融合逻辑改成乘法公式,并把 ES...
766
      product: Dict[str, Any],
6f7840cf   tangwang   refactor: rename ...
767
      target_lang: str,
36516857   tangwang   feat(product_enri...
768
      analysis_kind: str,
2703b6ea   tangwang   refactor(indexer)...
769
      category_taxonomy_profile: Optional[str] = None,
6f7840cf   tangwang   refactor: rename ...
770
  ) -> str:
36516857   tangwang   feat(product_enri...
771
      """构造缓存 key,仅由分析类型、prompt 实际输入文本内容与目标语言决定。"""
2703b6ea   tangwang   refactor(indexer)...
772
773
774
775
      schema = _get_analysis_schema(
          analysis_kind,
          category_taxonomy_profile=category_taxonomy_profile,
      )
a47416ec   tangwang   把融合逻辑改成乘法公式,并把 ES...
776
777
      prompt_input = _build_prompt_input_text(product)
      h = hashlib.md5(prompt_input.encode("utf-8")).hexdigest()
5aaf0c7d   tangwang   feat(indexer): 完善...
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
      prompt_contract = {
          "schema_name": schema.name,
          "cache_version": schema.cache_version,
          "system_message": SYSTEM_MESSAGE,
          "user_instruction_template": USER_INSTRUCTION_TEMPLATE,
          "shared_instruction": schema.shared_instruction,
          "assistant_headers": schema.get_headers(target_lang),
          "result_fields": schema.result_fields,
          "meaningful_fields": schema.meaningful_fields,
          "field_aliases": schema.field_aliases,
      }
      prompt_contract_hash = hashlib.md5(
          json.dumps(prompt_contract, ensure_ascii=False, sort_keys=True).encode("utf-8")
      ).hexdigest()[:12]
      return (
          f"{ANCHOR_CACHE_PREFIX}:{analysis_kind}:{prompt_contract_hash}:"
          f"{target_lang}:{prompt_input[:4]}{h}"
      )
6f7840cf   tangwang   refactor: rename ...
796
797
  
  
36516857   tangwang   feat(product_enri...
798
  def _make_anchor_cache_key(
a47416ec   tangwang   把融合逻辑改成乘法公式,并把 ES...
799
      product: Dict[str, Any],
6f7840cf   tangwang   refactor: rename ...
800
      target_lang: str,
36516857   tangwang   feat(product_enri...
801
802
803
804
805
806
807
808
  ) -> str:
      return _make_analysis_cache_key(product, target_lang, analysis_kind="content")
  
  
  def _get_cached_analysis_result(
      product: Dict[str, Any],
      target_lang: str,
      analysis_kind: str,
2703b6ea   tangwang   refactor(indexer)...
809
      category_taxonomy_profile: Optional[str] = None,
6f7840cf   tangwang   refactor: rename ...
810
811
812
  ) -> Optional[Dict[str, Any]]:
      if not _anchor_redis:
          return None
2703b6ea   tangwang   refactor(indexer)...
813
814
815
816
      schema = _get_analysis_schema(
          analysis_kind,
          category_taxonomy_profile=category_taxonomy_profile,
      )
6f7840cf   tangwang   refactor: rename ...
817
      try:
2703b6ea   tangwang   refactor(indexer)...
818
819
820
821
822
823
          key = _make_analysis_cache_key(
              product,
              target_lang,
              analysis_kind,
              category_taxonomy_profile=category_taxonomy_profile,
          )
6f7840cf   tangwang   refactor: rename ...
824
825
826
          raw = _anchor_redis.get(key)
          if not raw:
              return None
36516857   tangwang   feat(product_enri...
827
828
829
830
831
832
833
          result = _normalize_analysis_result(
              json.loads(raw),
              product=product,
              target_lang=target_lang,
              schema=schema,
          )
          if not _has_meaningful_analysis_content(result, schema):
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
834
835
              return None
          return result
6f7840cf   tangwang   refactor: rename ...
836
      except Exception as e:
36516857   tangwang   feat(product_enri...
837
          logger.warning("Failed to get %s analysis cache: %s", analysis_kind, e)
6f7840cf   tangwang   refactor: rename ...
838
839
840
          return None
  
  
36516857   tangwang   feat(product_enri...
841
842
843
844
845
846
847
848
  def _get_cached_anchor_result(
      product: Dict[str, Any],
      target_lang: str,
  ) -> Optional[Dict[str, Any]]:
      return _get_cached_analysis_result(product, target_lang, analysis_kind="content")
  
  
  def _set_cached_analysis_result(
a47416ec   tangwang   把融合逻辑改成乘法公式,并把 ES...
849
      product: Dict[str, Any],
6f7840cf   tangwang   refactor: rename ...
850
851
      target_lang: str,
      result: Dict[str, Any],
36516857   tangwang   feat(product_enri...
852
      analysis_kind: str,
2703b6ea   tangwang   refactor(indexer)...
853
      category_taxonomy_profile: Optional[str] = None,
6f7840cf   tangwang   refactor: rename ...
854
855
856
  ) -> None:
      if not _anchor_redis:
          return
2703b6ea   tangwang   refactor(indexer)...
857
858
859
860
      schema = _get_analysis_schema(
          analysis_kind,
          category_taxonomy_profile=category_taxonomy_profile,
      )
6f7840cf   tangwang   refactor: rename ...
861
      try:
36516857   tangwang   feat(product_enri...
862
863
864
865
866
867
868
          normalized = _normalize_analysis_result(
              result,
              product=product,
              target_lang=target_lang,
              schema=schema,
          )
          if not _has_meaningful_analysis_content(normalized, schema):
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
869
              return
2703b6ea   tangwang   refactor(indexer)...
870
871
872
873
874
875
          key = _make_analysis_cache_key(
              product,
              target_lang,
              analysis_kind,
              category_taxonomy_profile=category_taxonomy_profile,
          )
6f7840cf   tangwang   refactor: rename ...
876
          ttl = ANCHOR_CACHE_EXPIRE_DAYS * 24 * 3600
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
877
          _anchor_redis.setex(key, ttl, json.dumps(normalized, ensure_ascii=False))
6f7840cf   tangwang   refactor: rename ...
878
      except Exception as e:
36516857   tangwang   feat(product_enri...
879
880
881
882
883
884
885
886
887
          logger.warning("Failed to set %s analysis cache: %s", analysis_kind, e)
  
  
  def _set_cached_anchor_result(
      product: Dict[str, Any],
      target_lang: str,
      result: Dict[str, Any],
  ) -> None:
      _set_cached_analysis_result(product, target_lang, result, analysis_kind="content")
6f7840cf   tangwang   refactor: rename ...
888
889
  
  
a73a751f   tangwang   enrich
890
891
892
893
  def _build_assistant_prefix(headers: List[str]) -> str:
      header_line = "| " + " | ".join(headers) + " |"
      separator_line = "|" + "----|" * len(headers)
      return f"{header_line}\n{separator_line}\n"
6f7840cf   tangwang   refactor: rename ...
894
  
6f7840cf   tangwang   refactor: rename ...
895
  
36516857   tangwang   feat(product_enri...
896
897
  def _build_shared_context(products: List[Dict[str, str]], schema: AnalysisSchema) -> str:
      shared_context = schema.shared_instruction
6f7840cf   tangwang   refactor: rename ...
898
      for idx, product in enumerate(products, 1):
a47416ec   tangwang   把融合逻辑改成乘法公式,并把 ES...
899
900
          prompt_input = _build_prompt_input_text(product)
          shared_context += f"{idx}. {prompt_input}\n"
a73a751f   tangwang   enrich
901
      return shared_context
6f7840cf   tangwang   refactor: rename ...
902
  
6f7840cf   tangwang   refactor: rename ...
903
  
a73a751f   tangwang   enrich
904
905
906
907
908
  def _hash_text(text: str) -> str:
      return hashlib.md5((text or "").encode("utf-8")).hexdigest()[:12]
  
  
  def _mark_shared_context_logged_once(shared_context_key: str) -> bool:
41f0b2e9   tangwang   product_enrich支持并发
909
910
911
912
      with _logged_shared_context_lock:
          if shared_context_key in _logged_shared_context_keys:
              _logged_shared_context_keys.move_to_end(shared_context_key)
              return False
a73a751f   tangwang   enrich
913
  
41f0b2e9   tangwang   product_enrich支持并发
914
915
916
917
          _logged_shared_context_keys[shared_context_key] = None
          if len(_logged_shared_context_keys) > LOGGED_SHARED_CONTEXT_CACHE_SIZE:
              _logged_shared_context_keys.popitem(last=False)
          return True
6f7840cf   tangwang   refactor: rename ...
918
  
6f7840cf   tangwang   refactor: rename ...
919
  
a73a751f   tangwang   enrich
920
921
  def reset_logged_shared_context_keys() -> None:
      """测试辅助:清理已记录的共享 prompt key。"""
41f0b2e9   tangwang   product_enrich支持并发
922
923
      with _logged_shared_context_lock:
          _logged_shared_context_keys.clear()
6f7840cf   tangwang   refactor: rename ...
924
  
a73a751f   tangwang   enrich
925
926
927
928
  
  def create_prompt(
      products: List[Dict[str, str]],
      target_lang: str = "zh",
36516857   tangwang   feat(product_enri...
929
      analysis_kind: str = "content",
2703b6ea   tangwang   refactor(indexer)...
930
      category_taxonomy_profile: Optional[str] = None,
36516857   tangwang   feat(product_enri...
931
  ) -> Tuple[Optional[str], Optional[str], Optional[str]]:
a73a751f   tangwang   enrich
932
      """根据目标语言创建共享上下文、本地化输出要求和 Partial Mode assistant 前缀。"""
2703b6ea   tangwang   refactor(indexer)...
933
934
935
936
      schema = _get_analysis_schema(
          analysis_kind,
          category_taxonomy_profile=category_taxonomy_profile,
      )
36516857   tangwang   feat(product_enri...
937
      markdown_table_headers = schema.get_headers(target_lang)
a73a751f   tangwang   enrich
938
939
      if not markdown_table_headers:
          logger.warning(
36516857   tangwang   feat(product_enri...
940
941
              "Unsupported target_lang for markdown table headers: kind=%s lang=%s",
              analysis_kind,
a73a751f   tangwang   enrich
942
943
944
              target_lang,
          )
          return None, None, None
36516857   tangwang   feat(product_enri...
945
      shared_context = _build_shared_context(products, schema)
a73a751f   tangwang   enrich
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
      language_label = SOURCE_LANG_CODE_MAP.get(target_lang, target_lang)
      user_prompt = USER_INSTRUCTION_TEMPLATE.format(language=language_label).strip()
      assistant_prefix = _build_assistant_prefix(markdown_table_headers)
      return shared_context, user_prompt, assistant_prefix
  
  
  def _merge_partial_response(assistant_prefix: str, generated_content: str) -> str:
      """将 Partial Mode 的 assistant 前缀与补全文本拼成完整 markdown。"""
      generated = (generated_content or "").lstrip()
      prefix_lines = [line.strip() for line in assistant_prefix.strip().splitlines()]
      generated_lines = generated.splitlines()
  
      if generated_lines:
          first_line = generated_lines[0].strip()
          if prefix_lines and first_line == prefix_lines[0]:
              generated_lines = generated_lines[1:]
              if generated_lines and len(prefix_lines) > 1 and generated_lines[0].strip() == prefix_lines[1]:
                  generated_lines = generated_lines[1:]
          elif len(prefix_lines) > 1 and first_line == prefix_lines[1]:
              generated_lines = generated_lines[1:]
  
      suffix = "\n".join(generated_lines).lstrip("\n")
      if suffix:
          return f"{assistant_prefix}{suffix}"
      return assistant_prefix
  
  
  def call_llm(
      shared_context: str,
      user_prompt: str,
      assistant_prefix: str,
      target_lang: str = "zh",
36516857   tangwang   feat(product_enri...
978
      analysis_kind: str = "content",
a73a751f   tangwang   enrich
979
980
  ) -> Tuple[str, str]:
      """调用大模型 API(带重试机制),使用 Partial Mode 强制 markdown 表格前缀。"""
6f7840cf   tangwang   refactor: rename ...
981
982
983
984
      headers = {
          "Authorization": f"Bearer {API_KEY}",
          "Content-Type": "application/json",
      }
a73a751f   tangwang   enrich
985
986
987
      shared_context_key = _hash_text(shared_context)
      localized_tail_key = _hash_text(f"{target_lang}\n{user_prompt}\n{assistant_prefix}")
      combined_user_prompt = f"{shared_context.rstrip()}\n\n{user_prompt.strip()}"
6f7840cf   tangwang   refactor: rename ...
988
989
990
991
992
993
  
      payload = {
          "model": MODEL_NAME,
          "messages": [
              {
                  "role": "system",
a73a751f   tangwang   enrich
994
                  "content": SYSTEM_MESSAGE,
6f7840cf   tangwang   refactor: rename ...
995
996
997
              },
              {
                  "role": "user",
a73a751f   tangwang   enrich
998
999
1000
1001
1002
1003
                  "content": combined_user_prompt,
              },
              {
                  "role": "assistant",
                  "content": assistant_prefix,
                  "partial": True,
6f7840cf   tangwang   refactor: rename ...
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
              },
          ],
          "temperature": 0.3,
          "top_p": 0.8,
      }
  
      request_data = {
          "headers": {k: v for k, v in headers.items() if k != "Authorization"},
          "payload": payload,
      }
  
a73a751f   tangwang   enrich
1015
1016
1017
      if _mark_shared_context_logged_once(shared_context_key):
          logger.info(f"\n{'=' * 80}")
          logger.info(
36516857   tangwang   feat(product_enri...
1018
              "LLM Shared Context [model=%s, kind=%s, shared_key=%s, chars=%s] (logged once per process key)",
a73a751f   tangwang   enrich
1019
              MODEL_NAME,
36516857   tangwang   feat(product_enri...
1020
              analysis_kind,
a73a751f   tangwang   enrich
1021
1022
1023
1024
1025
              shared_context_key,
              len(shared_context),
          )
          logger.info("\nSystem Message:\n%s", SYSTEM_MESSAGE)
          logger.info("\nShared Context:\n%s", shared_context)
6f7840cf   tangwang   refactor: rename ...
1026
1027
  
      verbose_logger.info(f"\n{'=' * 80}")
a73a751f   tangwang   enrich
1028
      verbose_logger.info(
36516857   tangwang   feat(product_enri...
1029
          "LLM Request [model=%s, kind=%s, lang=%s, shared_key=%s, tail_key=%s]:",
a73a751f   tangwang   enrich
1030
          MODEL_NAME,
36516857   tangwang   feat(product_enri...
1031
          analysis_kind,
a73a751f   tangwang   enrich
1032
1033
1034
1035
          target_lang,
          shared_context_key,
          localized_tail_key,
      )
6f7840cf   tangwang   refactor: rename ...
1036
      verbose_logger.info(json.dumps(request_data, ensure_ascii=False, indent=2))
a73a751f   tangwang   enrich
1037
1038
1039
1040
1041
1042
      verbose_logger.info(f"\nCombined User Prompt:\n{combined_user_prompt}")
      verbose_logger.info(f"\nShared Context:\n{shared_context}")
      verbose_logger.info(f"\nLocalized Requirement:\n{user_prompt}")
      verbose_logger.info(f"\nAssistant Prefix:\n{assistant_prefix}")
  
      logger.info(
36516857   tangwang   feat(product_enri...
1043
1044
          "\nLLM Request Variant [kind=%s, lang=%s, shared_key=%s, tail_key=%s, prompt_chars=%s, prefix_chars=%s]",
          analysis_kind,
a73a751f   tangwang   enrich
1045
1046
1047
1048
1049
1050
1051
1052
          target_lang,
          shared_context_key,
          localized_tail_key,
          len(user_prompt),
          len(assistant_prefix),
      )
      logger.info("\nLocalized Requirement:\n%s", user_prompt)
      logger.info("\nAssistant Prefix:\n%s", assistant_prefix)
6f7840cf   tangwang   refactor: rename ...
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
  
      # 创建session,禁用代理
      session = requests.Session()
      session.trust_env = False  # 忽略系统代理设置
  
      try:
          # 重试机制
          for attempt in range(MAX_RETRIES):
              try:
                  response = session.post(
                      f"{API_BASE_URL}/chat/completions",
                      headers=headers,
                      json=payload,
                      timeout=REQUEST_TIMEOUT,
                      proxies={"http": None, "https": None},  # 明确禁用代理
                  )
  
                  response.raise_for_status()
                  result = response.json()
a73a751f   tangwang   enrich
1072
1073
1074
                  usage = result.get("usage") or {}
  
                  verbose_logger.info(
36516857   tangwang   feat(product_enri...
1075
                      "\nLLM Response [model=%s, kind=%s, lang=%s, shared_key=%s, tail_key=%s]:",
a73a751f   tangwang   enrich
1076
                      MODEL_NAME,
36516857   tangwang   feat(product_enri...
1077
                      analysis_kind,
a73a751f   tangwang   enrich
1078
1079
1080
1081
1082
                      target_lang,
                      shared_context_key,
                      localized_tail_key,
                  )
                  verbose_logger.info(json.dumps(result, ensure_ascii=False, indent=2))
6f7840cf   tangwang   refactor: rename ...
1083
  
a73a751f   tangwang   enrich
1084
1085
                  generated_content = result["choices"][0]["message"]["content"]
                  full_markdown = _merge_partial_response(assistant_prefix, generated_content)
6f7840cf   tangwang   refactor: rename ...
1086
  
a73a751f   tangwang   enrich
1087
                  logger.info(
36516857   tangwang   feat(product_enri...
1088
1089
                      "\nLLM Response Summary [kind=%s, lang=%s, shared_key=%s, tail_key=%s, generated_chars=%s, completion_tokens=%s, prompt_tokens=%s, total_tokens=%s]",
                      analysis_kind,
a73a751f   tangwang   enrich
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
                      target_lang,
                      shared_context_key,
                      localized_tail_key,
                      len(generated_content or ""),
                      usage.get("completion_tokens"),
                      usage.get("prompt_tokens"),
                      usage.get("total_tokens"),
                  )
                  logger.info("\nGenerated Content:\n%s", generated_content)
                  logger.info("\nMerged Markdown:\n%s", full_markdown)
6f7840cf   tangwang   refactor: rename ...
1100
  
a73a751f   tangwang   enrich
1101
1102
                  verbose_logger.info(f"\nGenerated Content:\n{generated_content}")
                  verbose_logger.info(f"\nMerged Markdown:\n{full_markdown}")
6f7840cf   tangwang   refactor: rename ...
1103
  
a73a751f   tangwang   enrich
1104
                  return full_markdown, json.dumps(result, ensure_ascii=False)
6f7840cf   tangwang   refactor: rename ...
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
  
              except requests.exceptions.ProxyError as e:
                  logger.warning(f"Attempt {attempt + 1}/{MAX_RETRIES}: Proxy error - {str(e)}")
                  if attempt < MAX_RETRIES - 1:
                      logger.info(f"Retrying in {RETRY_DELAY} seconds...")
                      time.sleep(RETRY_DELAY)
                  else:
                      raise
  
              except requests.exceptions.RequestException as e:
                  logger.warning(f"Attempt {attempt + 1}/{MAX_RETRIES}: Request error - {str(e)}")
                  if attempt < MAX_RETRIES - 1:
                      logger.info(f"Retrying in {RETRY_DELAY} seconds...")
                      time.sleep(RETRY_DELAY)
                  else:
                      raise
  
              except Exception as e:
                  logger.error(f"Unexpected error on attempt {attempt + 1}/{MAX_RETRIES}: {str(e)}")
                  if attempt < MAX_RETRIES - 1:
                      logger.info(f"Retrying in {RETRY_DELAY} seconds...")
                      time.sleep(RETRY_DELAY)
                  else:
                      raise
  
      finally:
          session.close()
  
  
36516857   tangwang   feat(product_enri...
1134
1135
1136
  def parse_markdown_table(
      markdown_content: str,
      analysis_kind: str = "content",
2703b6ea   tangwang   refactor(indexer)...
1137
      category_taxonomy_profile: Optional[str] = None,
36516857   tangwang   feat(product_enri...
1138
  ) -> List[Dict[str, str]]:
6f7840cf   tangwang   refactor: rename ...
1139
      """解析markdown表格内容"""
2703b6ea   tangwang   refactor(indexer)...
1140
1141
1142
1143
      schema = _get_analysis_schema(
          analysis_kind,
          category_taxonomy_profile=category_taxonomy_profile,
      )
6f7840cf   tangwang   refactor: rename ...
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
      lines = markdown_content.strip().split("\n")
      data = []
      data_started = False
  
      for line in lines:
          line = line.strip()
          if not line:
              continue
  
          # 表格行处理
          if line.startswith("|"):
              # 分隔行(---- 或 :---: 等;允许空格,如 "| ---- | ---- |")
              sep_chars = line.replace("|", "").strip().replace(" ", "")
              if sep_chars and set(sep_chars) <= {"-", ":"}:
                  data_started = True
                  continue
  
              # 首个表头行:无论语言如何,统一跳过
              if not data_started:
                  # 等待下一行数据行
                  continue
  
              # 解析数据行
              parts = [p.strip() for p in line.split("|")]
36516857   tangwang   feat(product_enri...
1168
1169
1170
1171
              if parts and parts[0] == "":
                  parts = parts[1:]
              if parts and parts[-1] == "":
                  parts = parts[:-1]
6f7840cf   tangwang   refactor: rename ...
1172
1173
  
              if len(parts) >= 2:
36516857   tangwang   feat(product_enri...
1174
1175
1176
                  row = {"seq_no": parts[0]}
                  for field_index, field_name in enumerate(schema.result_fields, start=1):
                      row[field_name] = parts[field_index] if len(parts) > field_index else ""
6f7840cf   tangwang   refactor: rename ...
1177
1178
1179
1180
1181
                  data.append(row)
  
      return data
  
  
a73a751f   tangwang   enrich
1182
1183
1184
1185
1186
  def _log_parsed_result_quality(
      batch_data: List[Dict[str, str]],
      parsed_results: List[Dict[str, str]],
      target_lang: str,
      batch_num: int,
36516857   tangwang   feat(product_enri...
1187
      analysis_kind: str,
2703b6ea   tangwang   refactor(indexer)...
1188
      category_taxonomy_profile: Optional[str] = None,
a73a751f   tangwang   enrich
1189
  ) -> None:
2703b6ea   tangwang   refactor(indexer)...
1190
1191
1192
1193
      schema = _get_analysis_schema(
          analysis_kind,
          category_taxonomy_profile=category_taxonomy_profile,
      )
a73a751f   tangwang   enrich
1194
1195
1196
1197
      expected = len(batch_data)
      actual = len(parsed_results)
      if actual != expected:
          logger.warning(
36516857   tangwang   feat(product_enri...
1198
1199
              "Parsed row count mismatch for kind=%s batch=%s lang=%s: expected=%s actual=%s",
              analysis_kind,
a73a751f   tangwang   enrich
1200
1201
1202
1203
1204
1205
              batch_num,
              target_lang,
              expected,
              actual,
          )
  
36516857   tangwang   feat(product_enri...
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
      if not schema.quality_fields:
          logger.info(
              "Parsed Quality Summary [kind=%s, batch=%s, lang=%s]: rows=%s/%s",
              analysis_kind,
              batch_num,
              target_lang,
              actual,
              expected,
          )
          return
a73a751f   tangwang   enrich
1216
  
36516857   tangwang   feat(product_enri...
1217
1218
1219
1220
1221
      missing_summary = ", ".join(
          f"missing_{field}="
          f"{sum(1 for item in parsed_results if not str(item.get(field) or '').strip())}"
          for field in schema.quality_fields
      )
a73a751f   tangwang   enrich
1222
      logger.info(
36516857   tangwang   feat(product_enri...
1223
1224
          "Parsed Quality Summary [kind=%s, batch=%s, lang=%s]: rows=%s/%s, %s",
          analysis_kind,
a73a751f   tangwang   enrich
1225
1226
1227
1228
          batch_num,
          target_lang,
          actual,
          expected,
36516857   tangwang   feat(product_enri...
1229
          missing_summary,
a73a751f   tangwang   enrich
1230
1231
1232
      )
  
  
6f7840cf   tangwang   refactor: rename ...
1233
1234
1235
1236
  def process_batch(
      batch_data: List[Dict[str, str]],
      batch_num: int,
      target_lang: str = "zh",
36516857   tangwang   feat(product_enri...
1237
      analysis_kind: str = "content",
2703b6ea   tangwang   refactor(indexer)...
1238
      category_taxonomy_profile: Optional[str] = None,
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
1239
  ) -> List[Dict[str, Any]]:
6f7840cf   tangwang   refactor: rename ...
1240
      """处理一个批次的数据"""
2703b6ea   tangwang   refactor(indexer)...
1241
1242
1243
1244
      schema = _get_analysis_schema(
          analysis_kind,
          category_taxonomy_profile=category_taxonomy_profile,
      )
6f7840cf   tangwang   refactor: rename ...
1245
      logger.info(f"\n{'#' * 80}")
36516857   tangwang   feat(product_enri...
1246
1247
1248
1249
1250
1251
      logger.info(
          "Processing Batch %s (%s items, kind=%s)",
          batch_num,
          len(batch_data),
          analysis_kind,
      )
6f7840cf   tangwang   refactor: rename ...
1252
1253
  
      # 创建提示词
a73a751f   tangwang   enrich
1254
1255
1256
      shared_context, user_prompt, assistant_prefix = create_prompt(
          batch_data,
          target_lang=target_lang,
36516857   tangwang   feat(product_enri...
1257
          analysis_kind=analysis_kind,
2703b6ea   tangwang   refactor(indexer)...
1258
          category_taxonomy_profile=category_taxonomy_profile,
a73a751f   tangwang   enrich
1259
1260
1261
1262
1263
      )
  
      # 如果提示词创建失败(例如不支持的 target_lang),本次批次整体失败,不再继续调用 LLM
      if shared_context is None or user_prompt is None or assistant_prefix is None:
          logger.error(
36516857   tangwang   feat(product_enri...
1264
              "Failed to create prompt for batch %s, kind=%s, target_lang=%s; "
a73a751f   tangwang   enrich
1265
1266
              "marking entire batch as failed without calling LLM",
              batch_num,
36516857   tangwang   feat(product_enri...
1267
              analysis_kind,
a73a751f   tangwang   enrich
1268
1269
1270
              target_lang,
          )
          return [
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
1271
1272
1273
              _make_empty_analysis_result(
                  item,
                  target_lang,
36516857   tangwang   feat(product_enri...
1274
                  schema,
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
1275
1276
                  error=f"prompt_creation_failed: unsupported target_lang={target_lang}",
              )
a73a751f   tangwang   enrich
1277
1278
              for item in batch_data
          ]
6f7840cf   tangwang   refactor: rename ...
1279
1280
1281
  
      # 调用LLM
      try:
a73a751f   tangwang   enrich
1282
1283
1284
1285
1286
          raw_response, full_response_json = call_llm(
              shared_context,
              user_prompt,
              assistant_prefix,
              target_lang=target_lang,
36516857   tangwang   feat(product_enri...
1287
              analysis_kind=analysis_kind,
a73a751f   tangwang   enrich
1288
          )
6f7840cf   tangwang   refactor: rename ...
1289
1290
  
          # 解析结果
2703b6ea   tangwang   refactor(indexer)...
1291
1292
1293
1294
1295
          parsed_results = parse_markdown_table(
              raw_response,
              analysis_kind=analysis_kind,
              category_taxonomy_profile=category_taxonomy_profile,
          )
36516857   tangwang   feat(product_enri...
1296
1297
1298
1299
1300
1301
          _log_parsed_result_quality(
              batch_data,
              parsed_results,
              target_lang,
              batch_num,
              analysis_kind,
2703b6ea   tangwang   refactor(indexer)...
1302
              category_taxonomy_profile,
36516857   tangwang   feat(product_enri...
1303
          )
6f7840cf   tangwang   refactor: rename ...
1304
1305
1306
1307
1308
1309
1310
1311
  
          logger.info(f"\nParsed Results ({len(parsed_results)} items):")
          logger.info(json.dumps(parsed_results, ensure_ascii=False, indent=2))
  
          # 映射回原始ID
          results_with_ids = []
          for i, parsed_item in enumerate(parsed_results):
              if i < len(batch_data):
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
1312
1313
1314
1315
1316
                  source_product = batch_data[i]
                  result = _normalize_analysis_result(
                      parsed_item,
                      product=source_product,
                      target_lang=target_lang,
36516857   tangwang   feat(product_enri...
1317
                      schema=schema,
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
1318
                  )
6f7840cf   tangwang   refactor: rename ...
1319
                  results_with_ids.append(result)
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
1320
                  logger.info(
36516857   tangwang   feat(product_enri...
1321
1322
                      "Mapped: kind=%s seq=%s -> original_id=%s",
                      analysis_kind,
90de78aa   tangwang   enrich接口 因为接口迭代、跟...
1323
1324
1325
                      parsed_item.get("seq_no"),
                      source_product.get("id"),
                  )
6f7840cf   tangwang   refactor: rename ...
1326
1327
1328
1329
  
          # 保存批次 JSON 日志到独立文件
          batch_log = {
              "batch_num": batch_num,
36516857   tangwang   feat(product_enri...
1330
              "analysis_kind": analysis_kind,
6f7840cf   tangwang   refactor: rename ...
1331
1332
1333
1334
1335
1336
1337
1338
              "timestamp": datetime.now().isoformat(),
              "input_products": batch_data,
              "raw_response": raw_response,
              "full_response_json": full_response_json,
              "parsed_results": parsed_results,
              "final_results": results_with_ids,
          }
  
41f0b2e9   tangwang   product_enrich支持并发
1339
1340
          # 并发写 batch json 日志时,保证文件名唯一避免覆盖
          batch_call_id = uuid.uuid4().hex[:12]
36516857   tangwang   feat(product_enri...
1341
1342
1343
1344
          batch_log_file = (
              LOG_DIR
              / f"batch_{analysis_kind}_{batch_num:04d}_{timestamp}_{batch_call_id}.json"
          )
6f7840cf   tangwang   refactor: rename ...
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
          with open(batch_log_file, "w", encoding="utf-8") as f:
              json.dump(batch_log, f, ensure_ascii=False, indent=2)
  
          logger.info(f"Batch log saved to: {batch_log_file}")
  
          return results_with_ids
  
      except Exception as e:
          logger.error(f"Error processing batch {batch_num}: {str(e)}", exc_info=True)
          # 返回空结果,保持ID映射
          return [
36516857   tangwang   feat(product_enri...
1356
              _make_empty_analysis_result(item, target_lang, schema, error=str(e))
6f7840cf   tangwang   refactor: rename ...
1357
1358
1359
1360
1361
1362
1363
1364
1365
              for item in batch_data
          ]
  
  
  def analyze_products(
      products: List[Dict[str, str]],
      target_lang: str = "zh",
      batch_size: Optional[int] = None,
      tenant_id: Optional[str] = None,
36516857   tangwang   feat(product_enri...
1366
      analysis_kind: str = "content",
2703b6ea   tangwang   refactor(indexer)...
1367
      category_taxonomy_profile: Optional[str] = None,
6f7840cf   tangwang   refactor: rename ...
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
  ) -> List[Dict[str, Any]]:
      """
      库调用入口:根据输入+语言,返回锚文本及各维度信息。
  
      Args:
          products: [{"id": "...", "title": "..."}]
          target_lang: 输出语言
          batch_size: 批大小,默认使用全局 BATCH_SIZE
      """
      if not API_KEY:
          raise RuntimeError("DASHSCOPE_API_KEY is not set, cannot call LLM")
  
      if not products:
          return []
  
2703b6ea   tangwang   refactor(indexer)...
1383
1384
1385
1386
      _get_analysis_schema(
          analysis_kind,
          category_taxonomy_profile=category_taxonomy_profile,
      )
76e1f088   tangwang   1. 减少一列sell point...
1387
1388
1389
1390
1391
1392
1393
1394
1395
      results_by_index: List[Optional[Dict[str, Any]]] = [None] * len(products)
      uncached_items: List[Tuple[int, Dict[str, str]]] = []
  
      for idx, product in enumerate(products):
          title = str(product.get("title") or "").strip()
          if not title:
              uncached_items.append((idx, product))
              continue
  
2703b6ea   tangwang   refactor(indexer)...
1396
1397
1398
1399
1400
1401
          cached = _get_cached_analysis_result(
              product,
              target_lang,
              analysis_kind,
              category_taxonomy_profile=category_taxonomy_profile,
          )
76e1f088   tangwang   1. 减少一列sell point...
1402
1403
1404
          if cached:
              logger.info(
                  f"[analyze_products] Cache hit for title='{title[:50]}...', "
36516857   tangwang   feat(product_enri...
1405
                  f"kind={analysis_kind}, lang={target_lang}"
76e1f088   tangwang   1. 减少一列sell point...
1406
1407
1408
1409
1410
1411
1412
1413
              )
              results_by_index[idx] = cached
              continue
  
          uncached_items.append((idx, product))
  
      if not uncached_items:
          return [item for item in results_by_index if item is not None]
6f7840cf   tangwang   refactor: rename ...
1414
1415
1416
1417
1418
1419
  
      # call_llm 一次处理上限固定为 BATCH_SIZE(默认 20):
      # - 尽可能攒批处理;
      # - 即便调用方传入更大的 batch_size,也会自动按上限拆批。
      req_bs = BATCH_SIZE if batch_size is None else int(batch_size)
      bs = max(1, min(req_bs, BATCH_SIZE))
76e1f088   tangwang   1. 减少一列sell point...
1420
      total_batches = (len(uncached_items) + bs - 1) // bs
6f7840cf   tangwang   refactor: rename ...
1421
  
41f0b2e9   tangwang   product_enrich支持并发
1422
      batch_jobs: List[Tuple[int, List[Tuple[int, Dict[str, str]]], List[Dict[str, str]]]] = []
76e1f088   tangwang   1. 减少一列sell point...
1423
      for i in range(0, len(uncached_items), bs):
6f7840cf   tangwang   refactor: rename ...
1424
          batch_num = i // bs + 1
76e1f088   tangwang   1. 减少一列sell point...
1425
1426
          batch_slice = uncached_items[i : i + bs]
          batch = [item for _, item in batch_slice]
41f0b2e9   tangwang   product_enrich支持并发
1427
1428
1429
1430
1431
1432
1433
          batch_jobs.append((batch_num, batch_slice, batch))
  
      # 只有一个批次时走串行,减少线程池创建开销与日志/日志文件的不可控交织
      if total_batches <= 1 or CONTENT_UNDERSTANDING_MAX_WORKERS <= 1:
          for batch_num, batch_slice, batch in batch_jobs:
              logger.info(
                  f"[analyze_products] Processing batch {batch_num}/{total_batches}, "
36516857   tangwang   feat(product_enri...
1434
1435
1436
1437
1438
1439
1440
                  f"size={len(batch)}, kind={analysis_kind}, target_lang={target_lang}"
              )
              batch_results = process_batch(
                  batch,
                  batch_num=batch_num,
                  target_lang=target_lang,
                  analysis_kind=analysis_kind,
2703b6ea   tangwang   refactor(indexer)...
1441
                  category_taxonomy_profile=category_taxonomy_profile,
41f0b2e9   tangwang   product_enrich支持并发
1442
              )
41f0b2e9   tangwang   product_enrich支持并发
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
  
              for (original_idx, product), item in zip(batch_slice, batch_results):
                  results_by_index[original_idx] = item
                  title_input = str(item.get("title_input") or "").strip()
                  if not title_input:
                      continue
                  if item.get("error"):
                      # 不缓存错误结果,避免放大临时故障
                      continue
                  try:
2703b6ea   tangwang   refactor(indexer)...
1453
1454
1455
1456
1457
1458
1459
                      _set_cached_analysis_result(
                          product,
                          target_lang,
                          item,
                          analysis_kind,
                          category_taxonomy_profile=category_taxonomy_profile,
                      )
41f0b2e9   tangwang   product_enrich支持并发
1460
1461
1462
1463
1464
                  except Exception:
                      # 已在内部记录 warning
                      pass
      else:
          max_workers = min(CONTENT_UNDERSTANDING_MAX_WORKERS, len(batch_jobs))
6f7840cf   tangwang   refactor: rename ...
1465
          logger.info(
41f0b2e9   tangwang   product_enrich支持并发
1466
              "[analyze_products] Using ThreadPoolExecutor for uncached batches: "
36516857   tangwang   feat(product_enri...
1467
              "max_workers=%s, total_batches=%s, bs=%s, kind=%s, target_lang=%s",
41f0b2e9   tangwang   product_enrich支持并发
1468
1469
1470
              max_workers,
              total_batches,
              bs,
36516857   tangwang   feat(product_enri...
1471
              analysis_kind,
41f0b2e9   tangwang   product_enrich支持并发
1472
              target_lang,
6f7840cf   tangwang   refactor: rename ...
1473
          )
6f7840cf   tangwang   refactor: rename ...
1474
  
41f0b2e9   tangwang   product_enrich支持并发
1475
1476
1477
1478
1479
1480
          # 只把“LLM 调用 + markdown 解析”放到线程里;Redis get/set 保持在主线程,避免并发写入带来额外风险。
          # 注意:线程池是模块级单例,因此这里的 max_workers 主要用于日志语义(实际并发受单例池上限约束)。
          executor = _get_content_understanding_executor()
          future_by_batch_num: Dict[int, Any] = {}
          for batch_num, _batch_slice, batch in batch_jobs:
              future_by_batch_num[batch_num] = executor.submit(
36516857   tangwang   feat(product_enri...
1481
1482
1483
1484
1485
                  process_batch,
                  batch,
                  batch_num=batch_num,
                  target_lang=target_lang,
                  analysis_kind=analysis_kind,
2703b6ea   tangwang   refactor(indexer)...
1486
                  category_taxonomy_profile=category_taxonomy_profile,
41f0b2e9   tangwang   product_enrich支持并发
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
              )
  
          # 按 batch_num 回填,确保输出稳定(results_by_index 是按原始 input index 映射的)
          for batch_num, batch_slice, _batch in batch_jobs:
              batch_results = future_by_batch_num[batch_num].result()
              for (original_idx, product), item in zip(batch_slice, batch_results):
                  results_by_index[original_idx] = item
                  title_input = str(item.get("title_input") or "").strip()
                  if not title_input:
                      continue
                  if item.get("error"):
                      # 不缓存错误结果,避免放大临时故障
                      continue
                  try:
2703b6ea   tangwang   refactor(indexer)...
1501
1502
1503
1504
1505
1506
1507
                      _set_cached_analysis_result(
                          product,
                          target_lang,
                          item,
                          analysis_kind,
                          category_taxonomy_profile=category_taxonomy_profile,
                      )
41f0b2e9   tangwang   product_enrich支持并发
1508
1509
1510
                  except Exception:
                      # 已在内部记录 warning
                      pass
6f7840cf   tangwang   refactor: rename ...
1511
  
76e1f088   tangwang   1. 减少一列sell point...
1512
      return [item for item in results_by_index if item is not None]