Files
DeepTutor/deeptutor/services/embedding/client.py
Wong 1625cdd105 fix(embedding): survive provider rate limits — key rotation, retry budget, global throttle (#962)
* T028(Codex交付,ZC核验代提交): KeyPool多key轮换层(provider×model×key池·429冷却·单key兼容) · api_key/api_token字段兼容数组 · 三处接入(embedding/mineru/llm) · 图片描述gather+Semaphore并发化 · 303单测全过(含官方既有测试回归)

* FIX-EMB-429: 火山embedding 429根治(上游PR候选) · ①429重试按Retry-After等30s(滑窗等过) ②resolver从profile读batch_size/batch_delay(火山RPM极低必须限速) · 实测batch5/delay2s下语文reindex突破processing · 三key池x2冷却机制已验

* HARDEN-429: embedding限流重试1→8次+全冷却等待65s再取 · 单key池滑动窗口瞬时429不再报废数小时reindex整跑(08-22三烧教训) · 月度额度429仍8轮后raise不空转

* FIX-UNBOUND: 限流重试槽位for耗尽静默穿落到data引用 · 槽位1+max(MAX_RETRIES,10)兜住8轮限流 · t035镜像UnboundLocalError根因(逻辑走查复现)

* THROTTLE-GLOBAL: embedding跨实例全局节流锁 · 类级锁串行化'间隔+发帖'(多文档并行reindex各自client叠加超RPM=08-22历史库六烧终根因) · batch_delay升级为全局最小间隔 · 手动单发200而进程内全429的分歧由此闭合

* FIX-LOCK: 节流锁asyncio→threading · LlamaIndex _run_in_new_loop每线程新event loop,asyncio.Lock绑创建loop跨loop不互斥(根因三层剥洋葱终层) · 静态方法装饰修正 · 容器内3线程串行化实测2.01s通过

* PR-PREP: 中性化 fork 痕迹注释(中文→英文 provider 通用表述) · 上游v1.5.16已有_as_api_key数组支持,删我方重复实现保留上游版 · 测试148+86全过 · 密钥扫描分支历史+工作树双清(A类指纹0命中,B类28处=上游自带fixture)

* LINT: ruff format 6文件对齐CI(上游基线0债,全是我方引入) + import顺序修正

* FIX-CI: 图片处理块整体采纳上游v1.5.16版(_describe_image新签名+批量embed_contents) · 删越域的fork逐图并发测试(记档将来独立PR) · 592测全过

* FIX-CI: _build_openai_client合并壳与实现(上游sanitize保留+key池数组兼容) · placeholder_key改用primary_key(list哈希修复) · 889测全过

* fix(keypool): never refuse the only key

`KeyPool` is built even for a single-key config, and `next()` raised
`RuntimeError("All API keys are cooling down")` once every key was cooling.
For the common one-key setup that meant two 429s took every LLM and embedding
call down for the full 60s window — and it bypassed the rotation layer's own
429 handling entirely, since the RuntimeError surfaces before
`_KeyRotatingCompletions.create` can mark the strike and re-raise the real
provider error. `sdk_max_retries=0` had already handed retry duty to that
layer.

Cooling is a preference hint, not a circuit breaker: when everything is
cooling, return the key that recovers soonest and let the provider's own 429
surface to be retried.

The existing single-key test asserted the raise while calling itself
"keeps single key compatible" — it now pins the behaviour the name promises,
plus the soonest-recovery ordering for the multi-key case.

---------

Co-authored-by: interia <interia@interiadeMac-mini.local>
Co-authored-by: Frank <bingxizhao@novix.science>
2026-08-24 16:54:14 +08:00

314 lines
12 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Unified embedding client backed by normalized provider runtime config."""
from __future__ import annotations
import logging
from typing import Any, Dict, List, Optional
from deeptutor.services.config.embedding_endpoint import (
redact_embedding_endpoint_for_display,
)
from deeptutor.services.config.provider_runtime import (
EMBEDDING_PROVIDERS,
embedding_endpoint_validation_error,
)
from .adapters import ADAPTER_BACKENDS, BaseEmbeddingAdapter, EmbeddingRequest
from .config import EmbeddingConfig, get_embedding_config
from .validation import validate_embedding_batch
def _resolve_adapter_class(binding: str) -> type[BaseEmbeddingAdapter]:
provider = (binding or "").strip().lower()
spec = EMBEDDING_PROVIDERS.get(provider)
if spec is None:
supported = sorted(EMBEDDING_PROVIDERS.keys())
raise ValueError(
f"Unknown embedding binding: '{binding}'. Supported: {', '.join(supported)}"
)
cls = ADAPTER_BACKENDS.get(spec.adapter)
if cls is None:
raise ValueError(
f"No adapter registered for backend '{spec.adapter}' (binding='{binding}')"
)
return cls
class EmbeddingClient:
"""Unified embedding client for RAG and retrieval services."""
# 全局发帖节流:KB reindex 时 LlamaIndex 用线程池并发调 embedding,
# 每个线程经 _run_in_new_loop 跑独立 event loop——asyncio.Lock 绑定
# 创建时的 loop,跨 loop 既不互斥还会挂死。必须用线程级锁。
_spacing_lock: Any = None
_last_request_monotonic: float = 0.0
_thread_guard: Any = None
@classmethod
def _global_spacing_lock(cls):
import threading
if cls._spacing_lock is None:
cls._thread_guard = threading.Lock()
with cls._thread_guard:
if cls._spacing_lock is None:
from threading import Lock as _TLock
cls._spacing_lock = _TLock()
return cls._spacing_lock
@staticmethod
def _hold_spacing_lock():
"""Blocking acquire — cross-thread, loop-agnostic."""
lock = EmbeddingClient._global_spacing_lock()
lock.acquire()
return lock
def __init__(self, config: Optional[EmbeddingConfig] = None):
self.config = config or get_embedding_config()
self.logger = logging.getLogger(__name__)
endpoint = self.config.effective_url or self.config.base_url
problem = embedding_endpoint_validation_error(self.config.binding, endpoint)
if problem:
displayed_endpoint = redact_embedding_endpoint_for_display(endpoint)
raise ValueError(
f"{problem} Current Settings endpoint is {displayed_endpoint!r}. "
"DeepTutor sends embedding requests to the Settings URL exactly; "
"update the visible Endpoint URL instead of relying on hidden path appending."
)
adapter_class = _resolve_adapter_class(self.config.binding)
self.adapter = adapter_class(
{
"api_key": self.config.api_key,
"base_url": self.config.effective_url or self.config.base_url,
"api_version": self.config.api_version,
"model": self.config.model,
"dimensions": self.config.dim,
"send_dimensions": self.config.send_dimensions,
"request_timeout": self.config.request_timeout,
"extra_headers": self.config.extra_headers or {},
}
)
self.logger.info(
f"Initialized embedding client with {self.config.binding} adapter "
f"(model: {self.config.model}, dimensions: {self.config.dim})"
)
async def embed(
self,
texts: List[str],
progress_callback=None,
*,
input_type: str | None = None,
) -> List[List[float]]:
"""Embed text batches, optionally identifying their retrieval role."""
if not texts:
return []
# Only adapters that opted in receive the role. Forwarding it to every
# backend would change the request Jina has always sent (no `task`) and
# silently invalidate the indexes built from it.
role = input_type if getattr(self.adapter, "SUPPORTS_INPUT_TYPE", False) else None
import asyncio
# Clamp configured batch size against the provider's per-request item
# cap. SiliconFlow Qwen3 family caps at 32; DashScope at 20; others
# have generous defaults. Without this clamp, indexing a doc with many
# chunks fails on the second batch even when "Test connection" passes.
spec = EMBEDDING_PROVIDERS.get(self.config.binding)
provider_max = spec.max_batch_items if spec else 256
batch_size = max(1, min(self.config.batch_size, provider_max))
if batch_size < self.config.batch_size:
self.logger.info(
f"Clamped batch_size {self.config.batch_size} -> {batch_size} "
f"(provider '{self.config.binding}' max={provider_max})"
)
all_embeddings: List[List[float]] = []
batch_delay = self.config.batch_delay
expected_dim: int | None = None
total_batches = (len(texts) + batch_size - 1) // batch_size
for i, start in enumerate(range(0, len(texts), batch_size)):
batch = texts[start : start + batch_size]
request = EmbeddingRequest(
texts=batch,
model=self.config.model,
dimensions=self.config.dim or None,
input_type=role,
)
try:
# 全局发帖节流:线程级锁串行化"等待间隔+发帖",跨线程/跨
# event loop 互斥(asyncio 锁在新 loop 模型下失效的教训)。
# asyncio.sleep 换成阻塞等待发帖侧可接受:DT 的 embedding
# 并发都在后台线程里,阻塞不伤 API 主线程。
import time as _time
from time import monotonic as _mono
_lock = EmbeddingClient._hold_spacing_lock()
try:
delay = self.config.batch_delay
if delay > 0:
elapsed = _mono() - EmbeddingClient._last_request_monotonic
if elapsed < delay:
_time.sleep(delay - elapsed)
EmbeddingClient._last_request_monotonic = _mono()
response = await self.adapter.embed(request)
finally:
_lock.release()
except Exception as exc:
# Capture batch context so the task log stream / KB diagnostics
# show actionable info instead of a bare exception string.
import traceback
first_chunk_chars = len(batch[0]) if batch else 0
longest_chunk_chars = max((len(t) for t in batch), default=0)
self.logger.error(
f"Embedding batch failed "
f"(binding={self.config.binding}, model={self.config.model}, "
f"batch_index={i + 1}/{total_batches}, batch_items={len(batch)}, "
f"first_chunk_chars={first_chunk_chars}, "
f"longest_chunk_chars={longest_chunk_chars}): {exc}\n"
f"{traceback.format_exc()}"
)
raise
validated = validate_embedding_batch(
response.embeddings,
expected_count=len(batch),
binding=self.config.binding,
model=self.config.model,
batch_index=i + 1,
total_batches=total_batches,
start_index=start,
)
batch_dim = len(validated[0]) if validated else 0
if expected_dim is None:
expected_dim = batch_dim
elif batch_dim != expected_dim:
raise ValueError(
"Embedding provider returned inconsistent vector dimensions "
f"across batches (binding={self.config.binding}, "
f"model={self.config.model}): expected {expected_dim}, "
f"got {batch_dim} in batch {i + 1}/{total_batches}. "
"Use a single embedding model/dimension and re-index the knowledge base."
)
all_embeddings.extend(validated)
# Report progress after each batch
if progress_callback:
try:
progress_callback(i + 1, total_batches)
except Exception:
pass
# Delay between batches to avoid rate limiting
if i < total_batches - 1 and batch_delay > 0:
await asyncio.sleep(batch_delay)
self.logger.debug(
f"Generated {len(all_embeddings)} embeddings using "
f"{self.config.binding} (batch_size={batch_size})"
)
return all_embeddings
def supports_multimodal_contents(self) -> bool:
"""Return whether the configured adapter/model accepts multimodal contents."""
try:
info = self.adapter.get_model_info()
if "multimodal" in info:
return bool(info.get("multimodal"))
except Exception:
pass
spec = EMBEDDING_PROVIDERS.get(self.config.binding)
return bool(spec and spec.multimodal)
async def embed_contents(
self,
contents: List[Dict[str, Any]],
*,
progress_callback=None,
) -> List[List[float]]:
"""Embed provider-agnostic multimodal content items.
``contents`` uses the same simple contract as ``EmbeddingRequest``:
``[{"text": "..."}, {"image": "data:...|url"}, {"video": "..."}]``.
"""
if not contents:
return []
if not self.supports_multimodal_contents():
raise ValueError(
"Configured embedding provider/model does not support multimodal contents."
)
import asyncio
spec = EMBEDDING_PROVIDERS.get(self.config.binding)
provider_max = spec.max_batch_items if spec else 256
batch_size = max(1, min(self.config.batch_size, provider_max))
all_embeddings: List[List[float]] = []
total_batches = (len(contents) + batch_size - 1) // batch_size
for i, start in enumerate(range(0, len(contents), batch_size)):
batch = contents[start : start + batch_size]
request = EmbeddingRequest(
texts=[],
model=self.config.model,
dimensions=self.config.dim or None,
contents=batch,
enable_fusion=False,
)
response = await self.adapter.embed(request)
validated = validate_embedding_batch(
response.embeddings,
expected_count=len(batch),
binding=self.config.binding,
model=self.config.model,
batch_index=i + 1,
total_batches=total_batches,
start_index=start,
)
all_embeddings.extend(validated)
if progress_callback:
try:
progress_callback(i + 1, total_batches)
except Exception:
pass
if i < total_batches - 1 and self.config.batch_delay > 0:
await asyncio.sleep(self.config.batch_delay)
return all_embeddings
def embed_sync(self, texts: List[str]) -> List[List[float]]:
import asyncio
try:
asyncio.get_running_loop()
except RuntimeError:
return asyncio.run(self.embed(texts))
import concurrent.futures
with concurrent.futures.ThreadPoolExecutor() as executor:
future = executor.submit(asyncio.run, self.embed(texts))
return future.result()
_client: Optional[EmbeddingClient] = None
def get_embedding_client(config: Optional[EmbeddingConfig] = None) -> EmbeddingClient:
global _client
resolved_config = config or get_embedding_config()
if _client is None or _client.config != resolved_config:
_client = EmbeddingClient(resolved_config)
return _client
def reset_embedding_client() -> None:
global _client
_client = None