Blame view

config/schema.py 9.65 KB
86d8358b   tangwang   config optimize
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  """
  Typed configuration schema for the unified application configuration.
  
  This module defines the normalized in-memory structure used by all services.
  """
  
  from __future__ import annotations
  
  from dataclasses import asdict, dataclass, field
  from pathlib import Path
  from typing import Any, Dict, List, Optional, Tuple
  
  
  @dataclass(frozen=True)
  class IndexConfig:
      """Deprecated compatibility shape for legacy diagnostics/tests."""
  
      name: str
      label: str
      fields: List[str]
      boost: float = 1.0
      example: Optional[str] = None
  
  
  @dataclass(frozen=True)
  class QueryConfig:
      """Configuration for query processing."""
  
      supported_languages: List[str] = field(default_factory=lambda: ["zh", "en"])
      default_language: str = "en"
      enable_text_embedding: bool = True
      enable_query_rewrite: bool = True
      rewrite_dictionary: Dict[str, str] = field(default_factory=dict)
      text_embedding_field: Optional[str] = "title_embedding"
      image_embedding_field: Optional[str] = None
      source_fields: Optional[List[str]] = None
      knn_boost: float = 0.25
      multilingual_fields: List[str] = field(
          default_factory=lambda: [
              "title",
              "brief",
              "description",
              "vendor",
              "category_path",
              "category_name_text",
          ]
      )
      shared_fields: List[str] = field(
          default_factory=lambda: ["tags", "option1_values", "option2_values", "option3_values"]
      )
      core_multilingual_fields: List[str] = field(
          default_factory=lambda: ["title", "brief", "vendor", "category_name_text"]
      )
272aeabe   tangwang   调参
54
55
      base_minimum_should_match: str = "70%"
      translation_minimum_should_match: str = "70%"
86d8358b   tangwang   config optimize
56
      translation_boost: float = 0.4
86d8358b   tangwang   config optimize
57
      tie_breaker_base_query: float = 0.9
e756b18e   tangwang   重构了文本召回构建器,现在每个 b...
58
59
60
61
      best_fields: Dict[str, float] = field(default_factory=dict)
      best_fields_boost: float = 2.0
      phrase_fields: Dict[str, float] = field(default_factory=dict)
      phrase_match_boost: float = 3.0
86d8358b   tangwang   config optimize
62
63
64
      zh_to_en_model: str = "opus-mt-zh-en"
      en_to_zh_model: str = "opus-mt-en-zh"
      default_translation_model: str = "nllb-200-distilled-600m"
86d0e83d   tangwang   query翻译,根据源语言是否在索...
65
66
67
68
      # 检测语种不在租户 index_languages(无可直接命中的多语字段)时使用;None 表示与上一组同模型。
      zh_to_en_model_source_not_in_index: Optional[str] = None
      en_to_zh_model_source_not_in_index: Optional[str] = None
      default_translation_model_source_not_in_index: Optional[str] = None
1556989b   tangwang   query翻译等待超时逻辑
69
70
71
72
73
      # 查询阶段:翻译与向量生成并发提交后,共用同一等待预算(毫秒)。
      # 检测语言已在租户 index_languages 内:偏快返回,预算较短。
      # 检测语言不在 index_languages 内:翻译对召回更关键,预算较长。
      translation_embedding_wait_budget_ms_source_in_index: int = 80
      translation_embedding_wait_budget_ms_source_not_in_index: int = 200
