fix(llm_cost_calc/google.py): fix google embedding cost calculation

Fixes https://github.com/BerriAI/litellm/issues/4630
This commit is contained in:
Krrish Dholakia 2024-07-11 11:52:18 -07:00
parent db7d417727
commit 3f965df68b
6 changed files with 133 additions and 18 deletions

View file

@ -1275,7 +1275,7 @@ class Logging:
f"Model={self.model}; cost={self.model_call_details['response_cost']}"
)
except litellm.NotFoundError as e:
verbose_logger.error(
verbose_logger.warning(
f"Model={self.model} not found in completion cost map. Setting 'response_cost' to None"
)
self.model_call_details["response_cost"] = None

View file

@ -1,7 +1,7 @@
# What is this?
## Cost calculation for Google AI Studio / Vertex AI models
import traceback
from typing import List, Literal, Optional, Tuple
from typing import List, Literal, Optional, Tuple, Union
import litellm
from litellm import verbose_logger
@ -29,6 +29,32 @@ def _is_above_128k(tokens: float) -> bool:
return False
def cost_router(
model: str,
custom_llm_provider: str,
prompt_tokens: float,
completion_tokens: float,
prompt_characters: float,
completion_characters: float,
call_type: Union[Literal["embedding", "aembedding"], str],
) -> Literal["cost_per_character", "cost_per_token"]:
"""
Route the cost calc to the right place, based on model/call_type/etc.
Returns
- str, the specific google cost calc function it should route to.
"""
if custom_llm_provider == "vertex_ai" and "claude" in model:
return "cost_per_token"
elif custom_llm_provider == "gemini":
return "cost_per_token"
elif custom_llm_provider == "vertex_ai" and (
call_type == "embedding" or call_type == "aembedding"
):
return "cost_per_token"
return "cost_per_character"
def cost_per_character(
model: str,
custom_llm_provider: str,