mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-03 23:10:30 +08:00
* 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>
439 lines
14 KiB
Python
439 lines
14 KiB
Python
"""Tests for normalized embedding runtime resolution."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from deeptutor.services.config.provider_runtime import (
|
|
EMBEDDING_PROVIDERS,
|
|
resolve_embedding_runtime_config,
|
|
)
|
|
|
|
NATIVE_GEMINI2_ENDPOINT = (
|
|
"https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-2:batchEmbedContents"
|
|
)
|
|
|
|
|
|
def _build_catalog(
|
|
*,
|
|
embedding_profile: dict | None = None,
|
|
embedding_model: dict | None = None,
|
|
) -> dict:
|
|
embedding_profile = embedding_profile or {
|
|
"id": "embedding-p",
|
|
"name": "Embedding",
|
|
"binding": "openai",
|
|
"base_url": "",
|
|
"api_key": "",
|
|
"api_version": "",
|
|
"extra_headers": {},
|
|
"models": [{"id": "embedding-m", "name": "m", "model": "text-embedding-3-large"}],
|
|
}
|
|
if embedding_model is not None:
|
|
# Replace whichever model lives at the active slot so the override is
|
|
# actually visible to ``resolve_embedding_runtime_config``.
|
|
embedding_profile["models"] = [embedding_model]
|
|
embedding_model = embedding_profile["models"][0]
|
|
return {
|
|
"version": 1,
|
|
"services": {
|
|
"llm": {"active_profile_id": None, "active_model_id": None, "profiles": []},
|
|
"embedding": {
|
|
"active_profile_id": embedding_profile["id"],
|
|
"active_model_id": embedding_model["id"],
|
|
"profiles": [embedding_profile],
|
|
},
|
|
"search": {"active_profile_id": None, "profiles": []},
|
|
},
|
|
}
|
|
|
|
|
|
def test_embedding_explicit_binding_and_headers() -> None:
|
|
catalog = _build_catalog(
|
|
embedding_profile={
|
|
"id": "embedding-p",
|
|
"name": "Embedding",
|
|
"binding": "jina",
|
|
"base_url": "",
|
|
"api_key": "jina-key",
|
|
"api_version": "",
|
|
"extra_headers": {"X-App": "demo"},
|
|
"models": [
|
|
{
|
|
"id": "embedding-m",
|
|
"name": "jina",
|
|
"model": "jina-embeddings-v3",
|
|
"dimension": "1024",
|
|
}
|
|
],
|
|
}
|
|
)
|
|
resolved = resolve_embedding_runtime_config(catalog=catalog)
|
|
assert resolved.provider_name == "jina"
|
|
assert resolved.provider_mode == "standard"
|
|
assert resolved.effective_url == "https://api.jina.ai/v1/embeddings"
|
|
assert resolved.extra_headers == {"X-App": "demo"}
|
|
assert resolved.dimension == 1024
|
|
|
|
|
|
def test_embedding_orcarouter_binding_uses_default_endpoint() -> None:
|
|
catalog = _build_catalog(
|
|
embedding_profile={
|
|
"id": "embedding-p",
|
|
"name": "Embedding",
|
|
"binding": "orcarouter",
|
|
"base_url": "",
|
|
"api_key": "sk-orca-test-key",
|
|
"api_version": "",
|
|
"extra_headers": {},
|
|
"models": [
|
|
{
|
|
"id": "embedding-m",
|
|
"name": "orcarouter",
|
|
"model": "openai/text-embedding-3-large",
|
|
"dimension": "3072",
|
|
}
|
|
],
|
|
}
|
|
)
|
|
resolved = resolve_embedding_runtime_config(catalog=catalog)
|
|
assert resolved.provider_name == "orcarouter"
|
|
assert resolved.provider_mode == "standard"
|
|
assert resolved.effective_url == "https://api.orcarouter.ai/v1/embeddings"
|
|
assert resolved.dimension == 3072
|
|
|
|
|
|
def test_embedding_runtime_preserves_api_key_array() -> None:
|
|
catalog = _build_catalog(
|
|
embedding_profile={
|
|
"id": "embedding-p",
|
|
"name": "Embedding pool",
|
|
"binding": "openai",
|
|
"base_url": "https://api.example.com/v1/embeddings",
|
|
"api_key": ["key-a", "key-b"],
|
|
"api_version": "",
|
|
"extra_headers": {},
|
|
"models": [
|
|
{
|
|
"id": "embedding-m",
|
|
"name": "m",
|
|
"model": "text-embedding-3-small",
|
|
"dimension": "1536",
|
|
}
|
|
],
|
|
}
|
|
)
|
|
assert resolve_embedding_runtime_config(catalog=catalog).api_key == ["key-a", "key-b"]
|
|
|
|
|
|
def test_embedding_alias_canonicalization_google_to_gemini() -> None:
|
|
catalog = _build_catalog(
|
|
embedding_profile={
|
|
"id": "embedding-p",
|
|
"name": "Embedding",
|
|
"binding": "google",
|
|
"base_url": "",
|
|
"api_key": "k",
|
|
"api_version": "",
|
|
"extra_headers": {},
|
|
"models": [{"id": "embedding-m", "name": "m", "model": "text-embedding-3-small"}],
|
|
}
|
|
)
|
|
resolved = resolve_embedding_runtime_config(catalog=catalog)
|
|
assert resolved.provider_name == "gemini"
|
|
assert resolved.binding == "gemini"
|
|
|
|
|
|
def test_embedding_gemini_default_base_and_profile_key() -> None:
|
|
"""An existing gemini-embedding-001 profile with no explicit endpoint must
|
|
keep the OpenAI-compatible URL — the native route sends a taskType and
|
|
L2-normalizes, so moving it would invalidate the index built from it."""
|
|
catalog = _build_catalog(
|
|
embedding_profile={
|
|
"id": "embedding-p",
|
|
"name": "Embedding",
|
|
"binding": "gemini",
|
|
"base_url": "",
|
|
"api_key": "gemini-test-key",
|
|
"api_version": "",
|
|
"extra_headers": {},
|
|
"models": [{"id": "embedding-m", "name": "m", "model": "gemini-embedding-001"}],
|
|
}
|
|
)
|
|
resolved = resolve_embedding_runtime_config(catalog=catalog)
|
|
assert resolved.provider_name == "gemini"
|
|
assert resolved.binding == "gemini"
|
|
assert resolved.api_key == "gemini-test-key"
|
|
assert (
|
|
resolved.effective_url
|
|
== "https://generativelanguage.googleapis.com/v1beta/openai/embeddings"
|
|
)
|
|
|
|
|
|
def test_embedding_gemini_defaults_to_stable_embedding2() -> None:
|
|
spec = EMBEDDING_PROVIDERS["gemini"]
|
|
|
|
assert spec.adapter == "gemini"
|
|
assert spec.default_model == "gemini-embedding-2"
|
|
assert spec.default_dim == 3072
|
|
assert spec.default_api_base == NATIVE_GEMINI2_ENDPOINT
|
|
|
|
|
|
def test_embedding_gemini_embedding2_defaults_to_the_native_endpoint() -> None:
|
|
"""Embedding 2 is new, so nothing has an index on it yet — it can default
|
|
straight to the native batch endpoint that carries its features."""
|
|
catalog = _build_catalog(
|
|
embedding_profile={
|
|
"id": "embedding-p",
|
|
"name": "Embedding",
|
|
"binding": "gemini",
|
|
"base_url": "",
|
|
"api_key": "gemini-test-key",
|
|
"api_version": "",
|
|
"extra_headers": {},
|
|
"models": [
|
|
{
|
|
"id": "embedding-m",
|
|
"name": "m",
|
|
"model": "gemini-embedding-2",
|
|
}
|
|
],
|
|
}
|
|
)
|
|
|
|
resolved = resolve_embedding_runtime_config(catalog=catalog)
|
|
|
|
assert resolved.effective_url == NATIVE_GEMINI2_ENDPOINT
|
|
|
|
|
|
def test_embedding_gemini_explicit_native_endpoint_opts_any_model_in() -> None:
|
|
"""A saved native URL is used verbatim, which is how an older model can
|
|
still be pointed at the native route deliberately."""
|
|
catalog = _build_catalog(
|
|
embedding_profile={
|
|
"id": "embedding-p",
|
|
"name": "Embedding",
|
|
"binding": "gemini",
|
|
"base_url": NATIVE_GEMINI2_ENDPOINT,
|
|
"api_key": "gemini-test-key",
|
|
"api_version": "",
|
|
"extra_headers": {},
|
|
"models": [
|
|
{
|
|
"id": "embedding-m",
|
|
"name": "m",
|
|
"model": "gemini-embedding-2",
|
|
}
|
|
],
|
|
}
|
|
)
|
|
|
|
resolved = resolve_embedding_runtime_config(catalog=catalog)
|
|
|
|
assert resolved.effective_url == NATIVE_GEMINI2_ENDPOINT
|
|
|
|
|
|
def test_embedding_local_fallback_from_base_url() -> None:
|
|
catalog = _build_catalog(
|
|
embedding_profile={
|
|
"id": "embedding-p",
|
|
"name": "Embedding",
|
|
"binding": "",
|
|
"base_url": "http://localhost:11434",
|
|
"api_key": "",
|
|
"api_version": "",
|
|
"extra_headers": {},
|
|
"models": [{"id": "embedding-m", "name": "m", "model": "nomic-embed-text"}],
|
|
}
|
|
)
|
|
resolved = resolve_embedding_runtime_config(catalog=catalog)
|
|
assert resolved.provider_name == "ollama"
|
|
assert resolved.provider_mode == "local"
|
|
assert resolved.api_key == ""
|
|
|
|
|
|
def test_embedding_local_vllm_uses_profile_key() -> None:
|
|
catalog = _build_catalog(
|
|
embedding_profile={
|
|
"id": "embedding-p",
|
|
"name": "Embedding",
|
|
"binding": "vllm",
|
|
"base_url": "http://localhost:1234/v1/embeddings",
|
|
"api_key": "local-secret",
|
|
"api_version": "",
|
|
"extra_headers": {},
|
|
"models": [{"id": "embedding-m", "name": "m", "model": "text-embedding-model"}],
|
|
}
|
|
)
|
|
resolved = resolve_embedding_runtime_config(catalog=catalog)
|
|
assert resolved.provider_name == "vllm"
|
|
assert resolved.provider_mode == "local"
|
|
assert resolved.api_key == "local-secret"
|
|
|
|
|
|
def test_embedding_openai_default_base_injected() -> None:
|
|
catalog = _build_catalog(
|
|
embedding_profile={
|
|
"id": "embedding-p",
|
|
"name": "Embedding",
|
|
"binding": "openai",
|
|
"base_url": "",
|
|
"api_key": "sk-test",
|
|
"api_version": "",
|
|
"extra_headers": {},
|
|
"models": [{"id": "embedding-m", "name": "m", "model": "text-embedding-3-large"}],
|
|
}
|
|
)
|
|
resolved = resolve_embedding_runtime_config(catalog=catalog)
|
|
assert resolved.provider_name == "openai"
|
|
# v1.3.0: provider defaults are full embedding endpoint URLs.
|
|
assert resolved.effective_url == "https://api.openai.com/v1/embeddings"
|
|
|
|
|
|
def test_embedding_send_dimensions_default_is_none() -> None:
|
|
"""Catalogs without the field should resolve to ``None`` (Auto behaviour)."""
|
|
catalog = _build_catalog() # default model has no `send_dimensions`
|
|
resolved = resolve_embedding_runtime_config(catalog=catalog)
|
|
assert resolved.send_dimensions is None
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("catalog_value", "expected"),
|
|
[
|
|
(True, True),
|
|
(False, False),
|
|
("true", True),
|
|
("false", False),
|
|
("on", True),
|
|
("off", False),
|
|
("", None),
|
|
("garbage", None),
|
|
],
|
|
)
|
|
def test_embedding_send_dimensions_parsed_from_catalog(
|
|
catalog_value: object,
|
|
expected: bool | None,
|
|
) -> None:
|
|
catalog = _build_catalog(
|
|
embedding_model={
|
|
"id": "embedding-m",
|
|
"name": "m",
|
|
"model": "text-embedding-v4",
|
|
"dimension": "1024",
|
|
"send_dimensions": catalog_value,
|
|
}
|
|
)
|
|
resolved = resolve_embedding_runtime_config(catalog=catalog)
|
|
assert resolved.send_dimensions is expected
|
|
|
|
|
|
def test_embedding_send_dimensions_catalog_unset_stays_auto() -> None:
|
|
catalog = _build_catalog()
|
|
resolved = resolve_embedding_runtime_config(catalog=catalog)
|
|
assert resolved.send_dimensions is None
|
|
|
|
|
|
def test_embedding_send_dimensions_resolves_from_catalog() -> None:
|
|
catalog = _build_catalog(
|
|
embedding_model={
|
|
"id": "embedding-m",
|
|
"name": "m",
|
|
"model": "text-embedding-3-large",
|
|
"dimension": "3072",
|
|
"send_dimensions": True,
|
|
}
|
|
)
|
|
resolved = resolve_embedding_runtime_config(catalog=catalog)
|
|
assert resolved.send_dimensions is True
|
|
|
|
|
|
def test_embedding_custom_openai_sdk_uses_user_supplied_base_url() -> None:
|
|
"""Legacy `custom_openai_sdk` configs still resolve for backwards compatibility."""
|
|
catalog = _build_catalog(
|
|
embedding_profile={
|
|
"id": "embedding-p",
|
|
"name": "Embedding",
|
|
"binding": "custom_openai_sdk",
|
|
"base_url": "https://my-proxy.example.com/v1",
|
|
"api_key": "sk-custom",
|
|
"api_version": "",
|
|
"extra_headers": {},
|
|
"models": [
|
|
{
|
|
"id": "embedding-m",
|
|
"name": "m",
|
|
"model": "text-embedding-3-large",
|
|
"dimension": "3072",
|
|
}
|
|
],
|
|
}
|
|
)
|
|
resolved = resolve_embedding_runtime_config(catalog=catalog)
|
|
assert resolved.provider_name == "custom_openai_sdk"
|
|
assert resolved.binding == "custom_openai_sdk"
|
|
assert resolved.effective_url == "https://my-proxy.example.com/v1"
|
|
assert resolved.api_key == "sk-custom"
|
|
|
|
|
|
def test_embedding_openrouter_default_base_url_injected() -> None:
|
|
"""When no base URL is set, the OpenRouter spec's default fills in."""
|
|
catalog = _build_catalog(
|
|
embedding_profile={
|
|
"id": "embedding-p",
|
|
"name": "Embedding",
|
|
"binding": "openrouter",
|
|
"base_url": "",
|
|
"api_key": "sk-or-xxxxx",
|
|
"api_version": "",
|
|
"extra_headers": {},
|
|
"models": [
|
|
{
|
|
"id": "embedding-m",
|
|
"name": "m",
|
|
"model": "qwen/qwen3-embedding-8b",
|
|
}
|
|
],
|
|
}
|
|
)
|
|
resolved = resolve_embedding_runtime_config(catalog=catalog)
|
|
assert resolved.provider_name == "openrouter"
|
|
assert resolved.binding == "openrouter"
|
|
assert resolved.effective_url == "https://openrouter.ai/api/v1/embeddings"
|
|
assert EMBEDDING_PROVIDERS["openrouter"].adapter == "openai_compat"
|
|
|
|
|
|
def test_embedding_openrouter_profile_key() -> None:
|
|
catalog = _build_catalog(
|
|
embedding_profile={
|
|
"id": "embedding-p",
|
|
"name": "Embedding",
|
|
"binding": "openrouter",
|
|
"base_url": "",
|
|
"api_key": "sk-or-from-profile",
|
|
"api_version": "",
|
|
"extra_headers": {},
|
|
"models": [{"id": "embedding-m", "name": "m", "model": "qwen/qwen3-embedding-8b"}],
|
|
}
|
|
)
|
|
resolved = resolve_embedding_runtime_config(catalog=catalog)
|
|
assert resolved.provider_name == "openrouter"
|
|
assert resolved.api_key == "sk-or-from-profile"
|
|
|
|
|
|
def test_embedding_provider_profile_key() -> None:
|
|
catalog = _build_catalog(
|
|
embedding_profile={
|
|
"id": "embedding-p",
|
|
"name": "Embedding",
|
|
"binding": "cohere",
|
|
"base_url": "",
|
|
"api_key": "cohere-test-key",
|
|
"api_version": "",
|
|
"extra_headers": {},
|
|
"models": [{"id": "embedding-m", "name": "m", "model": "embed-v4.0"}],
|
|
}
|
|
)
|
|
resolved = resolve_embedding_runtime_config(catalog=catalog)
|
|
assert resolved.provider_name == "cohere"
|
|
assert resolved.api_key == "cohere-test-key"
|