yimo-kit 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. yimo/__init__.py +26 -0
  2. yimo/kit/__init__.py +98 -0
  3. yimo/kit/ai/__init__.py +110 -0
  4. yimo/kit/ai/_internal/__init__.py +27 -0
  5. yimo/kit/ai/_internal/failover.py +535 -0
  6. yimo/kit/ai/_internal/pool.py +526 -0
  7. yimo/kit/ai/_internal/rerank_transport.py +137 -0
  8. yimo/kit/ai/_internal/retry_after.py +51 -0
  9. yimo/kit/ai/_internal/sdk_client.py +463 -0
  10. yimo/kit/ai/_internal/service_base.py +115 -0
  11. yimo/kit/ai/_internal/types.py +28 -0
  12. yimo/kit/ai/agent/__init__.py +127 -0
  13. yimo/kit/ai/agent/approval.py +66 -0
  14. yimo/kit/ai/agent/errors.py +128 -0
  15. yimo/kit/ai/agent/loop.py +643 -0
  16. yimo/kit/ai/agent/mcp.py +278 -0
  17. yimo/kit/ai/agent/registry.py +203 -0
  18. yimo/kit/ai/agent/session.py +150 -0
  19. yimo/kit/ai/agent/skill.py +370 -0
  20. yimo/kit/ai/agent/types.py +161 -0
  21. yimo/kit/ai/chat_service.py +724 -0
  22. yimo/kit/ai/decision/__init__.py +57 -0
  23. yimo/kit/ai/decision/decima.py +853 -0
  24. yimo/kit/ai/decision/laya.py +552 -0
  25. yimo/kit/ai/embedding_service.py +198 -0
  26. yimo/kit/ai/exceptions.py +109 -0
  27. yimo/kit/ai/image_service.py +84 -0
  28. yimo/kit/ai/messages.py +105 -0
  29. yimo/kit/ai/model_builder.py +121 -0
  30. yimo/kit/ai/model_client.py +62 -0
  31. yimo/kit/ai/profiles.py +577 -0
  32. yimo/kit/ai/rerank_service.py +136 -0
  33. yimo/kit/ai/runner.py +1127 -0
  34. yimo/kit/common_kit.py +221 -0
  35. yimo/kit/config/__init__.py +44 -0
  36. yimo/kit/config/kit_config.py +210 -0
  37. yimo/kit/config/log_config.py +171 -0
  38. yimo/kit/crawl/__init__.py +71 -0
  39. yimo/kit/crawl/article_io.py +325 -0
  40. yimo/kit/crawl/article_models.py +172 -0
  41. yimo/kit/crawl/deepseek_kit.py +673 -0
  42. yimo/kit/crawl/web_article_kit.py +638 -0
  43. yimo/kit/crawl/wx_article_kit.py +692 -0
  44. yimo/kit/db/__init__.py +101 -0
  45. yimo/kit/db/audit.py +625 -0
  46. yimo/kit/db/base_db_kit.py +1265 -0
  47. yimo/kit/db/crud.py +423 -0
  48. yimo/kit/db/engine.py +235 -0
  49. yimo/kit/db/exceptions.py +45 -0
  50. yimo/kit/db/models/__init__.py +33 -0
  51. yimo/kit/db/models/base.py +56 -0
  52. yimo/kit/db/mysql_kit.py +348 -0
  53. yimo/kit/db/pg_kit.py +442 -0
  54. yimo/kit/db/query.py +338 -0
  55. yimo/kit/db/session.py +85 -0
  56. yimo/kit/db/sqlite_kit.py +462 -0
  57. yimo/kit/db/transaction.py +109 -0
  58. yimo/kit/docs/__init__.py +34 -0
  59. yimo/kit/docs/anydoc_kit.py +490 -0
  60. yimo/kit/docs/docx_kit.py +895 -0
  61. yimo/kit/docs/md2doc_kit.py +1801 -0
  62. yimo/kit/docs/mineru_kit.py +1497 -0
  63. yimo/kit/docs/rc_docparser_kit.py +654 -0
  64. yimo/kit/file_kit.py +518 -0
  65. yimo/kit/license_kit.py +533 -0
  66. yimo/kit/redis_kit.py +392 -0
  67. yimo_kit-0.1.0.dist-info/METADATA +1673 -0
  68. yimo_kit-0.1.0.dist-info/RECORD +70 -0
  69. yimo_kit-0.1.0.dist-info/WHEEL +4 -0
  70. yimo_kit-0.1.0.dist-info/licenses/LICENSE +201 -0
