Repository navigation
chore(backport): add the remaining jev changes to stable/1.100.x #42668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devin-ai-integration
wants to merge
19
commits into
stable/1.100.x
Choose a base branch
from
litellm_jev_backport_1_100_x
base: stable/1.100.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
61d725d
fix(proxy): apply team model aliases on the JWT auth path
ryan-crabbe-berri 44b7b48
feat(auto-router): allow opted-in team members to manage their routers
tin-berri d400ee2
feat(router): add classifier circuit breaker
moe-berri 8bf252b
feat(router): add TypeSafe Jev as a complexity router classifier
mateo-berri ded7f60
fix(proxy): forward every method on the typesafe pass-through route
yuneng-berri d1bf609
feat(guardrails): add TypeSafe Jev relevance-based compaction guardrail
yassin-berriai 4fa1e08
fix(proxy): enforce virtual key budgets for JEV test routing
moe-berri 73e50a0
feat(auto-router): add JEV classifier alongside LLM classifier
moe-berri c3c466a
fix(ui): adapt the jev dashboard pieces to stable/1.102.x
mateo-berri 12ef369
chore(backport): regenerate the openapi snapshot and dashboard api ty…
devin-ai-integration[bot] 746686c
feat(openrouter): price typesafe/jev-1.13 and add an openrouter decis…
devin-ai-integration[bot] 8e4c26f
fix(ui): adapt the jev dashboard pieces to stable/1.100.x
devin-ai-integration[bot] 46f81ba
fix: place lint suppressions on the flagged annotation lines
devin-ai-integration[bot] a392f7b
fix(typing): clear the basedpyright errors the picks introduced
devin-ai-integration[bot] 54b42e3
fix(proxy): port the member auto-router write path and grant plumbing…
devin-ai-integration[bot] 4864227
test(proxy): restore the member-scope parametrization and cover team …
devin-ai-integration[bot] 77915d4
fix(proxy): port append_team_models for member auto-router create on …
devin-ai-integration[bot] e39e1c8
fix(proxy): take the write-slot advisory lock before member name checks
devin-ai-integration[bot] 55d1597
revert(proxy): drop the member auto-router write-path port from stabl…
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,6 +95,7 @@ | |
| "/vllm/", | ||
| "/mistral/", | ||
| "/typesafe/", | ||
| "/openrouter/", | ||
| "/groq/", | ||
| "/voyage/", | ||
| "/cursor/", | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| """Project a team row (plus the caller's membership in it) onto the ``team_*`` fields of ``UserAPIKeyAuth``. | ||
|
|
||
| The virtual-key path gets these fields for free from the combined-view SQL join. Every other auth path | ||
| starts from a ``LiteLLM_TeamTable`` object instead and has to copy them over by hand, which is how JWT | ||
| callers kept losing grants (aliases, permissions, limits) one field at a time. Build the badge through | ||
| ``team_grants`` and the two paths cannot drift. | ||
| """ | ||
|
|
||
| from collections.abc import Mapping, Sequence | ||
| from types import MappingProxyType | ||
| from typing import Annotated, Final | ||
|
|
||
| from pydantic import BaseModel, BeforeValidator, ConfigDict, TypeAdapter, ValidationError | ||
| from pydantic.main import IncEx | ||
| from typing_extensions import ReadOnly, TypedDict | ||
|
|
||
| from litellm.proxy._types import ( | ||
| LiteLLM_ObjectPermissionTable, | ||
| LiteLLM_TeamMembership, | ||
| LiteLLM_TeamTable, | ||
| Member, | ||
| ) | ||
|
|
||
| _MODEL_ALIASES_ADAPTER: Final = TypeAdapter(dict[str, str]) | ||
| _JSON_COLUMNS: Final[Mapping[str, IncEx | bool]] = MappingProxyType( | ||
| {"metadata": True, "litellm_model_table": MappingProxyType({"model_aliases": True})} | ||
| ) | ||
|
|
||
|
|
||
| def _decode_model_aliases(value: object) -> object: | ||
| """``LiteLLM_ModelTable.model_aliases`` is typed ``str | dict``; writers hand Prisma ``json.dumps(...)``, so take both.""" | ||
| if not isinstance(value, str): | ||
| return value | ||
| try: | ||
| return _MODEL_ALIASES_ADAPTER.validate_json(value) | ||
| except ValidationError: | ||
| return None | ||
|
|
||
|
|
||
| class TeamModelAliasTable(BaseModel): | ||
| model_config = ConfigDict(protected_namespaces=()) | ||
|
|
||
| model_aliases: Annotated[Mapping[str, str] | None, BeforeValidator(_decode_model_aliases)] = None | ||
|
|
||
|
|
||
| class _TeamJsonColumns(BaseModel): | ||
| """The two loosely typed columns on ``LiteLLM_TeamTable``, re-read with the shape the badge needs.""" | ||
|
|
||
| metadata: Mapping[str, object] | None = None | ||
| litellm_model_table: TeamModelAliasTable | None = None | ||
|
|
||
|
|
||
| class TeamGrants(TypedDict, total=False): | ||
| """Keyword arguments for ``UserAPIKeyAuth``. Empty when the caller has no team, so the model's own defaults apply.""" | ||
|
|
||
| team_alias: ReadOnly[str | None] | ||
| team_tpm_limit: ReadOnly[int | None] | ||
| team_rpm_limit: ReadOnly[int | None] | ||
| team_tpd_limit: ReadOnly[int | None] | ||
| team_max_budget: ReadOnly[float | None] | ||
| team_soft_budget: ReadOnly[float | None] | ||
| team_model_max_budget: ReadOnly[dict[str, object] | None] # mutable-ok: prisma table field typed loosely | ||
| team_spend: ReadOnly[float | None] | ||
| team_models: ReadOnly[Sequence[str]] | ||
| team_blocked: ReadOnly[bool] | ||
| team_metadata: ReadOnly[Mapping[str, object] | None] | ||
| team_model_aliases: ReadOnly[Mapping[str, str] | None] | ||
| team_object_permission_id: ReadOnly[str | None] | ||
| team_object_permission: ReadOnly[LiteLLM_ObjectPermissionTable | None] | ||
| team_member: ReadOnly[Member | None] | ||
| team_member_spend: ReadOnly[float | None] | ||
| team_member_tpm_limit: ReadOnly[int | None] | ||
| team_member_rpm_limit: ReadOnly[int | None] | ||
|
|
||
|
|
||
| def _json_columns(team_object: LiteLLM_TeamTable) -> _TeamJsonColumns: | ||
| try: | ||
| return _TeamJsonColumns.model_validate(team_object.model_dump(include=_JSON_COLUMNS)) | ||
| except ValidationError: | ||
| return _TeamJsonColumns() | ||
|
|
||
|
|
||
| def team_model_aliases(team_object: LiteLLM_TeamTable | None) -> Mapping[str, str] | None: | ||
| if team_object is None: | ||
| return None | ||
| alias_table: Final = _json_columns(team_object).litellm_model_table | ||
| return alias_table.model_aliases if alias_table is not None else None | ||
|
|
||
|
|
||
| def team_grants( | ||
| team_object: LiteLLM_TeamTable | None, | ||
| team_membership: LiteLLM_TeamMembership | None, | ||
| user_id: str | None, | ||
| ) -> TeamGrants: | ||
| if team_object is None: | ||
| return TeamGrants() | ||
| json_columns: Final = _json_columns(team_object) | ||
| return TeamGrants( | ||
| team_alias=team_object.team_alias, | ||
| team_tpm_limit=team_object.tpm_limit, | ||
| team_rpm_limit=team_object.rpm_limit, | ||
| team_tpd_limit=team_object.tpd_limit, | ||
| team_max_budget=team_object.max_budget, | ||
| team_soft_budget=team_object.soft_budget, | ||
| team_model_max_budget=team_object.model_max_budget, | ||
| team_spend=team_object.spend, | ||
| team_models=tuple(team_object.models), | ||
| team_blocked=team_object.blocked, | ||
| team_metadata=json_columns.metadata, | ||
| team_model_aliases=( | ||
| json_columns.litellm_model_table.model_aliases if json_columns.litellm_model_table is not None else None | ||
| ), | ||
| team_object_permission_id=team_object.object_permission_id, | ||
| team_object_permission=team_object.object_permission, | ||
| team_member=next( | ||
| (m for m in team_object.members_with_roles if user_id is not None and m.user_id == user_id), | ||
| None, | ||
| ), | ||
| team_member_spend=team_membership.spend if team_membership is not None else None, | ||
| team_member_tpm_limit=( | ||
| team_membership.safe_get_team_member_tpm_limit() if team_membership is not None else None | ||
| ), | ||
| team_member_rpm_limit=( | ||
| team_membership.safe_get_team_member_rpm_limit() if team_membership is not None else None | ||
| ), | ||
| ) | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
74 changes: 74 additions & 0 deletions
74
litellm/proxy/guardrails/guardrail_hooks/typesafe/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Final | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
| from litellm.types.guardrails import ( | ||
| GuardrailEventHooks, | ||
| Mode, | ||
| SupportedGuardrailIntegrations, | ||
| ) | ||
| from litellm.types.proxy.guardrails.guardrail_hooks.typesafe import ( | ||
| TypeSafeGuardrailOptionalParams, | ||
| ) | ||
|
|
||
| from .typesafe import TypeSafeGuardrail | ||
|
|
||
| if TYPE_CHECKING: | ||
| from litellm.types.guardrails import Guardrail, LitellmParams | ||
|
|
||
|
|
||
| def _coerce_event_hook( | ||
| mode: str | list[str] | Mode, # mutable-ok: event hook unions accept an ordered list | ||
| ) -> GuardrailEventHooks | list[GuardrailEventHooks] | Mode: # mutable-ok: event hook unions accept an ordered list | ||
| if isinstance(mode, Mode): | ||
| return mode | ||
| if isinstance(mode, list): | ||
| return [ # mutable-ok: CustomGuardrail event_hook contract wants a list | ||
| GuardrailEventHooks(item) for item in mode | ||
| ] | ||
| return GuardrailEventHooks(mode) | ||
|
|
||
|
|
||
| def _optional_params(litellm_params: LitellmParams) -> TypeSafeGuardrailOptionalParams: | ||
| value: Final = litellm_params.optional_params | ||
| if isinstance(value, TypeSafeGuardrailOptionalParams): | ||
| return value | ||
| if isinstance(value, BaseModel): | ||
| return TypeSafeGuardrailOptionalParams.model_validate(value.model_dump()) | ||
| return TypeSafeGuardrailOptionalParams() | ||
|
|
||
|
|
||
| def initialize_guardrail(litellm_params: LitellmParams, guardrail: Guardrail) -> TypeSafeGuardrail: | ||
| import litellm | ||
|
|
||
| optional_params: Final = _optional_params(litellm_params) | ||
|
|
||
| _callback: Final = TypeSafeGuardrail( | ||
| api_base=litellm_params.api_base, | ||
| api_key=litellm_params.api_key, | ||
| model=litellm_params.model, | ||
| relevance_threshold=optional_params.relevance_threshold, | ||
| min_chars_to_evaluate=optional_params.min_chars_to_evaluate, | ||
| max_result_chars_in_state=optional_params.max_result_chars_in_state, | ||
| guardrail_name=guardrail["guardrail_name"], | ||
| event_hook=_coerce_event_hook(litellm_params.mode), | ||
| default_on=litellm_params.default_on or False, | ||
| unreachable_fallback=( | ||
| litellm_params.unreachable_fallback if "unreachable_fallback" in litellm_params.model_fields_set else None | ||
| ), | ||
| ) | ||
| litellm.logging_callback_manager.add_litellm_callback( # pyright: ignore[reportUnknownMemberType] # callback manager is untyped | ||
| _callback | ||
| ) | ||
| return _callback | ||
|
|
||
|
|
||
| guardrail_initializer_registry: Final = { # mutable-ok: guardrail_registry discovery checks isinstance(registry, dict) | ||
| SupportedGuardrailIntegrations.TYPESAFE.value: initialize_guardrail, | ||
| } | ||
|
|
||
| guardrail_class_registry: Final = { # mutable-ok: guardrail_registry discovery checks isinstance(registry, dict) | ||
| SupportedGuardrailIntegrations.TYPESAFE.value: TypeSafeGuardrail, | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.