mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-01 01:18:34 +00:00
* feat(ui): add decisions and classifier providers to the Add Model form Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(cost): use literal upstream decisions paths Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix: drop duplicate TYPESAFE provider fields entry superseded by main's TypeSafe Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * style(ui): drop stray blank line in provider_info_helpers test Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(proxy): type the OSS decisions provider fields test * fix(decisions): keep Laya and Bespoke env keys on their configured server Route laya and bespoke Decisions deployments through the same oss_connection and validate_oss_model rules the pass-through and the classifier router use, so LAYA_API_KEY and BESPOKE_API_KEY only go to LAYA_API_BASE and BESPOKE_API_BASE, an api_base with credentials, a query, or a non-HTTP scheme answers 400, and an unknown checkpoint name answers 400 before any upstream call * fix(decisions): send the Laya and Bespoke env key to a form-added model on the env server A deployment added through Add Model always carries an API base, so the env key only reached deployments with no base. The env key now goes to a deployment whose base matches LAYA_API_BASE or BESPOKE_API_BASE, an empty deployment key counts as no key, and the Bespoke placeholder is the name Ollama serves. * test(decisions): cover the env-base classifier case in the complexity router test * fix(decisions): match the env server with or without /v1 and point Ollama users at bespoke/nimble * fix(decisions): call /v1/systemone once from classifiers and pass-through on a /v1 base * fix(decisions): strip /v1 from the API base path only, so a host named v1 still works --------- Co-authored-by: kerry <kerry@berri.ai> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
75 lines
2.9 KiB
Python
75 lines
2.9 KiB
Python
from collections.abc import Mapping
|
|
from dataclasses import dataclass, field
|
|
from types import MappingProxyType
|
|
from typing import Final, Literal, TypeAlias
|
|
from urllib.parse import urlsplit
|
|
|
|
from pydantic import AnyHttpUrl, TypeAdapter, ValidationError
|
|
|
|
from litellm.secret_managers.main import get_secret_str
|
|
|
|
OssDecisionProvider: TypeAlias = Literal["laya", "bespoke"]
|
|
OSS_DECISION_MODELS: Final = MappingProxyType(
|
|
{
|
|
"laya": ("english", "multilingual", "typed-decisions"),
|
|
"bespoke": ("nimble-latest", "nimble", "bespokelabs/Bespoke-Nimble-9B"),
|
|
}
|
|
)
|
|
|
|
|
|
def validate_oss_model(provider: OssDecisionProvider, value: object) -> str:
|
|
if not isinstance(value, str) or value not in OSS_DECISION_MODELS[provider]:
|
|
raise ValueError(f"{provider} model must be one of {', '.join(OSS_DECISION_MODELS[provider])}")
|
|
return value
|
|
|
|
|
|
def validate_oss_request(provider: OssDecisionProvider, body: Mapping[str, object]) -> str:
|
|
if "custom_body" in body:
|
|
raise ValueError(f"custom_body is not supported for {provider} requests")
|
|
if body.get("stream"):
|
|
raise ValueError(f"Streaming is not supported for {provider} requests")
|
|
return validate_oss_model(provider, body.get("model"))
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class OssDecisionConnection:
|
|
api_base: str
|
|
api_key: str | None = field(repr=False)
|
|
|
|
|
|
def validate_oss_api_base(provider: OssDecisionProvider, value: str) -> str:
|
|
try:
|
|
url: Final = TypeAdapter(AnyHttpUrl).validate_python(value)
|
|
except ValidationError as exc:
|
|
raise ValueError(f"{provider} api_base must be an HTTP or HTTPS server URL") from exc
|
|
if url.username or url.password or url.query or url.fragment:
|
|
raise ValueError(f"{provider} api_base must not contain credentials, a query, or a fragment")
|
|
return str(url).rstrip("/")
|
|
|
|
|
|
def _server_root(provider: OssDecisionProvider, base: str) -> str:
|
|
parts: Final = urlsplit(validate_oss_api_base(provider, base))
|
|
return parts._replace(path=parts.path.removesuffix("/v1")).geturl()
|
|
|
|
|
|
def _is_env_server(provider: OssDecisionProvider, server: str, env_base: str | None) -> bool:
|
|
if not env_base:
|
|
return False
|
|
try:
|
|
return _server_root(provider, env_base) == server
|
|
except ValueError:
|
|
return False
|
|
|
|
|
|
def oss_connection(
|
|
provider: OssDecisionProvider, api_base: str | None = None, api_key: str | None = None
|
|
) -> OssDecisionConnection:
|
|
env_base: Final = get_secret_str(f"{provider.upper()}_API_BASE")
|
|
base: Final = api_base if api_base is not None else env_base
|
|
if not base:
|
|
raise ValueError(f"{provider} requires api_base or {provider.upper()}_API_BASE pointing to its server")
|
|
server: Final = _server_root(provider, base)
|
|
env_key: Final = (
|
|
get_secret_str(f"{provider.upper()}_API_KEY") if _is_env_server(provider, server, env_base) else None
|
|
)
|
|
return OssDecisionConnection(api_base=server, api_key=api_key or env_key or None)
|