@@ -0,0 +1,526 @@
1
+ """
2
+ Copyright (c) 2021-2026 溢墨修心(Yimo). All Rights Reserved.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+
16
+ Project: SynthArk
17
+ Author: 溢墨修心(Yimo)
18
+ """
19
+
20
+ # date: 2026-09-05
21
+ """AI 服务候选池:激活探测、故障冷却、当前项切换
22
+
23
+ 按模型类型维护「候选链」:链上档案按优先级排序(``prefer()`` 指定项打头),
24
+ 仅 ``AVAILABLE`` 状态档案参与调用竞选;档案调用失败进入冷却(默认 60 秒),
25
+ 冷却期内被跳过(备胎顶上),到期自动恢复——冷却即粘性降级,自愈无需干预。
26
+
27
+ 预热探测(``probe_all``)经 ``GET {base_url}/models`` 验证密钥有效性:
28
+ 2xx / 429 视为 key 有效并激活;401 / 403 判定 key 无效出链;其余
29
+ (超时 / 404 / 5xx)无法判定出链。剔除后链空时回退「密钥已配置档案全部
30
+ 惰性入链」并告警,保证功能不瘫。
31
+
32
+ 线程模型:业务调用与探测均在 runner 后台事件循环线程内执行;
33
+ ``available()`` / ``prefer()`` 可能从业务线程调用,内部以锁保护共享状态。
34
+ """
35
+
36
+ import asyncio
37
+ import threading
38
+ import time
39
+ from collections.abc import Callable, Iterable, Iterator
40
+ from dataclasses import dataclass, field
41
+ from typing import Final
42
+
43
+ from yimo.kit.ai.chat_service import ChatService
44
+ from yimo.kit.ai.embedding_service import EmbeddingService
45
+ from yimo.kit.ai.exceptions import (
46
+ AIConfigError,
47
+ AIError,
48
+ AIModelTypeNotConfiguredError,
49
+ AIRequestError,
50
+ )
51
+ from yimo.kit.ai.image_service import ImageService
52
+ from yimo.kit.ai.model_builder import Service
53
+ from yimo.kit.ai.model_client import ProbeClient
54
+ from yimo.kit.ai.profiles import (
55
+ ModelProfile,
56
+ ModelRegistry,
57
+ ProfileState,
58
+ ProfileStatus,
59
+ get_model_registry,
60
+ )
61
+ from yimo.kit.ai.rerank_service import RerankService
62
+ from yimo.kit.config.log_config import get_default_logger
63
+
64
+ __all__ = ["AIServicePool"]
65
+
66
+ logger = get_default_logger(__name__)
67
+
68
+ # 调用失败后的冷却时长(秒):冷却期内备胎顶上,到期自动回位
69
+ _COOLDOWN_SECONDS: Final = 60.0
70
+
71
+ # 429 Retry-After 冷却钳制上下限(秒):防服务端异常值导致冷却过短/过长
72
+ _RETRY_AFTER_MIN: Final = 5.0
73
+ _RETRY_AFTER_MAX: Final = 300.0
74
+
75
+ # 各模型类型 → 单档案服务工厂
76
+ _FACTORIES: Final[dict[str, Callable[[ModelProfile], Service]]] = {
77
+ "chat": ChatService,
78
+ "embedding": EmbeddingService,
79
+ "rerank": RerankService,
80
+ "image": ImageService,
81
+ }
82
+
83
+ # 探测判定为「密钥无效」的 HTTP 状态码(2xx 已由 list_models 放行;
84
+ # 429 限流恰证明服务与密钥均有效,在 _probe_state 中特判为可用)
85
+ _PROBE_INVALID_STATUS: Final[frozenset[int]] = frozenset({401, 403})
86
+
87
+
88
+ async def _probe_profile(profile: ModelProfile) -> None:
89
+ """单档案探测:key 有效(2xx / 429)静默返回,否则抛 ``AIError``。
90
+
91
+ 独立模块级函数便于测试替换。探测客户端用毕即关。
92
+ """
93
+ client = ProbeClient(profile)
94
+ try:
95
+ await client.list_models()
96
+ finally:
97
+ await client.close()
98
+
99
+
100
+ def _probe_state(exc: BaseException) -> ProfileState:
101
+ """探测异常 → 档案状态:429 视为 key 有效;401/403 无效;其余无法判定。"""
102
+ if isinstance(exc, AIRequestError) and exc.status_code == 429:
103
+ return ProfileState.AVAILABLE
104
+ if isinstance(exc, AIRequestError) and exc.status_code in _PROBE_INVALID_STATUS:
105
+ return ProfileState.INVALID_KEY
106
+ return ProfileState.UNREACHABLE
107
+
108
+
109
+ class _Entry:
110
+ """链内单档案的运行时状态(可变)。"""
111
+
112
+ __slots__ = (
113
+ "profile",
114
+ "state",
115
+ "cooling_until",
116
+ "last_error",
117
+ "last_ok_at",
118
+ "service",
119
+ "tool_unsupported",
120
+ )
121
+
122
+ def __init__(self, profile: ModelProfile) -> None:
123
+ self.profile: ModelProfile = profile
124
+ self.state: ProfileState = ProfileState.AVAILABLE
125
+ # 冷却截止时刻(time.monotonic 秒);0 表示未冷却
126
+ self.cooling_until: float = 0.0
127
+ self.last_error: str | None = None
128
+ self.last_ok_at: float | None = None
129
+ self.service: Service | None = None
130
+ # function calling 能力:声明 False 或运行时 400 判定后为 True
131
+ self.tool_unsupported: bool = profile.supports_tools is False
132
+
133
+
134
+ @dataclass
135
+ class _Chain:
136
+ """某类型的候选链:链序 + 全档案状态表(entries 含被剔除档案)。"""
137
+
138
+ order: list[str] = field(default_factory=list)
139
+ entries: dict[str, _Entry] = field(default_factory=dict)
140
+
141
+
142
+ class AIServicePool:
143
+ """按模型类型维护候选链:状态表 + 冷却 + 当前项 + service 惰性单例。"""
144
+
145
+ def __init__(self, registry: ModelRegistry | None = None) -> None:
146
+ self._registry: ModelRegistry = registry or get_model_registry()
147
+ self._lock = threading.Lock()
148
+ self._chains: dict[str, _Chain] = {}
149
+ # prefer() 指定的长期当前项(类型 → 档案名);进程重启即失效
150
+ self._preferred: dict[str, str] = {}
151
+
152
+ # ---------- 链解析 ----------
153
+
154
+ def _ensure_chain(self, model_type: str) -> _Chain:
155
+ """取(或首次解析)类型候选链。
156
+
157
+ ``registry.chain`` 涉及读配置与环境变量,置于锁外执行;
158
+ 锁内双检避免并发重复解析。
159
+ """
160
+ with self._lock:
161
+ chain = self._chains.get(model_type)
162
+ if chain is not None:
163
+ return chain
164
+ profiles = self._registry.chain(model_type)
165
+ with self._lock:
166
+ chain = self._chains.get(model_type)
167
+ if chain is not None:
168
+ return chain
169
+ entries: dict[str, _Entry] = {}
170
+ order: list[str] = []
171
+ for profile in profiles:
172
+ if profile.name not in entries:
173
+ entries[profile.name] = _Entry(profile)
174
+ order.append(profile.name)
175
+ # embedding 链内强制同 model:维度一致性是向量库可用性的前提
176
+ if model_type == "embedding" and order:
177
+ base_model = entries[order[0]].profile.model
178
+ excluded = [
179
+ name for name in order if entries[name].profile.model != base_model
180
+ ]
181
+ for name in excluded:
182
+ entries[name].state = ProfileState.EXCLUDED
183
+ entries[name].last_error = (
184
+ f"embedding 链内 model 不一致(要求 {base_model})"
185
+ )
186
+ order.remove(name)
187
+ if excluded:
188
+ logger.warning(
189
+ "embedding 候选链剔除 model 不一致档案: "
190
+ f"{', '.join(excluded)}(链内仅允许 {base_model})"
191
+ )
192
+ chain = _Chain(order=order, entries=entries)
193
+ self._chains[model_type] = chain
194
+ return chain
195
+
196
+ # ---------- 探测 ----------
197
+
198
+ async def probe_all(self, model_types: Iterable[str] | None = None) -> None:
199
+ """后台预热探测:逐类型并行验证候选档案密钥有效性(不抛异常)。
200
+
201
+ 单类型未配置(json 缺该类型)仅跳过,不影响其余类型探测。
202
+ ``model_types`` 给定时仅探测子集(配置重载按失效类型重投),
203
+ None 探测全部类型(既有行为不变)。
204
+ """
205
+ targets = (
206
+ list(_FACTORIES)
207
+ if model_types is None
208
+ else [t for t in _FACTORIES if t in set(model_types)]
209
+ )
210
+ for model_type in targets:
211
+ try:
212
+ await self._probe_type(model_type)
213
+ except AIConfigError as exc:
214
+ logger.debug(f"{model_type} 探测跳过(类型未配置档案): {exc}")
215
+ except Exception as exc: # noqa: BLE001 - 预热失败不影响主流程
216
+ logger.warning(f"{model_type} 档案探测异常: {exc}")
217
+
218
+ async def _probe_type(self, model_type: str) -> None:
219
+ """探测单类型链上档案,按判定表激活或剔除;链空时回退惰性入链。"""
220
+ chain = self._ensure_chain(model_type)
221
+ with self._lock:
222
+ names = list(chain.order)
223
+ if not names:
224
+ return
225
+ results = await asyncio.gather(
226
+ *(_probe_profile(chain.entries[name].profile) for name in names),
227
+ return_exceptions=True,
228
+ )
229
+ dropped: list[str] = []
230
+ with self._lock:
231
+ for name, result in zip(names, results, strict=True):
232
+ entry = chain.entries[name]
233
+ if isinstance(result, BaseException):
234
+ state = _probe_state(result)
235
+ entry.state = state
236
+ entry.last_error = f"探测: {result}"
237
+ if state is ProfileState.AVAILABLE:
238
+ # 429:限流但 key 有效,保持可用
239
+ entry.last_ok_at = time.time()
240
+ else:
241
+ dropped.append(name)
242
+ logger.warning(
243
+ f"{model_type}.{name} 探测未通过,剔除出链: {result}"
244
+ )
245
+ else:
246
+ entry.state = ProfileState.AVAILABLE
247
+ entry.last_ok_at = time.time()
248
+ entry.last_error = None
249
+ if dropped:
250
+ for name in dropped:
251
+ if name in chain.order:
252
+ chain.order.remove(name)
253
+ # 兜底:剔除后链空 → 回退「密钥已配置档案全部惰性入链」
254
+ if not chain.order:
255
+ chain.order = list(names)
256
+ for name in names:
257
+ chain.entries[name].state = ProfileState.AVAILABLE
258
+ logger.warning(
259
+ f"{model_type} 探测后候选链为空,回退惰性入链: "
260
+ f"{', '.join(names)}(真实调用时再验证)"
261
+ )
262
+ else:
263
+ logger.info(
264
+ f"{model_type} 探测完成: 可用 "
265
+ f"{', '.join(chain.order)};剔除 {', '.join(dropped)}"
266
+ )
267
+
268
+ # ---------- 候选迭代 ----------
269
+
270
+ def iter_services(
271
+ self, model_type: str, *, require_tools: bool = False
272
+ ) -> Iterator[tuple[str, Service]]:
273
+ """按竞选顺序迭代(档案名, service):prefer 项打头,跳过冷却档案。
274
+
275
+ Args:
276
+ require_tools: True 时额外跳过声明/运行时判定不支持 function calling
277
+ 的档案(Agent 工具循环专用);普通对话不受影响。
278
+
279
+ 候选计划(顺序 + prefer 提前 + 冷却到期复位 + 可用性过滤)在生成器
280
+ 首次推进时一次定格:此后 ``prefer()`` 切换与其它请求的失败冷却均
281
+ 不影响已开始迭代的调用方——切换只为「下一次」请求生效。
282
+
283
+ 仅在后台事件循环线程内迭代;service 惰性构建并缓存复用。
284
+ """
285
+ chain = self._ensure_chain(model_type)
286
+ now = time.monotonic()
287
+ with self._lock:
288
+ seq = list(chain.order)
289
+ preferred = self._preferred.get(model_type)
290
+ if preferred is not None and preferred in seq:
291
+ seq.remove(preferred)
292
+ seq.insert(0, preferred)
293
+ plan: list[tuple[str, Service | None]] = []
294
+ for name in seq:
295
+ entry = chain.entries[name]
296
+ if require_tools and entry.tool_unsupported:
297
+ continue
298
+ if entry.state is ProfileState.COOLING and now >= entry.cooling_until:
299
+ entry.state = ProfileState.AVAILABLE
300
+ entry.last_error = None
301
+ if entry.state is not ProfileState.AVAILABLE:
302
+ continue
303
+ plan.append((name, entry.service))
304
+ for name, service in plan:
305
+ if service is None:
306
+ entry = chain.entries[name]
307
+ service = _FACTORIES[model_type](entry.profile)
308
+ with self._lock:
309
+ if entry.service is None:
310
+ entry.service = service
311
+ else:
312
+ service = entry.service
313
+ yield name, service
314
+
315
+ # ---------- 状态标记 ----------
316
+
317
+ def mark_failure(
318
+ self,
319
+ model_type: str,
320
+ name: str,
321
+ exc: AIError,
322
+ *,
323
+ retry_after: float | None = None,
324
+ ) -> None:
325
+ """档案调用失败:进入冷却并记录原因。
326
+
327
+ Args:
328
+ retry_after: 服务端 ``Retry-After`` 建议等待秒数(429 时由
329
+ model_client 解析);给出时冷却钳制到
330
+ ``[_RETRY_AFTER_MIN, _RETRY_AFTER_MAX]``,缺省回落 60s。
331
+ """
332
+ cooldown = _COOLDOWN_SECONDS
333
+ if retry_after is not None:
334
+ cooldown = min(max(retry_after, _RETRY_AFTER_MIN), _RETRY_AFTER_MAX)
335
+ with self._lock:
336
+ chain = self._chains.get(model_type)
337
+ entry = chain.entries.get(name) if chain else None
338
+ if entry is None:
339
+ return
340
+ entry.state = ProfileState.COOLING
341
+ entry.cooling_until = time.monotonic() + cooldown
342
+ entry.last_error = f"{type(exc).__name__}: {exc}"
343
+ logger.warning(f"{model_type}.{name} 调用失败,冷却 {cooldown:g}s: {exc}")
344
+
345
+ def mark_success(self, model_type: str, name: str) -> None:
346
+ """档案调用成功:记录成功时刻并清除失败痕迹。"""
347
+ with self._lock:
348
+ chain = self._chains.get(model_type)
349
+ entry = chain.entries.get(name) if chain else None
350
+ if entry is None:
351
+ return
352
+ entry.last_ok_at = time.time()
353
+ entry.last_error = None
354
+
355
+ def mark_tool_unsupported(
356
+ self, model_type: str, name: str, exc: BaseException
357
+ ) -> None:
358
+ """运行时判定档案不支持 function calling:后续工具调用跳过该档案。
359
+
360
+ 只影响 ``require_tools=True`` 的候选迭代;普通对话仍可使用该档案。
361
+ """
362
+ with self._lock:
363
+ chain = self._chains.get(model_type)
364
+ entry = chain.entries.get(name) if chain else None
365
+ if entry is None:
366
+ return
367
+ entry.tool_unsupported = True
368
+ logger.warning(
369
+ f"{model_type}.{name} 判定不支持 function calling,"
370
+ f"后续工具调用将跳过: {exc}"
371
+ )
372
+
373
+ # ---------- 当前项与状态 ----------
374
+
375
+ def prefer(self, model_type: str, profile: str) -> None:
376
+ """切换该类型长期当前项:指定档案成为固定链头并清除冷却。
377
+
378
+ Raises:
379
+ AIConfigError: 档案不存在,或不可用
380
+ (未配置 / 密钥无效 / 不可达 / 因约束被排除)。
381
+ """
382
+ chain = self._ensure_chain(model_type)
383
+ with self._lock:
384
+ entry = chain.entries.get(profile)
385
+ if entry is None:
386
+ known = ", ".join(chain.entries) or "无"
387
+ raise AIConfigError(
388
+ f"档案不存在: {model_type}.{profile}(已知档案: {known})"
389
+ )
390
+ if entry.state in (
391
+ ProfileState.UNCONFIGURED,
392
+ ProfileState.INVALID_KEY,
393
+ ProfileState.UNREACHABLE,
394
+ ProfileState.EXCLUDED,
395
+ ):
396
+ raise AIConfigError(
397
+ f"档案不可用: {model_type}.{profile}"
398
+ f"(状态 {entry.state.value},原因: {entry.last_error})"
399
+ )
400
+ entry.state = ProfileState.AVAILABLE
401
+ entry.cooling_until = 0.0
402
+ self._preferred[model_type] = profile
403
+ logger.info(f"已切换 {model_type} 当前使用档案: {profile}")
404
+
405
+ def available(self, model_type: str) -> list[ProfileStatus]:
406
+ """该类型全部档案状态快照(含未配置密钥与被剔除者),链头标记 current。
407
+
408
+ 类型名合法但 ``ai_models.json`` 未配置该段时返回空列表(可预期空态);
409
+ 类型名非法、注册表未加载等真正的配置错误仍抛 ``AIConfigError``。
410
+ """
411
+ try:
412
+ chain = self._ensure_chain(model_type)
413
+ except AIModelTypeNotConfiguredError as exc:
414
+ logger.debug(f"{model_type} 状态快照为空: {exc}")
415
+ return []
416
+ now = time.monotonic()
417
+ with self._lock:
418
+ preferred = self._preferred.get(model_type)
419
+ if preferred in chain.order:
420
+ current: str | None = preferred
421
+ else:
422
+ current = chain.order[0] if chain.order else None
423
+ rows = [
424
+ ProfileStatus(
425
+ name=name,
426
+ model=entry.profile.model,
427
+ state=(
428
+ ProfileState.AVAILABLE
429
+ if entry.state is ProfileState.COOLING
430
+ and now >= entry.cooling_until
431
+ else entry.state
432
+ ),
433
+ current=name == current,
434
+ last_error=entry.last_error,
435
+ last_ok_at=entry.last_ok_at,
436
+ thinking_switchable=entry.profile.thinking is not None,
437
+ json_mode_supported=entry.profile.structured_output is not None,
438
+ tools_supported=(
439
+ False
440
+ if entry.tool_unsupported
441
+ else entry.profile.supports_tools
442
+ ),
443
+ )
444
+ for name, entry in chain.entries.items()
445
+ ]
446
+ try:
447
+ all_profiles = self._registry.type_profiles(model_type)
448
+ except AIConfigError:
449
+ all_profiles = {}
450
+ for name, config in all_profiles.items():
451
+ if name not in chain.entries:
452
+ rows.append(
453
+ ProfileStatus(
454
+ name=name,
455
+ model=config.model,
456
+ state=ProfileState.UNCONFIGURED,
457
+ current=False,
458
+ thinking_switchable=config.thinking is not None,
459
+ json_mode_supported=(
460
+ model_type == "chat"
461
+ and config.structured_output is not False
462
+ ),
463
+ tools_supported=(
464
+ config.supports_tools if model_type == "chat" else None
465
+ ),
466
+ )
467
+ )
468
+ return rows
469
+
470
+ # ---------- 生命周期 ----------
471
+
472
+ async def close_all(self) -> None:
473
+ """关闭全部已构建 service(释放底层 httpx 连接)。幂等。"""
474
+ with self._lock:
475
+ services = [
476
+ entry.service
477
+ for chain in self._chains.values()
478
+ for entry in chain.entries.values()
479
+ if entry.service is not None
480
+ ]
481
+ for service in services:
482
+ try:
483
+ await service.close()
484
+ except Exception as exc: # noqa: BLE001 - 关闭阶段失败不影响退出
485
+ logger.warning(f"关闭 {service} 失败: {exc}")
486
+
487
+ def invalidate(
488
+ self, model_types: Iterable[str] | None = None
489
+ ) -> list[Service]:
490
+ """失效候选链:摘除指定类型(None=全部)的链并返回已构建 service。
491
+
492
+ 供配置热重载使用:摘除后下次 ``_ensure_chain`` 按当前 registry
493
+ 重新解析;``_preferred`` 中档案仍存在的偏好保留、已删除的剔除
494
+ (避免改密钥等不动档案集合的操作让「当前模型」无端漂移)。
495
+
496
+ **不关闭**返回的 service——关闭时机(立即 / 延迟回收)由调用方
497
+ 决定。重载前在途调用持有的旧链引用不受影响,其后续
498
+ ``mark_failure`` / ``mark_success`` 因链已摘除被静默忽略
499
+ (与「切换只为下一次请求生效」语义一致)。
500
+
501
+ Returns:
502
+ 被摘除链上已构建的 service 列表(未构建的不在其中)。
503
+ """
504
+ with self._lock:
505
+ targets = list(self._chains) if model_types is None else list(model_types)
506
+ old: list[Service] = []
507
+ for model_type in targets:
508
+ chain = self._chains.pop(model_type, None)
509
+ if chain is not None:
510
+ old.extend(
511
+ entry.service
512
+ for entry in chain.entries.values()
513
+ if entry.service is not None
514
+ )
515
+ for model_type in targets:
516
+ preferred = self._preferred.get(model_type)
517
+ if preferred is None:
518
+ continue
519
+ try:
520
+ known = self._registry.type_profiles(model_type)
521
+ except AIConfigError:
522
+ del self._preferred[model_type]
523
+ continue
524
+ if preferred not in known:
525
+ del self._preferred[model_type]
526
+ return old
@@ -0,0 +1,137 @@
1
+ """
2
+ Copyright (c) 2021-2026 溢墨修心(Yimo). All Rights Reserved.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+
16
+ Project: yimo
17
+ Author: 溢墨修心(Yimo)
18
+ """
19
+
20
+ # date: 2026-11-04
21
+ """重排序 HTTP 传输(httpx)。
22
+
23
+ `openai` SDK 不提供 ``/rerank`` 资源,故 rerank 保留自研传输:单次 JSON
24
+ POST、5xx 指数退避重试、429 ``Retry-After`` 解析。语义与 0.6.9 的
25
+ 通用传输实现完全一致。
26
+ """
27
+
28
+ import asyncio
29
+ import json
30
+ import random
31
+ import time
32
+
33
+ try:
34
+ import httpx
35
+ except ImportError as e:
36
+ raise ImportError(
37
+ "yimo.kit.ai 需要可选依赖,请安装:pip install yimo-kit[ai]"
38
+ ) from e
39
+
40
+ from yimo.kit.ai._internal.retry_after import MIN_RETRY_BUDGET, parse_retry_after
41
+ from yimo.kit.ai._internal.types import JSONObject
42
+ from yimo.kit.ai.exceptions import AIRequestError, AIResponseError, AITimeoutError
43
+ from yimo.kit.ai.profiles import ModelProfile
44
+
45
+ __all__ = ["RerankTransport"]
46
+
47
+
48
+ def _retryable(exc: BaseException) -> bool:
49
+ """判断异常是否可重试:仅服务端 5xx。"""
50
+ return (
51
+ isinstance(exc, AIRequestError)
52
+ and exc.status_code is not None
53
+ and exc.status_code >= 500
54
+ )
55
+
56
+
57
+ class RerankTransport:
58
+ """重排序专用 HTTP 客户端(全异步,单客户端)。"""
59
+
60
+ def __init__(self, profile: ModelProfile) -> None:
61
+ self._profile: ModelProfile = profile
62
+ self._client: httpx.AsyncClient = httpx.AsyncClient(
63
+ base_url=profile.base_url,
64
+ timeout=profile.timeout,
65
+ )
66
+
67
+ @staticmethod
68
+ def _headers(profile: ModelProfile) -> dict[str, str]:
69
+ return {
70
+ "Content-Type": "application/json",
71
+ "Authorization": f"Bearer {profile.api_key}",
72
+ }
73
+
74
+ @staticmethod
75
+ async def _parse_json(data: str) -> JSONObject:
76
+ try:
77
+ parsed = json.loads(data)
78
+ except (json.JSONDecodeError, TypeError) as exc:
79
+ raise AIResponseError(f"响应不是合法 JSON: {exc}") from exc
80
+ if not isinstance(parsed, dict):
81
+ raise AIResponseError(f"响应顶层必须是 JSON 对象: {data[:200]}")
82
+ return parsed
83
+
84
+ async def _post_once(self, payload: JSONObject) -> JSONObject:
85
+ try:
86
+ resp = await self._client.post(
87
+ self._profile.endpoint,
88
+ headers=self._headers(self._profile),
89
+ json=payload,
90
+ )
91
+ resp.raise_for_status()
92
+ return await self._parse_json(resp.text)
93
+ except httpx.TimeoutException as exc:
94
+ raise AITimeoutError(f"请求超时: {exc}") from exc
95
+ except httpx.HTTPStatusError as exc:
96
+ status_code = exc.response.status_code
97
+ retry_after = (
98
+ parse_retry_after(exc.response.headers.get("Retry-After"))
99
+ if status_code == 429
100
+ else None
101
+ )
102
+ raise AIRequestError(
103
+ f"HTTP 错误 {status_code}: {exc.response.text[:200]}",
104
+ status_code=status_code,
105
+ retry_after=retry_after,
106
+ ) from exc
107
+ except httpx.HTTPError as exc:
108
+ raise AIRequestError(f"请求失败: {exc}") from exc
109
+
110
+ async def post_json(
111
+ self,
112
+ payload: JSONObject,
113
+ *,
114
+ deadline: float | None = None,
115
+ ) -> JSONObject:
116
+ """发送请求并按档案 ``retries`` 对 5xx 重试。"""
117
+ max_attempts = self._profile.retries + 1
118
+ for attempt in range(max_attempts):
119
+ try:
120
+ return await self._post_once(payload)
121
+ except (AITimeoutError, AIRequestError) as exc:
122
+ if attempt >= max_attempts - 1 or not _retryable(exc):
123
+ raise
124
+ if (
125
+ deadline is not None
126
+ and time.monotonic() + MIN_RETRY_BUDGET >= deadline
127
+ ):
128
+ raise AIRequestError(
129
+ f"剩余整链预算不足,放弃重试: {exc}",
130
+ status_code=exc.status_code,
131
+ retry_after=exc.retry_after,
132
+ ) from exc
133
+ await asyncio.sleep(min(2**attempt, 8) + random.uniform(0, 0.5))
134
+ raise AIRequestError("重试后仍未成功")
135
+
136
+ async def close(self) -> None:
137
+ await self._client.aclose()