From 9d0ce1aefdc676cf5625adea28eb986e86dd7061 Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Wed, 27 Mar 2024 17:08:55 -0700 Subject: [PATCH 1/2] feat(proxy_server.py): new `/spend/calculate` endpoint Allows user to calculate spend before making the call --- litellm/proxy/proxy_server.py | 77 +++++++++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 3 deletions(-) diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 8fa2862f2..4b00e9bd7 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -4343,7 +4343,7 @@ async def info_key_fn( @router.get( "/spend/keys", - tags=["budget & spend Tracking"], + tags=["Budget & Spend Tracking"], dependencies=[Depends(user_api_key_auth)], ) async def spend_key_fn(): @@ -4375,7 +4375,7 @@ async def spend_key_fn(): @router.get( "/spend/users", - tags=["budget & spend Tracking"], + tags=["Budget & Spend Tracking"], dependencies=[Depends(user_api_key_auth)], ) async def spend_user_fn( @@ -4427,7 +4427,7 @@ async def spend_user_fn( @router.get( "/spend/tags", - tags=["budget & spend Tracking"], + tags=["Budget & Spend Tracking"], dependencies=[Depends(user_api_key_auth)], responses={ 200: {"model": List[LiteLLM_SpendLogs]}, @@ -4500,6 +4500,77 @@ async def view_spend_tags( ) +@router.post( + "/spend/calculate", + tags=["Budget & Spend Tracking"], + dependencies=[Depends(user_api_key_auth)], + responses={ + 200: { + "cost": { + "description": "The calculated cost", + "example": 0.0, + "type": "float", + } + } + }, +) +async def calculate_spend(request: Request): + """ + Accepts all the params of completion_cost. + + Calculate spend **before** making call: + + ``` + curl --location 'http://localhost:4000/spend/calculate' + --header 'Authorization: Bearer sk-1234' + --header 'Content-Type: application/json' + --data '{ + "model": "anthropic.claude-v2", + "messages": [{"role": "user", "content": "Hey, how'''s it going?"}] + }' + ``` + + Calculate spend **after** making call: + + ``` + curl --location 'http://localhost:4000/spend/calculate' + --header 'Authorization: Bearer sk-1234' + --header 'Content-Type: application/json' + --data '{ + "completion_response": { + "id": "chatcmpl-123", + "object": "chat.completion", + "created": 1677652288, + "model": "gpt-3.5-turbo-0125", + "system_fingerprint": "fp_44709d6fcb", + "choices": [{ + "index": 0, + "message": { + "role": "assistant", + "content": "Hello there, how may I assist you today?" + }, + "logprobs": null, + "finish_reason": "stop" + }] + "usage": { + "prompt_tokens": 9, + "completion_tokens": 12, + "total_tokens": 21 + } + } + }' + ``` + """ + from litellm import completion_cost + + data = await request.json() + if "completion_response" in data: + data["completion_response"] = litellm.ModelResponse( + **data["completion_response"] + ) + return {"cost": completion_cost(**data)} + + @router.get( "/spend/logs", tags=["Budget & Spend Tracking"], From 848b0ba67fcb2e41fdce9197886239a19eb76a66 Mon Sep 17 00:00:00 2001 From: Krish Dholakia Date: Wed, 27 Mar 2024 19:23:33 -0700 Subject: [PATCH 2/2] Updated config.yml --- .circleci/config.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index b8084f1df..3084c6a88 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -48,6 +48,7 @@ jobs: pip install argon2-cffi pip install "pytest-mock==3.12.0" pip install python-multipart + pip install google-cloud-aiplatform - save_cache: paths: - ./venv