mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-15 09:12:18 +00:00
Merge d929ee6c6d into 60fc121ce8
This commit is contained in:
commit
3ae29b2e8a
2 changed files with 49 additions and 20 deletions
|
|
@ -1359,6 +1359,8 @@ def update_valid_token_with_end_user_params(valid_token: UserAPIKeyAuth, end_use
|
|||
valid_token.end_user_rpm_limit = end_user_params["end_user_rpm_limit"]
|
||||
if end_user_params.get("end_user_tpd_limit") is not None:
|
||||
valid_token.end_user_tpd_limit = end_user_params["end_user_tpd_limit"]
|
||||
if cast(dict[str, object], end_user_params).get("end_user_max_budget") is not None: # cast-ok: pin dict param types
|
||||
valid_token.end_user_max_budget = end_user_params["end_user_max_budget"] # rebind-ok: sibling limit write-back
|
||||
if end_user_params.get("allowed_model_region") is not None:
|
||||
valid_token.allowed_model_region = end_user_params["allowed_model_region"]
|
||||
if end_user_params.get("end_user_model_max_budget") is not None:
|
||||
|
|
@ -4561,6 +4563,12 @@ async def _lookup_end_user_and_apply_budget(
|
|||
budget_info=default_budget,
|
||||
end_user_id=valid_token.end_user_id or "",
|
||||
)
|
||||
# This branch has no end-user row, so the budget here is the key's
|
||||
# ``end_user_budget_id`` (or the proxy-wide default) - a fallback, not
|
||||
# this end user's own budget. It must only fill a cap the custom auth
|
||||
# callable left unset, never loosen one it set, so keep it out of the
|
||||
# write-back and let the fill-only assignment below apply it.
|
||||
end_user_params.pop("end_user_max_budget", None)
|
||||
valid_token = update_valid_token_with_end_user_params(
|
||||
valid_token=valid_token, end_user_params=end_user_params
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,15 +1,17 @@
|
|||
import pytest
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
import litellm
|
||||
from litellm.proxy.auth.user_api_key_auth import (
|
||||
_run_post_custom_auth_checks,
|
||||
update_valid_token_with_end_user_params,
|
||||
)
|
||||
from litellm.proxy._types import (
|
||||
LiteLLM_BudgetTable,
|
||||
LiteLLM_EndUserTable,
|
||||
UserAPIKeyAuth,
|
||||
)
|
||||
from litellm.proxy.auth.user_api_key_auth import (
|
||||
_run_post_custom_auth_checks,
|
||||
update_valid_token_with_end_user_params,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
|
@ -21,9 +23,7 @@ async def test_custom_auth_run_post_custom_auth_checks_without_end_user_id():
|
|||
valid_token = UserAPIKeyAuth(token="test_token")
|
||||
|
||||
# Default: common_checks should NOT be called inside the helper
|
||||
with patch(
|
||||
"litellm.proxy.auth.user_api_key_auth.common_checks", new_callable=AsyncMock
|
||||
) as mock_common:
|
||||
with patch("litellm.proxy.auth.user_api_key_auth.common_checks", new_callable=AsyncMock) as mock_common:
|
||||
mock_common.return_value = True
|
||||
result = await _run_post_custom_auth_checks(
|
||||
valid_token=valid_token,
|
||||
|
|
@ -39,9 +39,7 @@ async def test_custom_auth_run_post_custom_auth_checks_without_end_user_id():
|
|||
# With opt-in flag: still not from the helper — the centralized gate
|
||||
# in the wrapper handles it.
|
||||
with (
|
||||
patch(
|
||||
"litellm.proxy.auth.user_api_key_auth.common_checks", new_callable=AsyncMock
|
||||
) as mock_common,
|
||||
patch("litellm.proxy.auth.user_api_key_auth.common_checks", new_callable=AsyncMock) as mock_common,
|
||||
patch(
|
||||
"litellm.proxy.proxy_server.general_settings",
|
||||
{"custom_auth_run_common_checks": True},
|
||||
|
|
@ -64,15 +62,11 @@ async def test_custom_auth_run_post_custom_auth_checks_with_end_user_budget_exce
|
|||
valid_token = UserAPIKeyAuth(
|
||||
token="test_token",
|
||||
end_user_id="test_user",
|
||||
end_user_model_max_budget={
|
||||
"gpt-4": {"budget_limit": 10.0, "time_period": "1d"}
|
||||
},
|
||||
end_user_model_max_budget={"gpt-4": {"budget_limit": 10.0, "time_period": "1d"}},
|
||||
)
|
||||
request_data = {"model": "gpt-4"}
|
||||
|
||||
with patch(
|
||||
"litellm.proxy.auth.user_api_key_auth.common_checks", new_callable=AsyncMock
|
||||
):
|
||||
with patch("litellm.proxy.auth.user_api_key_auth.common_checks", new_callable=AsyncMock):
|
||||
with patch(
|
||||
"litellm.proxy.proxy_server.model_max_budget_limiter.is_end_user_within_model_budget",
|
||||
new_callable=AsyncMock,
|
||||
|
|
@ -106,9 +100,7 @@ async def test_custom_auth_enforces_end_user_budget_when_common_checks_skipped()
|
|||
litellm_budget_table=LiteLLM_BudgetTable(max_budget=1.0),
|
||||
)
|
||||
|
||||
async def mock_get_current_spend(
|
||||
counter_key, fallback_spend, max_budget=None, **kwargs
|
||||
):
|
||||
async def mock_get_current_spend(counter_key, fallback_spend, max_budget=None, **kwargs):
|
||||
if counter_key == "spend:end_user:customer-1":
|
||||
return 5.0
|
||||
return fallback_spend
|
||||
|
|
@ -403,3 +395,32 @@ def test_end_user_budget_tpd_limit_reaches_the_token():
|
|||
|
||||
assert result.end_user_rpm_limit == 5
|
||||
assert result.end_user_tpd_limit == 750000
|
||||
|
||||
|
||||
def test_end_user_max_budget_reaches_the_token():
|
||||
from litellm.proxy.auth.user_api_key_auth import _apply_budget_limits_to_end_user_params
|
||||
|
||||
end_user_params = {"end_user_id": "user_1"}
|
||||
_apply_budget_limits_to_end_user_params(
|
||||
end_user_params=end_user_params,
|
||||
budget_info=LiteLLM_BudgetTable(max_budget=20.0),
|
||||
end_user_id="user_1",
|
||||
)
|
||||
result = update_valid_token_with_end_user_params(UserAPIKeyAuth(token="test_token"), end_user_params)
|
||||
|
||||
assert result.end_user_max_budget == 20.0
|
||||
|
||||
|
||||
def test_end_user_max_budget_is_not_leaked_from_a_previous_end_user():
|
||||
from litellm.proxy.auth.user_api_key_auth import _apply_budget_limits_to_end_user_params
|
||||
|
||||
reused_token = UserAPIKeyAuth(token="test_token", end_user_max_budget=2.0)
|
||||
end_user_params = {"end_user_id": "user_2"}
|
||||
_apply_budget_limits_to_end_user_params(
|
||||
end_user_params=end_user_params,
|
||||
budget_info=LiteLLM_BudgetTable(max_budget=20.0),
|
||||
end_user_id="user_2",
|
||||
)
|
||||
result = update_valid_token_with_end_user_params(reused_token, end_user_params)
|
||||
|
||||
assert result.end_user_max_budget == 20.0
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue