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.
- yimo/__init__.py +26 -0
- yimo/kit/__init__.py +98 -0
- yimo/kit/ai/__init__.py +110 -0
- yimo/kit/ai/_internal/__init__.py +27 -0
- yimo/kit/ai/_internal/failover.py +535 -0
- yimo/kit/ai/_internal/pool.py +526 -0
- yimo/kit/ai/_internal/rerank_transport.py +137 -0
- yimo/kit/ai/_internal/retry_after.py +51 -0
- yimo/kit/ai/_internal/sdk_client.py +463 -0
- yimo/kit/ai/_internal/service_base.py +115 -0
- yimo/kit/ai/_internal/types.py +28 -0
- yimo/kit/ai/agent/__init__.py +127 -0
- yimo/kit/ai/agent/approval.py +66 -0
- yimo/kit/ai/agent/errors.py +128 -0
- yimo/kit/ai/agent/loop.py +643 -0
- yimo/kit/ai/agent/mcp.py +278 -0
- yimo/kit/ai/agent/registry.py +203 -0
- yimo/kit/ai/agent/session.py +150 -0
- yimo/kit/ai/agent/skill.py +370 -0
- yimo/kit/ai/agent/types.py +161 -0
- yimo/kit/ai/chat_service.py +724 -0
- yimo/kit/ai/decision/__init__.py +57 -0
- yimo/kit/ai/decision/decima.py +853 -0
- yimo/kit/ai/decision/laya.py +552 -0
- yimo/kit/ai/embedding_service.py +198 -0
- yimo/kit/ai/exceptions.py +109 -0
- yimo/kit/ai/image_service.py +84 -0
- yimo/kit/ai/messages.py +105 -0
- yimo/kit/ai/model_builder.py +121 -0
- yimo/kit/ai/model_client.py +62 -0
- yimo/kit/ai/profiles.py +577 -0
- yimo/kit/ai/rerank_service.py +136 -0
- yimo/kit/ai/runner.py +1127 -0
- yimo/kit/common_kit.py +221 -0
- yimo/kit/config/__init__.py +44 -0
- yimo/kit/config/kit_config.py +210 -0
- yimo/kit/config/log_config.py +171 -0
- yimo/kit/crawl/__init__.py +71 -0
- yimo/kit/crawl/article_io.py +325 -0
- yimo/kit/crawl/article_models.py +172 -0
- yimo/kit/crawl/deepseek_kit.py +673 -0
- yimo/kit/crawl/web_article_kit.py +638 -0
- yimo/kit/crawl/wx_article_kit.py +692 -0
- yimo/kit/db/__init__.py +101 -0
- yimo/kit/db/audit.py +625 -0
- yimo/kit/db/base_db_kit.py +1265 -0
- yimo/kit/db/crud.py +423 -0
- yimo/kit/db/engine.py +235 -0
- yimo/kit/db/exceptions.py +45 -0
- yimo/kit/db/models/__init__.py +33 -0
- yimo/kit/db/models/base.py +56 -0
- yimo/kit/db/mysql_kit.py +348 -0
- yimo/kit/db/pg_kit.py +442 -0
- yimo/kit/db/query.py +338 -0
- yimo/kit/db/session.py +85 -0
- yimo/kit/db/sqlite_kit.py +462 -0
- yimo/kit/db/transaction.py +109 -0
- yimo/kit/docs/__init__.py +34 -0
- yimo/kit/docs/anydoc_kit.py +490 -0
- yimo/kit/docs/docx_kit.py +895 -0
- yimo/kit/docs/md2doc_kit.py +1801 -0
- yimo/kit/docs/mineru_kit.py +1497 -0
- yimo/kit/docs/rc_docparser_kit.py +654 -0
- yimo/kit/file_kit.py +518 -0
- yimo/kit/license_kit.py +533 -0
- yimo/kit/redis_kit.py +392 -0
- yimo_kit-0.1.0.dist-info/METADATA +1673 -0
- yimo_kit-0.1.0.dist-info/RECORD +70 -0
- yimo_kit-0.1.0.dist-info/WHEEL +4 -0
- yimo_kit-0.1.0.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,535 @@
|
|
|
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
|
+
"""候选链故障切换调用层
|
|
22
|
+
|
|
23
|
+
将单档案 Service 的公开方法包装为链式调用:按池内候选顺序尝试,任一档案
|
|
24
|
+
抛出 ``AIError`` 即冷却该档案并尝试下一个;全部失败(或整链预算耗尽,
|
|
25
|
+
不再发起新候选)抛出 ``AIAggregateError``——携带已尝试档案列表、各候选
|
|
26
|
+
状态码与最后失败原因,全候选状态码一致时透传(如整链 429)。
|
|
27
|
+
|
|
28
|
+
流式对话仅在「首帧到达前」允许切换档案;首帧已产出后失败直接抛出
|
|
29
|
+
(已输出 token 无法回滚)。首帧等待可施加 ``first_frame_timeout``,
|
|
30
|
+
超时按失败处理切下一候选。空流(首帧即结束)视为正常完成。
|
|
31
|
+
|
|
32
|
+
各 Failover 服务公开方法与对应单档案 Service 保持同形,业务侧无感替换。
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
import asyncio
|
|
36
|
+
import time
|
|
37
|
+
from collections.abc import (
|
|
38
|
+
AsyncGenerator,
|
|
39
|
+
AsyncIterator,
|
|
40
|
+
Awaitable,
|
|
41
|
+
Callable,
|
|
42
|
+
Mapping,
|
|
43
|
+
Sequence,
|
|
44
|
+
)
|
|
45
|
+
from typing import Any, Final, TypeVar, cast
|
|
46
|
+
|
|
47
|
+
from yimo.kit.ai._internal.pool import AIServicePool
|
|
48
|
+
from yimo.kit.ai.chat_service import ChatService, StreamStats, ToolSpecLike
|
|
49
|
+
from yimo.kit.ai.embedding_service import EmbeddingService
|
|
50
|
+
from yimo.kit.ai.exceptions import AIAggregateError, AIError, AIRequestError, AITimeoutError
|
|
51
|
+
from yimo.kit.ai.image_service import ImageResult, ImageService
|
|
52
|
+
from yimo.kit.ai.messages import ChatReply
|
|
53
|
+
from yimo.kit.ai.model_builder import Service
|
|
54
|
+
from yimo.kit.ai.rerank_service import RerankHit, RerankService
|
|
55
|
+
|
|
56
|
+
__all__ = [
|
|
57
|
+
"FailoverChatService",
|
|
58
|
+
"FailoverEmbeddingService",
|
|
59
|
+
"FailoverImageService",
|
|
60
|
+
"FailoverRerankService",
|
|
61
|
+
]
|
|
62
|
+
|
|
63
|
+
T = TypeVar("T")
|
|
64
|
+
|
|
65
|
+
# 批量向量化默认并发(与 EmbeddingService 保持一致)
|
|
66
|
+
_EMBED_CONCURRENCY = 4
|
|
67
|
+
|
|
68
|
+
# 流式首帧等待的轮询粒度(秒):只用于「有无任何帧」的续期判定,
|
|
69
|
+
# 不改变超时时长语义(推理帧被吞掉,不轮询就看不到思考阶段的活动)
|
|
70
|
+
_FIRST_FRAME_POLL: Final[float] = 1.0
|
|
71
|
+
|
|
72
|
+
# 运行时判定「档案不支持 function calling」的关键词(仅 400 时匹配):
|
|
73
|
+
# 必须同时命中「工具词」+「不支持/未识别词」,避免把参数错误误判为能力缺失
|
|
74
|
+
_TOOL_ERROR_TERMS: Final[tuple[str, ...]] = ("tool", "function")
|
|
75
|
+
_UNSUPPORTED_TERMS: Final[tuple[str, ...]] = (
|
|
76
|
+
"not supported",
|
|
77
|
+
"unsupported",
|
|
78
|
+
"not support",
|
|
79
|
+
"does not support",
|
|
80
|
+
"not enabled",
|
|
81
|
+
"unrecognized",
|
|
82
|
+
"unknown parameter",
|
|
83
|
+
"unknown field",
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _looks_like_tools_unsupported(exc: AIError) -> bool:
|
|
88
|
+
"""判断 400 错误是否由 tools / function calling 不被支持引起。
|
|
89
|
+
|
|
90
|
+
仅匹配「工具词 + 不支持词」组合,未知错误一律不标记,避免把参数错误
|
|
91
|
+
误判为能力缺失。
|
|
92
|
+
"""
|
|
93
|
+
if not isinstance(exc, AIRequestError) or exc.status_code != 400:
|
|
94
|
+
return False
|
|
95
|
+
message = str(exc).lower()
|
|
96
|
+
has_tool = any(term in message for term in _TOOL_ERROR_TERMS)
|
|
97
|
+
has_unsupported = any(term in message for term in _UNSUPPORTED_TERMS)
|
|
98
|
+
return has_tool and has_unsupported
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class _FailoverBase:
|
|
102
|
+
"""链式调用基类:候选迭代 + 失败冷却 + 整链预算 + 聚合异常。"""
|
|
103
|
+
|
|
104
|
+
def __init__(self, pool: AIServicePool, model_type: str) -> None:
|
|
105
|
+
self._pool: AIServicePool = pool
|
|
106
|
+
self._model_type: str = model_type
|
|
107
|
+
# 最近一次成功的档案名(工具循环据此锁定档案,避免中途换模型)
|
|
108
|
+
self._last_profile: str | None = None
|
|
109
|
+
|
|
110
|
+
@property
|
|
111
|
+
def last_profile(self) -> str | None:
|
|
112
|
+
"""最近一次成功调用使用的档案名;从未成功时为 None。"""
|
|
113
|
+
return self._last_profile
|
|
114
|
+
|
|
115
|
+
def _aggregate_error(
|
|
116
|
+
self,
|
|
117
|
+
label: str,
|
|
118
|
+
tried: list[str],
|
|
119
|
+
errors: list[AIError],
|
|
120
|
+
*,
|
|
121
|
+
budget_exhausted: bool,
|
|
122
|
+
) -> AIAggregateError:
|
|
123
|
+
"""把候选链各候选的失败聚合为 ``AIAggregateError``。
|
|
124
|
+
|
|
125
|
+
全候选状态码一致(非 None)时透传该值(业务可据此判定限流);
|
|
126
|
+
``retry_after`` 取最后一个错误携带的建议等待秒数。
|
|
127
|
+
"""
|
|
128
|
+
statuses: list[int | None] = [
|
|
129
|
+
exc.status_code if isinstance(exc, AIRequestError) else None
|
|
130
|
+
for exc in errors
|
|
131
|
+
]
|
|
132
|
+
consistent = (
|
|
133
|
+
statuses[0]
|
|
134
|
+
if statuses and all(s == statuses[0] for s in statuses)
|
|
135
|
+
else None
|
|
136
|
+
)
|
|
137
|
+
last = errors[-1] if errors else None
|
|
138
|
+
suffix = ";整链预算已耗尽,未再发起新候选" if budget_exhausted else ""
|
|
139
|
+
message = (
|
|
140
|
+
f"{label}失败:已尝试 {len(tried)} 个档案"
|
|
141
|
+
f"({', '.join(tried) or '无可用候选'}){suffix}"
|
|
142
|
+
+ (f",最后错误: {last}" if last is not None else "")
|
|
143
|
+
)
|
|
144
|
+
return AIAggregateError(
|
|
145
|
+
message,
|
|
146
|
+
status_code=consistent,
|
|
147
|
+
tried=tried,
|
|
148
|
+
statuses=statuses,
|
|
149
|
+
last_error=last,
|
|
150
|
+
retry_after=last.retry_after if isinstance(last, AIRequestError) else None,
|
|
151
|
+
budget_exhausted=budget_exhausted,
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
def _budget_exhausted(self, deadline: float | None) -> bool:
|
|
155
|
+
"""整链预算是否已耗尽(deadline 为 None 表示不限制)。"""
|
|
156
|
+
return deadline is not None and time.monotonic() >= deadline
|
|
157
|
+
|
|
158
|
+
async def _attempt(
|
|
159
|
+
self,
|
|
160
|
+
label: str,
|
|
161
|
+
run: Callable[[Service], Awaitable[T]],
|
|
162
|
+
*,
|
|
163
|
+
deadline: float | None = None,
|
|
164
|
+
require_tools: bool = False,
|
|
165
|
+
) -> T:
|
|
166
|
+
"""按候选顺序执行 ``run``;单档案失败冷却后换下一个,全失败聚合抛出。
|
|
167
|
+
|
|
168
|
+
Args:
|
|
169
|
+
deadline: 整链预算截止时刻(``time.monotonic()`` 秒);每个候选
|
|
170
|
+
发起前检查,预算耗尽则不再发起新候选,改抛携带「预算耗尽」
|
|
171
|
+
语义的聚合异常(避免门面从外部 cancel 使备胎白等)
|
|
172
|
+
require_tools: True 时跳过声明/运行时判定不支持 function calling
|
|
173
|
+
的档案(工具循环专用)
|
|
174
|
+
"""
|
|
175
|
+
tried: list[str] = []
|
|
176
|
+
errors: list[AIError] = []
|
|
177
|
+
budget_exhausted = False
|
|
178
|
+
for name, service in self._pool.iter_services(
|
|
179
|
+
self._model_type, require_tools=require_tools
|
|
180
|
+
):
|
|
181
|
+
if self._budget_exhausted(deadline):
|
|
182
|
+
budget_exhausted = True
|
|
183
|
+
break
|
|
184
|
+
tried.append(name)
|
|
185
|
+
try:
|
|
186
|
+
result = await run(service)
|
|
187
|
+
except AIError as exc:
|
|
188
|
+
errors.append(exc)
|
|
189
|
+
self._pool.mark_failure(
|
|
190
|
+
self._model_type,
|
|
191
|
+
name,
|
|
192
|
+
exc,
|
|
193
|
+
retry_after=getattr(exc, "retry_after", None),
|
|
194
|
+
)
|
|
195
|
+
continue
|
|
196
|
+
self._pool.mark_success(self._model_type, name)
|
|
197
|
+
self._last_profile = name
|
|
198
|
+
return result
|
|
199
|
+
raise self._aggregate_error(
|
|
200
|
+
label, tried, errors, budget_exhausted=budget_exhausted
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
class FailoverChatService(_FailoverBase):
|
|
205
|
+
"""对话服务链式调用(ask / complete / stream)。"""
|
|
206
|
+
|
|
207
|
+
def __init__(self, pool: AIServicePool) -> None:
|
|
208
|
+
super().__init__(pool, "chat")
|
|
209
|
+
|
|
210
|
+
async def create(
|
|
211
|
+
self,
|
|
212
|
+
messages: list[dict[str, str]],
|
|
213
|
+
*,
|
|
214
|
+
temperature: float | None = None,
|
|
215
|
+
max_tokens: int | None = None,
|
|
216
|
+
thinking: bool | None = None,
|
|
217
|
+
json_mode: bool | None = None,
|
|
218
|
+
response_format: dict[str, Any] | None = None,
|
|
219
|
+
stats: StreamStats | None = None,
|
|
220
|
+
deadline: float | None = None,
|
|
221
|
+
) -> str:
|
|
222
|
+
"""多轮对话,返回模型回答文本(链式故障切换)。
|
|
223
|
+
|
|
224
|
+
``thinking`` / ``json_mode`` / ``response_format`` 原样下传:注入发生在
|
|
225
|
+
``ChatService`` 内部,按**当前候选自己**的档案声明组装;该候选不支持时
|
|
226
|
+
忽略(不因此切候选)。``stats`` 在每个候选发起前清零,故只反映最终
|
|
227
|
+
成功的候选。
|
|
228
|
+
"""
|
|
229
|
+
|
|
230
|
+
async def _run(service: Service) -> str:
|
|
231
|
+
if stats is not None:
|
|
232
|
+
stats.reset()
|
|
233
|
+
return await cast(ChatService, service).create(
|
|
234
|
+
messages,
|
|
235
|
+
temperature=temperature,
|
|
236
|
+
max_tokens=max_tokens,
|
|
237
|
+
thinking=thinking,
|
|
238
|
+
json_mode=json_mode,
|
|
239
|
+
response_format=response_format,
|
|
240
|
+
stats=stats,
|
|
241
|
+
deadline=deadline,
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
return await self._attempt("对话", _run, deadline=deadline)
|
|
245
|
+
|
|
246
|
+
async def create_with_tools(
|
|
247
|
+
self,
|
|
248
|
+
messages: list[dict[str, Any]],
|
|
249
|
+
tools: Sequence[ToolSpecLike | Mapping[str, Any]],
|
|
250
|
+
*,
|
|
251
|
+
tool_choice: str | dict[str, Any] | None = None,
|
|
252
|
+
temperature: float | None = None,
|
|
253
|
+
max_tokens: int | None = None,
|
|
254
|
+
thinking: bool | None = None,
|
|
255
|
+
stats: StreamStats | None = None,
|
|
256
|
+
deadline: float | None = None,
|
|
257
|
+
) -> ChatReply:
|
|
258
|
+
"""带工具定义的对话(链式故障切换)。
|
|
259
|
+
|
|
260
|
+
仅第 1 轮使用本方法:成功后调用方可读 :attr:`last_profile` 锁定档案,
|
|
261
|
+
后续轮次改用单档案 ``ChatService``,不再切换候选。
|
|
262
|
+
"""
|
|
263
|
+
|
|
264
|
+
async def _run(service: Service) -> ChatReply:
|
|
265
|
+
if stats is not None:
|
|
266
|
+
stats.reset()
|
|
267
|
+
try:
|
|
268
|
+
return await cast(ChatService, service).create_with_tools(
|
|
269
|
+
messages,
|
|
270
|
+
tools,
|
|
271
|
+
tool_choice=tool_choice,
|
|
272
|
+
temperature=temperature,
|
|
273
|
+
max_tokens=max_tokens,
|
|
274
|
+
thinking=thinking,
|
|
275
|
+
stats=stats,
|
|
276
|
+
deadline=deadline,
|
|
277
|
+
)
|
|
278
|
+
except AIRequestError as exc:
|
|
279
|
+
if _looks_like_tools_unsupported(exc):
|
|
280
|
+
profile = getattr(service, "profile", None)
|
|
281
|
+
name = getattr(profile, "name", None)
|
|
282
|
+
if isinstance(name, str):
|
|
283
|
+
self._pool.mark_tool_unsupported(
|
|
284
|
+
self._model_type, name, exc
|
|
285
|
+
)
|
|
286
|
+
raise
|
|
287
|
+
|
|
288
|
+
return await self._attempt(
|
|
289
|
+
"工具对话", _run, deadline=deadline, require_tools=True
|
|
290
|
+
)
|
|
291
|
+
|
|
292
|
+
async def create_once(
|
|
293
|
+
self,
|
|
294
|
+
prompt: str,
|
|
295
|
+
*,
|
|
296
|
+
temperature: float | None = None,
|
|
297
|
+
max_tokens: int | None = None,
|
|
298
|
+
thinking: bool | None = None,
|
|
299
|
+
json_mode: bool | None = None,
|
|
300
|
+
response_format: dict[str, Any] | None = None,
|
|
301
|
+
stats: StreamStats | None = None,
|
|
302
|
+
deadline: float | None = None,
|
|
303
|
+
) -> str:
|
|
304
|
+
"""单轮对话,返回模型回答文本(链式故障切换)。"""
|
|
305
|
+
|
|
306
|
+
async def _run(service: Service) -> str:
|
|
307
|
+
if stats is not None:
|
|
308
|
+
stats.reset()
|
|
309
|
+
return await cast(ChatService, service).create_once(
|
|
310
|
+
prompt,
|
|
311
|
+
temperature=temperature,
|
|
312
|
+
max_tokens=max_tokens,
|
|
313
|
+
thinking=thinking,
|
|
314
|
+
json_mode=json_mode,
|
|
315
|
+
response_format=response_format,
|
|
316
|
+
stats=stats,
|
|
317
|
+
deadline=deadline,
|
|
318
|
+
)
|
|
319
|
+
|
|
320
|
+
return await self._attempt("单轮对话", _run, deadline=deadline)
|
|
321
|
+
|
|
322
|
+
async def create_stream(
|
|
323
|
+
self,
|
|
324
|
+
messages: list[dict[str, str]],
|
|
325
|
+
*,
|
|
326
|
+
temperature: float | None = None,
|
|
327
|
+
max_tokens: int | None = None,
|
|
328
|
+
thinking: bool | None = None,
|
|
329
|
+
json_mode: bool | None = None,
|
|
330
|
+
response_format: dict[str, Any] | None = None,
|
|
331
|
+
first_frame_timeout: float | None = None,
|
|
332
|
+
deadline: float | None = None,
|
|
333
|
+
stats: StreamStats | None = None,
|
|
334
|
+
) -> AsyncIterator[str]:
|
|
335
|
+
"""流式对话:首帧前失败自动换档案重试;首帧后失败直接抛出。
|
|
336
|
+
|
|
337
|
+
Args:
|
|
338
|
+
thinking: 思考开关;按当前候选自己的档案声明组装,无声明则忽略
|
|
339
|
+
json_mode: 结构化输出要求;按当前候选自己的声明组装
|
|
340
|
+
response_format: 协议级原样透传(与 ``json_mode`` 互斥)
|
|
341
|
+
first_frame_timeout: 单候选首帧等待上限(秒);窗口内**一帧都没有**
|
|
342
|
+
(含推理帧)才按失败处理切下一候选;None 不限制
|
|
343
|
+
deadline: 整链预算截止时刻;每个候选发起前检查
|
|
344
|
+
stats: 收尾统计累加器;候选切换前清零,故只反映最终成功的候选
|
|
345
|
+
"""
|
|
346
|
+
tried: list[str] = []
|
|
347
|
+
errors: list[AIError] = []
|
|
348
|
+
budget_exhausted = False
|
|
349
|
+
stats = stats if stats is not None else StreamStats()
|
|
350
|
+
for name, service in self._pool.iter_services(self._model_type):
|
|
351
|
+
if self._budget_exhausted(deadline):
|
|
352
|
+
budget_exhausted = True
|
|
353
|
+
break
|
|
354
|
+
tried.append(name)
|
|
355
|
+
stats.reset()
|
|
356
|
+
agen = cast(
|
|
357
|
+
AsyncGenerator[str, None],
|
|
358
|
+
cast(ChatService, service).create_stream(
|
|
359
|
+
messages,
|
|
360
|
+
temperature=temperature,
|
|
361
|
+
max_tokens=max_tokens,
|
|
362
|
+
thinking=thinking,
|
|
363
|
+
json_mode=json_mode,
|
|
364
|
+
response_format=response_format,
|
|
365
|
+
stats=stats,
|
|
366
|
+
),
|
|
367
|
+
)
|
|
368
|
+
try:
|
|
369
|
+
try:
|
|
370
|
+
first = await self._await_first_frame(
|
|
371
|
+
agen, stats, first_frame_timeout
|
|
372
|
+
)
|
|
373
|
+
except StopAsyncIteration:
|
|
374
|
+
# 空流(首帧即结束)视为正常完成
|
|
375
|
+
await agen.aclose()
|
|
376
|
+
return
|
|
377
|
+
except AIError as exc:
|
|
378
|
+
errors.append(exc)
|
|
379
|
+
self._pool.mark_failure(
|
|
380
|
+
self._model_type,
|
|
381
|
+
name,
|
|
382
|
+
exc,
|
|
383
|
+
retry_after=getattr(exc, "retry_after", None),
|
|
384
|
+
)
|
|
385
|
+
await agen.aclose()
|
|
386
|
+
continue
|
|
387
|
+
try:
|
|
388
|
+
self._pool.mark_success(self._model_type, name)
|
|
389
|
+
yield first
|
|
390
|
+
async for delta in agen:
|
|
391
|
+
yield delta
|
|
392
|
+
finally:
|
|
393
|
+
await agen.aclose()
|
|
394
|
+
return
|
|
395
|
+
except BaseException:
|
|
396
|
+
# 消费侧中断(提前 break / 取消)时确保底层流关闭
|
|
397
|
+
await agen.aclose()
|
|
398
|
+
raise
|
|
399
|
+
raise self._aggregate_error(
|
|
400
|
+
"流式对话", tried, errors, budget_exhausted=budget_exhausted
|
|
401
|
+
)
|
|
402
|
+
|
|
403
|
+
async def _await_first_frame(
|
|
404
|
+
self,
|
|
405
|
+
agen: AsyncGenerator[str, None],
|
|
406
|
+
stats: StreamStats,
|
|
407
|
+
timeout: float | None,
|
|
408
|
+
) -> str:
|
|
409
|
+
"""等首个正文帧;期间任何帧(含推理帧)都算活动并重置窗口。
|
|
410
|
+
|
|
411
|
+
``ChatService.stream`` 只产出正文、推理帧仅计数,故「首个 yield 到达」
|
|
412
|
+
会把整个思考阶段表现为静默——按窗口计时会误判健康的思考型模型失败
|
|
413
|
+
(并重放整段 prompt)。改为按 ``stats.reasoning_chars`` 的推进续期。
|
|
414
|
+
|
|
415
|
+
Raises:
|
|
416
|
+
AITimeoutError: 窗口内一帧都没有
|
|
417
|
+
StopAsyncIteration: 空流(首帧即结束)
|
|
418
|
+
"""
|
|
419
|
+
if timeout is None:
|
|
420
|
+
return await agen.__anext__()
|
|
421
|
+
task = asyncio.ensure_future(agen.__anext__())
|
|
422
|
+
waited = 0.0
|
|
423
|
+
seen = stats.reasoning_chars
|
|
424
|
+
try:
|
|
425
|
+
while True:
|
|
426
|
+
remaining = timeout - waited
|
|
427
|
+
if remaining <= 0:
|
|
428
|
+
raise AITimeoutError(f"流式首帧超过 {timeout:g}s 未到达")
|
|
429
|
+
step = min(_FIRST_FRAME_POLL, remaining)
|
|
430
|
+
done, _ = await asyncio.wait({task}, timeout=step)
|
|
431
|
+
if task in done:
|
|
432
|
+
return task.result()
|
|
433
|
+
if stats.reasoning_chars != seen:
|
|
434
|
+
# 推理帧在推进:思考阶段属于正常活动,窗口续期
|
|
435
|
+
seen = stats.reasoning_chars
|
|
436
|
+
waited = 0.0
|
|
437
|
+
continue
|
|
438
|
+
waited += step
|
|
439
|
+
except BaseException:
|
|
440
|
+
if not task.done():
|
|
441
|
+
task.cancel()
|
|
442
|
+
# 等 __anext__ 真正退栈,否则调用方随后 aclose() 会撞
|
|
443
|
+
# 「asynchronous generator is already running」
|
|
444
|
+
try:
|
|
445
|
+
await task
|
|
446
|
+
except BaseException: # noqa: BLE001 - 取消后的收尾无需上抛
|
|
447
|
+
pass
|
|
448
|
+
raise
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
class FailoverEmbeddingService(_FailoverBase):
|
|
452
|
+
"""向量化服务链式调用(embed_text / embed_texts)。"""
|
|
453
|
+
|
|
454
|
+
def __init__(self, pool: AIServicePool) -> None:
|
|
455
|
+
super().__init__(pool, "embedding")
|
|
456
|
+
|
|
457
|
+
async def create(self, text: str) -> list[float]:
|
|
458
|
+
"""单文本向量化(链式故障切换)。"""
|
|
459
|
+
vectors = await self.create_batch([text])
|
|
460
|
+
return vectors[0]
|
|
461
|
+
|
|
462
|
+
async def create_batch(
|
|
463
|
+
self,
|
|
464
|
+
texts: list[str],
|
|
465
|
+
*,
|
|
466
|
+
batch_size: int | None = None,
|
|
467
|
+
concurrency: int = _EMBED_CONCURRENCY,
|
|
468
|
+
deadline: float | None = None,
|
|
469
|
+
) -> list[list[float]]:
|
|
470
|
+
"""批量向量化(链式故障切换;整批失败才切换档案)。"""
|
|
471
|
+
if not texts:
|
|
472
|
+
return []
|
|
473
|
+
|
|
474
|
+
async def _run(service: Service) -> list[list[float]]:
|
|
475
|
+
return await cast(EmbeddingService, service).create_batch(
|
|
476
|
+
texts, batch_size=batch_size, concurrency=concurrency
|
|
477
|
+
)
|
|
478
|
+
|
|
479
|
+
return await self._attempt("向量化", _run, deadline=deadline)
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
class FailoverRerankService(_FailoverBase):
|
|
483
|
+
"""重排序服务链式调用(rerank)。"""
|
|
484
|
+
|
|
485
|
+
def __init__(self, pool: AIServicePool) -> None:
|
|
486
|
+
super().__init__(pool, "rerank")
|
|
487
|
+
|
|
488
|
+
async def create(
|
|
489
|
+
self,
|
|
490
|
+
query: str,
|
|
491
|
+
documents: list[str],
|
|
492
|
+
*,
|
|
493
|
+
top_n: int | None = None,
|
|
494
|
+
deadline: float | None = None,
|
|
495
|
+
) -> list[RerankHit]:
|
|
496
|
+
"""候选文档重排序(链式故障切换)。"""
|
|
497
|
+
if not documents:
|
|
498
|
+
return []
|
|
499
|
+
|
|
500
|
+
async def _run(service: Service) -> list[RerankHit]:
|
|
501
|
+
return await cast(RerankService, service).create(
|
|
502
|
+
query, documents, top_n=top_n
|
|
503
|
+
)
|
|
504
|
+
|
|
505
|
+
return await self._attempt("重排序", _run, deadline=deadline)
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
class FailoverImageService(_FailoverBase):
|
|
509
|
+
"""图像生成服务链式调用(generate)。"""
|
|
510
|
+
|
|
511
|
+
def __init__(self, pool: AIServicePool) -> None:
|
|
512
|
+
super().__init__(pool, "image")
|
|
513
|
+
|
|
514
|
+
async def create(
|
|
515
|
+
self,
|
|
516
|
+
prompt: str,
|
|
517
|
+
*,
|
|
518
|
+
size: str | None = None,
|
|
519
|
+
n: int = 1,
|
|
520
|
+
return_base64: bool = False,
|
|
521
|
+
ratio: str | None = None,
|
|
522
|
+
deadline: float | None = None,
|
|
523
|
+
) -> ImageResult:
|
|
524
|
+
"""文生图,返回 :class:`ImageResult`(链式故障切换)。"""
|
|
525
|
+
|
|
526
|
+
async def _run(service: Service) -> ImageResult:
|
|
527
|
+
return await cast(ImageService, service).create(
|
|
528
|
+
prompt,
|
|
529
|
+
size=size,
|
|
530
|
+
n=n,
|
|
531
|
+
return_base64=return_base64,
|
|
532
|
+
ratio=ratio,
|
|
533
|
+
)
|
|
534
|
+
|
|
535
|
+
return await self._attempt("图像生成", _run, deadline=deadline)
|