Spaces:
Configuration error
Configuration error
File size: 1,675 Bytes
447ebeb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
"""
This file is used to calculate the cost of the Gemini API.
Handles the context caching for Gemini API.
"""
from typing import TYPE_CHECKING, Tuple
if TYPE_CHECKING:
from litellm.types.utils import ModelInfo, Usage
def cost_per_token(model: str, usage: "Usage") -> Tuple[float, float]:
"""
Calculates the cost per token for a given model, prompt tokens, and completion tokens.
Follows the same logic as Anthropic's cost per token calculation.
"""
from litellm.litellm_core_utils.llm_cost_calc.utils import generic_cost_per_token
return generic_cost_per_token(
model=model, usage=usage, custom_llm_provider="gemini"
)
def cost_per_web_search_request(usage: "Usage", model_info: "ModelInfo") -> float:
"""
Calculates the cost per web search request for a given model, prompt tokens, and completion tokens.
"""
from litellm.types.utils import PromptTokensDetailsWrapper
# cost per web search request
cost_per_web_search_request = 35e-3
number_of_web_search_requests = 0
# Get number of web search requests
if (
usage is not None
and usage.prompt_tokens_details is not None
and isinstance(usage.prompt_tokens_details, PromptTokensDetailsWrapper)
and hasattr(usage.prompt_tokens_details, "web_search_requests")
and usage.prompt_tokens_details.web_search_requests is not None
):
number_of_web_search_requests = usage.prompt_tokens_details.web_search_requests
else:
number_of_web_search_requests = 0
# Calculate total cost
total_cost = cost_per_web_search_request * number_of_web_search_requests
return total_cost
|