forked from phoenix/litellm-mirror
fix(proxy/utils.py): fix reset monthly budget
fix to reset at the same time each month (not at start of month)
This commit is contained in:
parent
056913fd70
commit
d8acda9f39
3 changed files with 60 additions and 14 deletions
|
@ -46,7 +46,7 @@ Possible values for `budget_duration`
|
||||||
| `budget_duration="1m"` | every 1 min |
|
| `budget_duration="1m"` | every 1 min |
|
||||||
| `budget_duration="1h"` | every 1 hour |
|
| `budget_duration="1h"` | every 1 hour |
|
||||||
| `budget_duration="1d"` | every 1 day |
|
| `budget_duration="1d"` | every 1 day |
|
||||||
| `budget_duration="1mo"` | start of every month |
|
| `budget_duration="1mo"` | every 1 month |
|
||||||
|
|
||||||
|
|
||||||
### 2. Create a key for the `team`
|
### 2. Create a key for the `team`
|
||||||
|
|
|
@ -2115,6 +2115,16 @@ def _extract_from_regex(duration: str) -> Tuple[int, str]:
|
||||||
return value, unit
|
return value, unit
|
||||||
|
|
||||||
|
|
||||||
|
def get_last_day_of_month(year, month):
|
||||||
|
# Handle December case
|
||||||
|
if month == 12:
|
||||||
|
return 31
|
||||||
|
# Next month is January, so subtract a day from March 1st
|
||||||
|
next_month = datetime(year=year, month=month + 1, day=1)
|
||||||
|
last_day_of_month = (next_month - timedelta(days=1)).day
|
||||||
|
return last_day_of_month
|
||||||
|
|
||||||
|
|
||||||
def _duration_in_seconds(duration: str) -> int:
|
def _duration_in_seconds(duration: str) -> int:
|
||||||
"""
|
"""
|
||||||
Parameters:
|
Parameters:
|
||||||
|
@ -2141,13 +2151,29 @@ def _duration_in_seconds(duration: str) -> int:
|
||||||
now = time.time()
|
now = time.time()
|
||||||
current_time = datetime.fromtimestamp(now)
|
current_time = datetime.fromtimestamp(now)
|
||||||
|
|
||||||
# Calculate the first day of the next month
|
|
||||||
if current_time.month == 12:
|
if current_time.month == 12:
|
||||||
next_month = datetime(year=current_time.year + 1, month=1, day=1)
|
target_year = current_time.year + 1
|
||||||
|
target_month = 1
|
||||||
else:
|
else:
|
||||||
next_month = datetime(
|
target_year = current_time.year
|
||||||
year=current_time.year, month=current_time.month + value, day=1
|
target_month = current_time.month + value
|
||||||
)
|
|
||||||
|
# Determine the day to set for next month
|
||||||
|
target_day = current_time.day
|
||||||
|
last_day_of_target_month = get_last_day_of_month(target_year, target_month)
|
||||||
|
|
||||||
|
if target_day > last_day_of_target_month:
|
||||||
|
target_day = last_day_of_target_month
|
||||||
|
|
||||||
|
next_month = datetime(
|
||||||
|
year=target_year,
|
||||||
|
month=target_month,
|
||||||
|
day=target_day,
|
||||||
|
hour=current_time.hour,
|
||||||
|
minute=current_time.minute,
|
||||||
|
second=current_time.second,
|
||||||
|
microsecond=current_time.microsecond,
|
||||||
|
)
|
||||||
|
|
||||||
# Calculate the duration until the first day of the next month
|
# Calculate the duration until the first day of the next month
|
||||||
duration_until_next_month = next_month - current_time
|
duration_until_next_month = next_month - current_time
|
||||||
|
|
|
@ -26,7 +26,11 @@ from litellm.utils import (
|
||||||
get_max_tokens,
|
get_max_tokens,
|
||||||
get_supported_openai_params,
|
get_supported_openai_params,
|
||||||
)
|
)
|
||||||
from litellm.proxy.utils import _duration_in_seconds, _extract_from_regex
|
from litellm.proxy.utils import (
|
||||||
|
_duration_in_seconds,
|
||||||
|
_extract_from_regex,
|
||||||
|
get_last_day_of_month,
|
||||||
|
)
|
||||||
|
|
||||||
# Assuming your trim_messages, shorten_message_to_fit_limit, and get_token_count functions are all in a module named 'message_utils'
|
# Assuming your trim_messages, shorten_message_to_fit_limit, and get_token_count functions are all in a module named 'message_utils'
|
||||||
|
|
||||||
|
@ -467,15 +471,31 @@ def test_duration_in_seconds():
|
||||||
|
|
||||||
now = time.time()
|
now = time.time()
|
||||||
current_time = datetime.fromtimestamp(now)
|
current_time = datetime.fromtimestamp(now)
|
||||||
print("current_time={}".format(current_time))
|
|
||||||
# Calculate the first day of the next month
|
|
||||||
if current_time.month == 12:
|
if current_time.month == 12:
|
||||||
next_month = datetime(year=current_time.year + 1, month=1, day=1)
|
target_year = current_time.year + 1
|
||||||
|
target_month = 1
|
||||||
else:
|
else:
|
||||||
next_month = datetime(
|
target_year = current_time.year
|
||||||
year=current_time.year, month=current_time.month + 1, day=1
|
target_month = current_time.month + 1
|
||||||
)
|
|
||||||
print("next_month={}".format(next_month))
|
# Determine the day to set for next month
|
||||||
|
target_day = current_time.day
|
||||||
|
last_day_of_target_month = get_last_day_of_month(target_year, target_month)
|
||||||
|
|
||||||
|
if target_day > last_day_of_target_month:
|
||||||
|
target_day = last_day_of_target_month
|
||||||
|
|
||||||
|
next_month = datetime(
|
||||||
|
year=target_year,
|
||||||
|
month=target_month,
|
||||||
|
day=target_day,
|
||||||
|
hour=current_time.hour,
|
||||||
|
minute=current_time.minute,
|
||||||
|
second=current_time.second,
|
||||||
|
microsecond=current_time.microsecond,
|
||||||
|
)
|
||||||
|
|
||||||
# Calculate the duration until the first day of the next month
|
# Calculate the duration until the first day of the next month
|
||||||
duration_until_next_month = next_month - current_time
|
duration_until_next_month = next_month - current_time
|
||||||
expected_duration = int(duration_until_next_month.total_seconds())
|
expected_duration = int(duration_until_next_month.total_seconds())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue