feat: classify provider prompt-cache strategy (explicit/implicit/none)

This commit is contained in:
m4
2026-08-20 08:27:23 +08:00
parent 836ff5c939
commit 8d7d95a20d
2 changed files with 32 additions and 3 deletions
+20 -3
View File
@@ -333,11 +333,28 @@ def _drop_unsupported_chat_model_kwargs(kwargs: dict[str, Any]) -> None:
model_kwargs.pop(key, None)
_IMPLICIT_CACHE_PROVIDERS = frozenset({"zhipu", "zhipu-code", "siliconflow", "nvidia"})
def _cache_strategy(provider: str, model_id: str) -> str:
"""Return the provider's prompt-cache mechanism.
``explicit`` — needs Anthropic-style ``cache_control`` markers (OpenRouter
Claude routes only, matching the pre-existing condition).
``implicit`` — provider prefixes-cache automatically; no markers, but the
prompt prefix must stay byte-stable for hits (see memory injection order).
``none`` — no cache model to declare.
"""
if provider == "openrouter" and model_id.startswith(("anthropic/", "~anthropic/")):
return "explicit"
if provider in _IMPLICIT_CACHE_PROVIDERS:
return "implicit"
return "none"
def _supports_openrouter_anthropic_prompt_cache(provider: str, model_id: str) -> bool:
"""Return whether EvoScientist should declare OpenRouter Claude caching."""
return provider == "openrouter" and model_id.startswith(
("anthropic/", "~anthropic/")
)
return _cache_strategy(provider, model_id) == "explicit"
def _has_cache_control_override(kwargs: dict[str, Any]) -> bool:
+12
View File
@@ -965,6 +965,18 @@ class TestThirdPartyRouting:
assert "thinking" not in call_kwargs
def test_cache_strategy_classifies_providers():
from EvoScientist.llm.models import _cache_strategy
assert _cache_strategy("openrouter", "anthropic/claude-sonnet-4-6") == "explicit"
assert _cache_strategy("openrouter", "x-ai/grok-4.3") == "none"
assert _cache_strategy("anthropic", "claude-sonnet-4-6") == "none"
assert _cache_strategy("zhipu", "glm-5.2") == "implicit"
assert _cache_strategy("zhipu-code", "glm-5.2") == "implicit"
assert _cache_strategy("siliconflow", "glm-5.2") == "implicit"
assert _cache_strategy("ollama", "llama3.1:8b") == "none"
# =============================================================================
# Test MiniMax provider
# =============================================================================