86d8358b   tangwang   config optimize
74
75
76
77
78
79
80
81
82
83
84
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
  
  
  @dataclass(frozen=True)
  class SPUConfig:
      """SPU aggregation/search configuration."""
  
      enabled: bool = False
      spu_field: Optional[str] = None
      inner_hits_size: int = 3
      searchable_option_dimensions: List[str] = field(
          default_factory=lambda: ["option1", "option2", "option3"]
      )
  
  
  @dataclass(frozen=True)
  class FunctionScoreConfig:
      """Function score configuration."""
  
      score_mode: str = "sum"
      boost_mode: str = "multiply"
      functions: List[Dict[str, Any]] = field(default_factory=list)
  
  
  @dataclass(frozen=True)
  class RerankConfig:
      """Search-time rerank configuration."""
  
      enabled: bool = True
      rerank_window: int = 384
      timeout_sec: float = 15.0
      weight_es: float = 0.4
      weight_ai: float = 0.6
      rerank_query_template: str = "{query}"
      rerank_doc_template: str = "{title}"
  
  
  @dataclass(frozen=True)
  class SearchConfig:
      """Search behavior configuration shared by backend and indexer."""
  
      field_boosts: Dict[str, float]
      indexes: List[IndexConfig] = field(default_factory=list)
      query_config: QueryConfig = field(default_factory=QueryConfig)
      function_score: FunctionScoreConfig = field(default_factory=FunctionScoreConfig)
      rerank: RerankConfig = field(default_factory=RerankConfig)
      spu_config: SPUConfig = field(default_factory=SPUConfig)
      es_index_name: str = "search_products"
      es_settings: Dict[str, Any] = field(default_factory=dict)
  
  
  @dataclass(frozen=True)
  class TranslationServiceConfig:
      """Translator service configuration."""
  
      endpoint: str
      timeout_sec: float
      default_model: str
      default_scene: str
      cache: Dict[str, Any]
      capabilities: Dict[str, Dict[str, Any]]
  
      def as_dict(self) -> Dict[str, Any]:
          return {
              "service_url": self.endpoint,
              "timeout_sec": self.timeout_sec,
              "default_model": self.default_model,
              "default_scene": self.default_scene,
              "cache": self.cache,
              "capabilities": self.capabilities,
          }
  
  
  @dataclass(frozen=True)
  class EmbeddingServiceConfig:
      """Embedding service configuration."""
  
      provider: str
      providers: Dict[str, Any]
      backend: str
      backends: Dict[str, Dict[str, Any]]
      image_backend: str
      image_backends: Dict[str, Dict[str, Any]]
  
      def get_provider_config(self) -> Dict[str, Any]:
          return dict(self.providers.get(self.provider, {}) or {})
  
      def get_backend_config(self) -> Dict[str, Any]:
          return dict(self.backends.get(self.backend, {}) or {})
  
      def get_image_backend_config(self) -> Dict[str, Any]:
          return dict(self.image_backends.get(self.image_backend, {}) or {})
  
  
  @dataclass(frozen=True)
  class RerankServiceConfig:
      """Reranker service configuration."""
  
      provider: str
      providers: Dict[str, Any]
      backend: str
      backends: Dict[str, Dict[str, Any]]
      request: Dict[str, Any]
  
      def get_provider_config(self) -> Dict[str, Any]:
          return dict(self.providers.get(self.provider, {}) or {})
  
      def get_backend_config(self) -> Dict[str, Any]:
          return dict(self.backends.get(self.backend, {}) or {})
  
  
  @dataclass(frozen=True)
  class ServicesConfig:
      """All service-level configuration."""
  
      translation: TranslationServiceConfig
      embedding: EmbeddingServiceConfig
      rerank: RerankServiceConfig
  
  
  @dataclass(frozen=True)
  class TenantCatalogConfig:
      """Tenant catalog configuration."""
  
      default: Dict[str, Any]
      tenants: Dict[str, Dict[str, Any]]
  
      def get_raw(self) -> Dict[str, Any]:
          return {
              "default": dict(self.default),
              "tenants": {str(key): dict(value) for key, value in self.tenants.items()},
          }
  
  
  @dataclass(frozen=True)
  class ElasticsearchSettings:
      host: str = "http://localhost:9200"
      username: Optional[str] = None
      password: Optional[str] = None
  
  
  @dataclass(frozen=True)
  class RedisSettings:
      host: str = "localhost"
      port: int = 6479
      snapshot_db: int = 0
      password: Optional[str] = None
      socket_timeout: int = 1
      socket_connect_timeout: int = 1
      retry_on_timeout: bool = False
      cache_expire_days: int = 720
      embedding_cache_prefix: str = "embedding"
      anchor_cache_prefix: str = "product_anchors"
      anchor_cache_expire_days: int = 30
  
  
  @dataclass(frozen=True)
  class DatabaseSettings:
      host: Optional[str] = None
      port: int = 3306
      database: Optional[str] = None
      username: Optional[str] = None
      password: Optional[str] = None
  
  
  @dataclass(frozen=True)
  class SecretsConfig:
      dashscope_api_key: Optional[str] = None
      deepl_auth_key: Optional[str] = None
  
  
  @dataclass(frozen=True)
  class InfrastructureConfig:
      elasticsearch: ElasticsearchSettings
      redis: RedisSettings
      database: DatabaseSettings
      secrets: SecretsConfig
  
  
  @dataclass(frozen=True)
