From 3bb861ae02a0d85f0cc206a942c99f83e6670c2b Mon Sep 17 00:00:00 2001 From: ishaan-jaff Date: Fri, 1 Mar 2024 07:54:09 -0800 Subject: [PATCH 1/2] (feat) predict spend --- enterprise/utils.py | 69 +++++++++++++++++++ ...odel_prices_and_context_window_backup.json | 30 ++++++++ 2 files changed, 99 insertions(+) diff --git a/enterprise/utils.py b/enterprise/utils.py index 3b5a90fc0ff..8651ec2f309 100644 --- a/enterprise/utils.py +++ b/enterprise/utils.py @@ -245,3 +245,72 @@ def _create_clickhouse_aggregate_tables(client=None, table_names=[]): """ ) return + + +def _forecast_daily_cost(data: list): + import requests + from datetime import datetime, timedelta + + # Get the current date + current_date = datetime.now().date() + + # Get the last entry in the data + last_entry = data[-1] + + # Parse the date from the last entry + last_entry_date = datetime.strptime(last_entry["date"], "%Y-%m-%d").date() + + # Get the month of the last entry + last_entry_month = last_entry_date.month + + # Calculate the last day of the month + last_day_of_month = ( + datetime(last_entry_date.year, last_entry_date.month % 12 + 1, 1) + - timedelta(days=1) + ).day + + # Calculate the remaining days in the month + remaining_days = last_day_of_month - last_entry_date.day + + series = {} + for entry in data: + date = entry["date"] + spend = entry["spend"] + series[date] = spend + + payload = {"series": series, "count": 5} + print("Prediction Data:", payload) + + headers = { + "Content-Type": "application/json", + } + + response = requests.post( + url="https://trend-api-production.up.railway.app/forecast", + json=payload, + headers=headers, + ) + + json_response = response.json() + forecast_data = json_response["forecast"] + + print("Forecast Data:", forecast_data) + + # print(f"Date: {entry['date']}, Spend: {entry['spend']}, Response: {response.text}") + + +_forecast_daily_cost( + [ + {"date": "2022-01-01", "spend": 100}, + {"date": "2022-01-02", "spend": 200}, + {"date": "2022-01-03", "spend": 300}, + {"date": "2022-01-04", "spend": 400}, + {"date": "2022-01-05", "spend": 500}, + {"date": "2022-01-06", "spend": 600}, + {"date": "2022-01-07", "spend": 700}, + {"date": "2022-01-08", "spend": 800}, + {"date": "2022-01-09", "spend": 900}, + {"date": "2022-01-10", "spend": 1000}, + {"date": "2022-01-11", "spend": 50}, + ] +) diff --git a/litellm/model_prices_and_context_window_backup.json b/litellm/model_prices_and_context_window_backup.json index 4870025cb10..66061acc4e9 100644 --- a/litellm/model_prices_and_context_window_backup.json +++ b/litellm/model_prices_and_context_window_backup.json @@ -2070,6 +2070,36 @@ "output_cost_per_token": 0.00000028, "litellm_provider": "perplexity", "mode": "chat" + }, + "perplexity/sonar-small-chat": { + "max_tokens": 16384, + "input_cost_per_token": 0.00000007, + "output_cost_per_token": 0.00000028, + "litellm_provider": "perplexity", + "mode": "chat" + }, + "perplexity/sonar-small-online": { + "max_tokens": 12000, + "input_cost_per_token": 0, + "output_cost_per_token": 0.00000028, + "input_cost_per_request": 0.005, + "litellm_provider": "perplexity", + "mode": "chat" + }, + "perplexity/sonar-medium-chat": { + "max_tokens": 16384, + "input_cost_per_token": 0.0000006, + "output_cost_per_token": 0.0000018, + "litellm_provider": "perplexity", + "mode": "chat" + }, + "perplexity/sonar-medium-online": { + "max_tokens": 12000, + "input_cost_per_token": 0, + "output_cost_per_token": 0.0000018, + "input_cost_per_request": 0.005, + "litellm_provider": "perplexity", + "mode": "chat" }, "anyscale/mistralai/Mistral-7B-Instruct-v0.1": { "max_tokens": 16384, From 47c5b94c504ed465e624ccaf612fe46193ddf04a Mon Sep 17 00:00:00 2001 From: ishaan-jaff Date: Fri, 1 Mar 2024 08:20:35 -0800 Subject: [PATCH 2/2] (feat) /predict/spend endpoint --- enterprise/utils.py | 52 +++++++++++++++++++++-------------- litellm/proxy/proxy_server.py | 12 ++++++++ 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/enterprise/utils.py b/enterprise/utils.py index 8651ec2f309..ba4c8d0a4c4 100644 --- a/enterprise/utils.py +++ b/enterprise/utils.py @@ -251,26 +251,27 @@ def _forecast_daily_cost(data: list): import requests from datetime import datetime, timedelta - # Get the current date - current_date = datetime.now().date() - # Get the last entry in the data last_entry = data[-1] # Parse the date from the last entry last_entry_date = datetime.strptime(last_entry["date"], "%Y-%m-%d").date() + # print("Last Entry Date:", last_entry_date) # Get the month of the last entry last_entry_month = last_entry_date.month + # print("Last Entry Month:", last_entry_month) # Calculate the last day of the month last_day_of_month = ( datetime(last_entry_date.year, last_entry_date.month % 12 + 1, 1) - timedelta(days=1) ).day + # print("Last Day of Month:", last_day_of_month) # Calculate the remaining days in the month remaining_days = last_day_of_month - last_entry_date.day + # print("Remaining Days:", remaining_days) series = {} for entry in data: @@ -278,7 +279,7 @@ def _forecast_daily_cost(data: list): spend = entry["spend"] series[date] = spend - payload = {"series": series, "count": 5} + payload = {"series": series, "count": remaining_days} print("Prediction Data:", payload) headers = { @@ -294,23 +295,34 @@ def _forecast_daily_cost(data: list): json_response = response.json() forecast_data = json_response["forecast"] - print("Forecast Data:", forecast_data) + # print("Forecast Data:", forecast_data) + + response_data = [] + for date in forecast_data: + spend = forecast_data[date] + entry = { + "date": date, + "predicted_spend": spend, + } + response_data.append(entry) + # print("Response Data:", response_data) + return response_data # print(f"Date: {entry['date']}, Spend: {entry['spend']}, Response: {response.text}") -_forecast_daily_cost( - [ - {"date": "2022-01-01", "spend": 100}, - {"date": "2022-01-02", "spend": 200}, - {"date": "2022-01-03", "spend": 300}, - {"date": "2022-01-04", "spend": 400}, - {"date": "2022-01-05", "spend": 500}, - {"date": "2022-01-06", "spend": 600}, - {"date": "2022-01-07", "spend": 700}, - {"date": "2022-01-08", "spend": 800}, - {"date": "2022-01-09", "spend": 900}, - {"date": "2022-01-10", "spend": 1000}, - {"date": "2022-01-11", "spend": 50}, - ] -) +# _forecast_daily_cost( +# [ +# {"date": "2022-01-01", "spend": 100}, +# {"date": "2022-01-02", "spend": 200}, +# {"date": "2022-01-03", "spend": 300}, +# {"date": "2022-01-04", "spend": 400}, +# {"date": "2022-01-05", "spend": 500}, +# {"date": "2022-01-06", "spend": 600}, +# {"date": "2022-01-07", "spend": 700}, +# {"date": "2022-01-08", "spend": 800}, +# {"date": "2022-01-09", "spend": 900}, +# {"date": "2022-01-10", "spend": 1000}, +# {"date": "2022-01-11", "spend": 50}, +# ] +# ) diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 20fbb20ffb6..f1656f770a6 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -4190,6 +4190,18 @@ async def global_spend_models( return response +@router.post( + "/global/predict/spend/logs", + tags=["Budget & Spend Tracking"], + dependencies=[Depends(user_api_key_auth)], +) +async def global_predict_spend_logs(request: Request): + from litellm.proxy.enterprise.utils import _forecast_daily_cost + + data = await request.json() + return _forecast_daily_cost(data) + + @router.get( "/daily_metrics", summary="Get daily spend metrics",