mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-08 03:08:45 +00:00
fix(budget): pass messages to token_counter in projected_cost
This commit is contained in:
parent
4b1e24eae9
commit
47bffe38a1
2 changed files with 49 additions and 2 deletions
|
|
@ -100,8 +100,7 @@ class BudgetManager:
|
|||
return self.user_dict[user]
|
||||
|
||||
def projected_cost(self, model: str, messages: list, user: str):
|
||||
text: Final = "".join(message["content"] for message in messages)
|
||||
prompt_tokens: Final = litellm.token_counter(model=model, text=text)
|
||||
prompt_tokens: Final = litellm.token_counter(model=model, messages=messages)
|
||||
prompt_cost, _ = litellm.cost_per_token(model=model, prompt_tokens=prompt_tokens, completion_tokens=0)
|
||||
current_cost: Final = self.user_dict[user].get("current_cost", 0)
|
||||
projected_cost: Final = prompt_cost + current_cost
|
||||
|
|
|
|||
48
tests/test_litellm/test_budget_manager.py
Normal file
48
tests/test_litellm/test_budget_manager.py
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import pytest
|
||||
|
||||
from litellm.budget_manager import BudgetManager
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def manager() -> BudgetManager:
|
||||
bm = BudgetManager(project_name="test", client_type="local")
|
||||
bm.create_budget(total_budget=10, user="u", duration="daily")
|
||||
return bm
|
||||
|
||||
|
||||
def test_projected_cost_string_content(manager: BudgetManager):
|
||||
cost = manager.projected_cost(
|
||||
model="gpt-4o-mini",
|
||||
messages=[{"role": "user", "content": "hello"}],
|
||||
user="u",
|
||||
)
|
||||
assert cost >= 0
|
||||
|
||||
|
||||
def test_projected_cost_vision_content(manager: BudgetManager):
|
||||
cost = manager.projected_cost(
|
||||
model="gpt-4o-mini",
|
||||
messages=[
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{"type": "text", "text": "hi"},
|
||||
{"type": "image_url", "image_url": {"url": "http://x/y.png"}},
|
||||
],
|
||||
}
|
||||
],
|
||||
user="u",
|
||||
)
|
||||
assert cost >= 0
|
||||
|
||||
|
||||
def test_projected_cost_none_and_missing_content(manager: BudgetManager):
|
||||
assert (
|
||||
manager.projected_cost(
|
||||
model="gpt-4o-mini",
|
||||
messages=[{"role": "assistant", "content": None}],
|
||||
user="u",
|
||||
)
|
||||
>= 0
|
||||
)
|
||||
assert manager.projected_cost(model="gpt-4o-mini", messages=[{"role": "user"}], user="u") >= 0
|
||||
Loading…
Add table
Reference in a new issue