|
| 1 | +from datetime import datetime, timezone, timedelta |
| 2 | + |
| 3 | +import pytest |
| 4 | +from freezegun import freeze_time |
| 5 | + |
| 6 | +from llm_accounting import LLMAccounting |
| 7 | +from llm_accounting.backends.sqlite import SQLiteBackend |
| 8 | +from llm_accounting.models.limits import (LimitScope, LimitType, TimeInterval, |
| 9 | + UsageLimitDTO) |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def sqlite_backend_for_accounting(tmp_path): |
| 14 | + db_path = str(tmp_path / "test_accounting_minute.sqlite") |
| 15 | + backend = SQLiteBackend(db_path=db_path) |
| 16 | + backend.initialize() |
| 17 | + yield backend |
| 18 | + backend.close() |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def accounting_instance(sqlite_backend_for_accounting): |
| 23 | + acc = LLMAccounting(backend=sqlite_backend_for_accounting) |
| 24 | + yield acc |
| 25 | + |
| 26 | + |
| 27 | +def test_account_model_requests_per_minute(accounting_instance: LLMAccounting, sqlite_backend_for_accounting: SQLiteBackend): |
| 28 | + """Test requests per minute limit for a specific account and model.""" |
| 29 | + username = "test_user_ab" |
| 30 | + model_name = "model_x" |
| 31 | + caller = "caller_rpm" |
| 32 | + |
| 33 | + global_limit = UsageLimitDTO( |
| 34 | + scope=LimitScope.GLOBAL.value, limit_type=LimitType.REQUESTS.value, |
| 35 | + max_value=100, interval_unit=TimeInterval.MINUTE.value, interval_value=1 |
| 36 | + ) |
| 37 | + account_model_limit = UsageLimitDTO( |
| 38 | + scope=LimitScope.USER.value, |
| 39 | + username=username, |
| 40 | + model=model_name, |
| 41 | + limit_type=LimitType.REQUESTS.value, |
| 42 | + max_value=3, |
| 43 | + interval_unit=TimeInterval.MINUTE.value, |
| 44 | + interval_value=1 |
| 45 | + ) |
| 46 | + sqlite_backend_for_accounting.insert_usage_limit(account_model_limit) |
| 47 | + sqlite_backend_for_accounting.insert_usage_limit(global_limit) |
| 48 | + accounting_instance.quota_service.refresh_limits_cache() |
| 49 | + |
| 50 | + with freeze_time("2023-01-01 00:00:00", tz_offset=0) as freezer: |
| 51 | + for i in range(3): |
| 52 | + freezer.tick(delta=timedelta(seconds=1)) |
| 53 | + allowed, reason = accounting_instance.check_quota( |
| 54 | + model=model_name, username=username, caller_name=caller, input_tokens=10, completion_tokens=10 |
| 55 | + ) |
| 56 | + assert allowed, f"Request {i+1}/3 for {model_name} by {username} should be allowed. Reason: {reason}" |
| 57 | + accounting_instance.track_usage( |
| 58 | + model=model_name, username=username, caller_name=caller, |
| 59 | + prompt_tokens=10, completion_tokens=10, cost=0.01, timestamp=datetime.now(timezone.utc) |
| 60 | + ) |
| 61 | + |
| 62 | + freezer.tick(delta=timedelta(seconds=1)) |
| 63 | + allowed, message = accounting_instance.check_quota( |
| 64 | + model=model_name, username=username, caller_name=caller, input_tokens=10, completion_tokens=10 |
| 65 | + ) |
| 66 | + assert not allowed, f"4th request for {model_name} by {username} should be denied" |
| 67 | + assert message is not None, "Denial message should not be None" |
| 68 | + assert f"USER (user: {username})" in message |
| 69 | + assert "limit: 3.00 requests per 1 minute" in message |
| 70 | + assert "exceeded. Current usage: 3.00, request: 1.00." in message |
| 71 | + |
| 72 | + allowed_other_user, _ = accounting_instance.check_quota( |
| 73 | + model=model_name, username="other_user_rpm", caller_name=caller, input_tokens=10, completion_tokens=10 |
| 74 | + ) |
| 75 | + assert allowed_other_user, "Request for same model by other_user_rpm should be allowed" |
| 76 | + |
| 77 | + allowed_other_model, _ = accounting_instance.check_quota( |
| 78 | + model="other_model_rpm", username=username, caller_name=caller, input_tokens=10, completion_tokens=10 |
| 79 | + ) |
| 80 | + assert allowed_other_model, f"Request for other_model_rpm by {username} should be allowed" |
| 81 | + |
| 82 | + |
| 83 | +def test_account_model_completion_tokens_per_minute(accounting_instance: LLMAccounting, sqlite_backend_for_accounting: SQLiteBackend): |
| 84 | + """Test completion tokens per minute limit for a specific account and model.""" |
| 85 | + username = "test_user_ef" |
| 86 | + model_name = "model_z" |
| 87 | + caller = "caller_ctpm" |
| 88 | + |
| 89 | + global_limit = UsageLimitDTO( |
| 90 | + scope=LimitScope.GLOBAL.value, limit_type=LimitType.OUTPUT_TOKENS.value, |
| 91 | + max_value=5000, interval_unit=TimeInterval.MINUTE.value, interval_value=1 |
| 92 | + ) |
| 93 | + account_model_limit = UsageLimitDTO( |
| 94 | + scope=LimitScope.USER.value, |
| 95 | + username=username, |
| 96 | + model=model_name, |
| 97 | + limit_type=LimitType.OUTPUT_TOKENS.value, |
| 98 | + max_value=1000, |
| 99 | + interval_unit=TimeInterval.MINUTE.value, |
| 100 | + interval_value=1 |
| 101 | + ) |
| 102 | + sqlite_backend_for_accounting.insert_usage_limit(account_model_limit) |
| 103 | + sqlite_backend_for_accounting.insert_usage_limit(global_limit) |
| 104 | + accounting_instance.quota_service.refresh_limits_cache() |
| 105 | + |
| 106 | + with freeze_time("2023-01-01 00:00:00", tz_offset=0) as freezer: |
| 107 | + freezer.tick(delta=timedelta(seconds=0)) |
| 108 | + allowed, reason = accounting_instance.check_quota( |
| 109 | + model=model_name, username=username, caller_name=caller, input_tokens=10, completion_tokens=500 |
| 110 | + ) |
| 111 | + assert allowed, f"Request 1 (500 tokens) for {model_name} by {username} should be allowed. Reason: {reason}" |
| 112 | + accounting_instance.track_usage( |
| 113 | + model=model_name, username=username, caller_name=caller, |
| 114 | + prompt_tokens=10, completion_tokens=500, cost=0.01, timestamp=datetime.now(timezone.utc) |
| 115 | + ) |
| 116 | + |
| 117 | + freezer.tick(delta=timedelta(seconds=1)) |
| 118 | + allowed, reason = accounting_instance.check_quota( |
| 119 | + model=model_name, username=username, caller_name=caller, input_tokens=10, completion_tokens=500 |
| 120 | + ) |
| 121 | + assert allowed, f"Request 2 (500 tokens) for {model_name} by {username} should be allowed. Reason: {reason}" |
| 122 | + accounting_instance.track_usage( |
| 123 | + model=model_name, username=username, caller_name=caller, |
| 124 | + prompt_tokens=10, completion_tokens=500, cost=0.01, timestamp=datetime.now(timezone.utc) |
| 125 | + ) |
| 126 | + |
| 127 | + freezer.tick(delta=timedelta(seconds=1)) |
| 128 | + allowed, message = accounting_instance.check_quota( |
| 129 | + model=model_name, username=username, caller_name=caller, input_tokens=10, completion_tokens=1 |
| 130 | + ) |
| 131 | + assert not allowed, f"Request 3 (1 token) for {model_name} by {username} should be denied" |
| 132 | + assert message is not None, "Denial message should not be None" |
| 133 | + assert f"USER (user: {username})" in message |
| 134 | + assert f"limit: 1000.00 {LimitType.OUTPUT_TOKENS.value} per 1 minute" in message |
| 135 | + assert "exceeded. Current usage: 1000.00, request: 1.00." in message |
| 136 | + |
| 137 | + allowed_other_user, _ = accounting_instance.check_quota( |
| 138 | + model=model_name, username="other_user_ctpm", caller_name=caller, input_tokens=10, completion_tokens=10 |
| 139 | + ) |
| 140 | + assert allowed_other_user, "Request for same model by other_user_ctpm should be allowed" |
| 141 | + |
| 142 | + allowed_other_model, _ = accounting_instance.check_quota( |
| 143 | + model="other_model_ctpm", username=username, caller_name=caller, input_tokens=10, completion_tokens=10 |
| 144 | + ) |
| 145 | + assert allowed_other_model, f"Request for other_model_ctpm by {username} should be allowed" |
0 commit comments