41f0b2e9   tangwang   product_enrich支持并发
253
254
255
256
257
258
259
  class ProductEnrichConfig:
      """Configuration for LLM-based product content understanding (enrich-content)."""
  
      max_workers: int = 40
  
  
  @dataclass(frozen=True)
86d8358b   tangwang   config optimize
260
261
262
263
264
265
266
  class RuntimeConfig:
      environment: str = "prod"
      index_namespace: str = ""
      api_host: str = "0.0.0.0"
      api_port: int = 6002
      indexer_host: str = "0.0.0.0"
      indexer_port: int = 6004
fe80e80e   tangwang   fix host config
267
      embedding_host: str = "0.0.0.0"
86d8358b   tangwang   config optimize
268
269
270
      embedding_port: int = 6005
      embedding_text_port: int = 6005
      embedding_image_port: int = 6008
fe80e80e   tangwang   fix host config
271
      translator_host: str = "0.0.0.0"
86d8358b   tangwang   config optimize
272
      translator_port: int = 6006
fe80e80e   tangwang   fix host config
273
      reranker_host: str = "0.0.0.0"
86d8358b   tangwang   config optimize
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
      reranker_port: int = 6007
  
  
  @dataclass(frozen=True)
  class AssetsConfig:
      query_rewrite_dictionary_path: Path
  
  
  @dataclass(frozen=True)
  class ConfigMetadata:
      loaded_files: Tuple[str, ...]
      config_hash: str
      deprecated_keys: Tuple[str, ...] = field(default_factory=tuple)
  
  
  @dataclass(frozen=True)
  class AppConfig:
      """Root application configuration."""
  
      runtime: RuntimeConfig
      infrastructure: InfrastructureConfig
41f0b2e9   tangwang   product_enrich支持并发
295
      product_enrich: ProductEnrichConfig
86d8358b   tangwang   config optimize
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
325
      search: SearchConfig
      services: ServicesConfig
      tenants: TenantCatalogConfig
      assets: AssetsConfig
      metadata: ConfigMetadata
  
      def sanitized_dict(self) -> Dict[str, Any]:
          data = asdict(self)
          data["infrastructure"]["elasticsearch"]["password"] = _mask_secret(
              data["infrastructure"]["elasticsearch"].get("password")
          )
          data["infrastructure"]["database"]["password"] = _mask_secret(
              data["infrastructure"]["database"].get("password")
          )
          data["infrastructure"]["redis"]["password"] = _mask_secret(
              data["infrastructure"]["redis"].get("password")
          )
          data["infrastructure"]["secrets"]["dashscope_api_key"] = _mask_secret(
              data["infrastructure"]["secrets"].get("dashscope_api_key")
          )
          data["infrastructure"]["secrets"]["deepl_auth_key"] = _mask_secret(
              data["infrastructure"]["secrets"].get("deepl_auth_key")
          )
          return data
  
  
  def _mask_secret(value: Optional[str]) -> Optional[str]:
      if not value:
          return value
      return "***"