fix: use config key to detect provider, prevent api_base misidentifying as vLLM

This commit is contained in:
Re-bin
2026-02-08 19:31:25 +00:00
parent 2931694eb8
commit eb2fbf80da
5 changed files with 72 additions and 44 deletions

View File

@@ -26,18 +26,16 @@ class LiteLLMProvider(LLMProvider):
api_base: str | None = None,
default_model: str = "anthropic/claude-opus-4-5",
extra_headers: dict[str, str] | None = None,
provider_name: str | None = None,
):
super().__init__(api_key, api_base)
self.default_model = default_model
self.extra_headers = extra_headers or {}
# Detect gateway / local deployment from api_key and api_base
self._gateway = find_gateway(api_key, api_base)
# Backwards-compatible flags (used by tests and possibly external code)
self.is_openrouter = bool(self._gateway and self._gateway.name == "openrouter")
self.is_aihubmix = bool(self._gateway and self._gateway.name == "aihubmix")
self.is_vllm = bool(self._gateway and self._gateway.is_local)
# Detect gateway / local deployment.
# provider_name (from config key) is the primary signal;
# api_key / api_base are fallback for auto-detection.
self._gateway = find_gateway(provider_name, api_key, api_base)
# Configure environment variables
if api_key:
@@ -51,23 +49,24 @@ class LiteLLMProvider(LLMProvider):
def _setup_env(self, api_key: str, api_base: str | None, model: str) -> None:
"""Set environment variables based on detected provider."""
if self._gateway:
# Gateway / local: direct set (not setdefault)
os.environ[self._gateway.env_key] = api_key
spec = self._gateway or find_by_model(model)
if not spec:
return
# Standard provider: match by model name
spec = find_by_model(model)
if spec:
# Gateway/local overrides existing env; standard provider doesn't
if self._gateway:
os.environ[spec.env_key] = api_key
else:
os.environ.setdefault(spec.env_key, api_key)
# Resolve env_extras placeholders:
# {api_key} → user's API key
# {api_base} → user's api_base, falling back to spec.default_api_base
effective_base = api_base or spec.default_api_base
for env_name, env_val in spec.env_extras:
resolved = env_val.replace("{api_key}", api_key)
resolved = resolved.replace("{api_base}", effective_base)
os.environ.setdefault(env_name, resolved)
# Resolve env_extras placeholders:
# {api_key} → user's API key
# {api_base} → user's api_base, falling back to spec.default_api_base
effective_base = api_base or spec.default_api_base
for env_name, env_val in spec.env_extras:
resolved = env_val.replace("{api_key}", api_key)
resolved = resolved.replace("{api_base}", effective_base)
os.environ.setdefault(env_name, resolved)
def _resolve_model(self, model: str) -> str:
"""Resolve model name by applying provider/gateway prefixes."""
@@ -131,7 +130,7 @@ class LiteLLMProvider(LLMProvider):
# Apply model-specific overrides (e.g. kimi-k2.5 temperature)
self._apply_model_overrides(model, kwargs)
# Pass api_base directly for custom endpoints (vLLM, etc.)
# Pass api_base for custom endpoints
if self.api_base:
kwargs["api_base"] = self.api_base