mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 11:14:04 +00:00
(test) /key/gen and budgets
This commit is contained in:
parent
bae9cd7439
commit
9c214d67b0
1 changed files with 74 additions and 25 deletions
|
@ -3,10 +3,8 @@
|
||||||
# 2. Make a call with invalid key, expect it to fail
|
# 2. Make a call with invalid key, expect it to fail
|
||||||
# 3. Make a call to a key with invalid model - expect to fail
|
# 3. Make a call to a key with invalid model - expect to fail
|
||||||
# 4. Make a call to a key with valid model - expect to pass
|
# 4. Make a call to a key with valid model - expect to pass
|
||||||
# 5. Make a call with expired key - expect to fail
|
# 5. Make a call with key over budget, expect to fail
|
||||||
# 6. Make a call with unexpired key - expect to pass
|
# 6. Make a streaming chat/completions call with key over budget, expect to fail
|
||||||
# 7. Make a call with key under budget, expect to pass
|
|
||||||
# 8. Make a call with key over budget, expect to fail
|
|
||||||
|
|
||||||
|
|
||||||
# function to call to generate key - async def new_user(data: NewUserRequest):
|
# function to call to generate key - async def new_user(data: NewUserRequest):
|
||||||
|
@ -169,22 +167,8 @@ def test_call_with_valid_model():
|
||||||
pytest.fail(f"An exception occurred - {str(e)}")
|
pytest.fail(f"An exception occurred - {str(e)}")
|
||||||
|
|
||||||
|
|
||||||
# def test_call_with_expired_key():
|
def test_call_with_key_over_budget():
|
||||||
# # 5. Make a call with an expired key - expect to fail
|
# 5. Make a call with a key over budget, expect to fail
|
||||||
# key = new_user(ExpiredKeyRequest())
|
|
||||||
# result = user_auth(ValidRequest(key))
|
|
||||||
# assert result is False
|
|
||||||
|
|
||||||
|
|
||||||
# def test_call_with_unexpired_key():
|
|
||||||
# # 6. Make a call with an unexpired key - expect to pass
|
|
||||||
# key = new_user(UnexpiredKeyRequest())
|
|
||||||
# result = user_auth(ValidRequest(key))
|
|
||||||
# assert result is True
|
|
||||||
|
|
||||||
|
|
||||||
def test_call_with_key_under_budget():
|
|
||||||
# 7. Make a call with a key under budget, expect to pass
|
|
||||||
setattr(litellm.proxy.proxy_server, "custom_db_client", custom_db_client)
|
setattr(litellm.proxy.proxy_server, "custom_db_client", custom_db_client)
|
||||||
setattr(litellm.proxy.proxy_server, "master_key", "sk-1234")
|
setattr(litellm.proxy.proxy_server, "master_key", "sk-1234")
|
||||||
try:
|
try:
|
||||||
|
@ -249,8 +233,73 @@ def test_call_with_key_under_budget():
|
||||||
print(vars(e))
|
print(vars(e))
|
||||||
|
|
||||||
|
|
||||||
# def test_call_with_key_over_budget():
|
def test_call_with_key_over_budget_stream():
|
||||||
# # 8. Make a call with a key over budget, expect to fail
|
# 6. Make a call with a key over budget, expect to fail
|
||||||
# key = new_user(KeyOverBudgetRequest())
|
setattr(litellm.proxy.proxy_server, "custom_db_client", custom_db_client)
|
||||||
# result = user_auth(ValidRequest(key))
|
setattr(litellm.proxy.proxy_server, "master_key", "sk-1234")
|
||||||
# assert result is False
|
from litellm._logging import verbose_proxy_logger
|
||||||
|
import logging
|
||||||
|
|
||||||
|
litellm.set_verbose = True
|
||||||
|
verbose_proxy_logger.setLevel(logging.DEBUG)
|
||||||
|
try:
|
||||||
|
|
||||||
|
async def test():
|
||||||
|
request = NewUserRequest(max_budget=0.00001)
|
||||||
|
key = await new_user(request)
|
||||||
|
print(key)
|
||||||
|
|
||||||
|
generated_key = key.key
|
||||||
|
user_id = key.user_id
|
||||||
|
bearer_token = "Bearer " + generated_key
|
||||||
|
|
||||||
|
request = Request(scope={"type": "http"})
|
||||||
|
request._url = URL(url="/chat/completions")
|
||||||
|
|
||||||
|
# use generated key to auth in
|
||||||
|
result = await user_api_key_auth(request=request, api_key=bearer_token)
|
||||||
|
print("result from user auth with new key", result)
|
||||||
|
|
||||||
|
# update spend using track_cost callback, make 2nd request, it should fail
|
||||||
|
from litellm.proxy.proxy_server import track_cost_callback
|
||||||
|
from litellm import ModelResponse, Choices, Message, Usage
|
||||||
|
|
||||||
|
resp = ModelResponse(
|
||||||
|
id="chatcmpl-e41836bb-bb8b-4df2-8e70-8f3e160155ac",
|
||||||
|
choices=[
|
||||||
|
Choices(
|
||||||
|
finish_reason=None,
|
||||||
|
index=0,
|
||||||
|
message=Message(
|
||||||
|
content=" Sure! Here is a short poem about the sky:\n\nA canvas of blue, a",
|
||||||
|
role="assistant",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
],
|
||||||
|
model="gpt-35-turbo", # azure always has model written like this
|
||||||
|
usage=Usage(prompt_tokens=210, completion_tokens=200, total_tokens=410),
|
||||||
|
)
|
||||||
|
await track_cost_callback(
|
||||||
|
kwargs={
|
||||||
|
"stream": True,
|
||||||
|
"complete_streaming_response": resp,
|
||||||
|
"litellm_params": {
|
||||||
|
"metadata": {
|
||||||
|
"user_api_key": generated_key,
|
||||||
|
"user_api_key_user_id": user_id,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
completion_response=ModelResponse(),
|
||||||
|
)
|
||||||
|
|
||||||
|
# use generated key to auth in
|
||||||
|
result = await user_api_key_auth(request=request, api_key=bearer_token)
|
||||||
|
print("result from user auth with new key", result)
|
||||||
|
pytest.fail(f"This should have failed!. They key crossed it's budget")
|
||||||
|
|
||||||
|
asyncio.run(test())
|
||||||
|
except Exception as e:
|
||||||
|
error_detail = e.detail
|
||||||
|
assert "Authentication Error, ExceededBudget:" in error_detail
|
||||||
|
print(vars(e))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue