mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
refactor(cost): compare providers through LlmProviders instead of string literals
LlmProviders::matches mirrors Python's custom_llm_provider == LlmProviders.X.value so a misspelled provider no longer compiles Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
ec55adffa9
commit
e54ac548ec
7 changed files with 37 additions and 18 deletions
|
|
@ -6,6 +6,7 @@ use crate::error::CostError;
|
|||
use crate::model_selection::{
|
||||
ModelSelectionRequest, get_response_model, select_model_name_for_cost_calc,
|
||||
};
|
||||
use crate::provider::LlmProviders;
|
||||
use crate::responses_usage::ChatUsage;
|
||||
use crate::usage_dispatch::get_usage_object;
|
||||
|
||||
|
|
@ -111,7 +112,7 @@ pub fn prepare_completion_input(
|
|||
infer_call_type(request.call_type, request.response_kind).unwrap_or("completion");
|
||||
let model = if matches!(call_type, "image_generation" | "aimage_generation")
|
||||
&& request.model_selection.model == Some("")
|
||||
&& request.model_selection.provider == Some("azure")
|
||||
&& LlmProviders::AZURE.matches(request.model_selection.provider)
|
||||
{
|
||||
Some("dall-e-2")
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ use crate::image_cost_router::{
|
|||
};
|
||||
use crate::mcp_cost::calculate_mcp_tool_call_cost;
|
||||
use crate::per_second::{DEFAULT_REPLICATE_GPU_PRICE_PER_SECOND, get_replicate_completion_pricing};
|
||||
use crate::provider::LlmProviders;
|
||||
use crate::realtime_cost::{
|
||||
collect_and_combine_usage_from_realtime_stream_results, combine_usage_objects, event_usage,
|
||||
partition_results_by_service_tier,
|
||||
|
|
@ -410,7 +411,7 @@ fn unregistered_replicate_cost(
|
|||
provider: Option<&str>,
|
||||
) -> Option<(String, f64)> {
|
||||
let model = prepared.model_candidates.iter().flatten().next()?;
|
||||
if !(provider == Some("replicate") || model.contains("replicate"))
|
||||
if !(LlmProviders::REPLICATE.matches(provider) || model.contains("replicate"))
|
||||
|| catalog.contains_exact_model(model)
|
||||
{
|
||||
return None;
|
||||
|
|
@ -768,7 +769,7 @@ pub fn completion_cost_from_response(
|
|||
.filter(|model| !model.is_empty())
|
||||
.or_else(|| hidden.get("litellm_model_name").and_then(Value::as_str))
|
||||
});
|
||||
let request_model = if provider == Some("azure_ai") {
|
||||
let request_model = if LlmProviders::AZURE_AI.matches(provider) {
|
||||
hidden_model
|
||||
.filter(|model| is_azure_model_router(model))
|
||||
.or(request.request_model)
|
||||
|
|
@ -790,7 +791,7 @@ pub fn completion_cost_from_response(
|
|||
),
|
||||
|model| {
|
||||
let token_request_model =
|
||||
if provider == Some("azure_ai") && !is_azure_model_router(model) {
|
||||
if LlmProviders::AZURE_AI.matches(provider) && !is_azure_model_router(model) {
|
||||
None
|
||||
} else {
|
||||
request_model
|
||||
|
|
@ -819,10 +820,11 @@ pub fn completion_cost_from_response(
|
|||
cost_per_token_for_call(catalog, cost_request, call)
|
||||
},
|
||||
)?;
|
||||
let router_fee = (!is_search && provider == Some("azure_ai") && !is_azure_model_router(&model))
|
||||
.then(|| azure_ai_router_fee(catalog, &model, request_model, usage.prompt_tokens))
|
||||
.and_then(Result::ok)
|
||||
.flatten();
|
||||
let router_fee =
|
||||
(!is_search && LlmProviders::AZURE_AI.matches(provider) && !is_azure_model_router(&model))
|
||||
.then(|| azure_ai_router_fee(catalog, &model, request_model, usage.prompt_tokens))
|
||||
.and_then(Result::ok)
|
||||
.flatten();
|
||||
let supplied_additional_costs = if is_search {
|
||||
&[]
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -241,7 +241,7 @@ pub fn cost_per_token(
|
|||
None => catalog.select_model_key(request.model, request.provider, request.region),
|
||||
}
|
||||
.or_else(|| {
|
||||
(request.provider == Some("fireworks_ai"))
|
||||
(provider == Some(LlmProviders::FIREWORKS_AI))
|
||||
.then(|| get_base_model_for_pricing(request.model, FireworksThresholds::default()))
|
||||
.and_then(|category| {
|
||||
catalog.select_model_key(category, request.provider, request.region)
|
||||
|
|
@ -354,8 +354,13 @@ pub fn speech_cost(
|
|||
let model_info = catalog
|
||||
.entry(request.model, request.provider, request.region)
|
||||
.ok_or(CostError::ModelNotFound)?;
|
||||
if matches!(request.provider, Some("vertex_ai" | "vertex_ai_beta"))
|
||||
&& let Some(cost) = lyria_generation_cost(model_info)
|
||||
let provider = request
|
||||
.provider
|
||||
.and_then(|provider| provider.parse::<LlmProviders>().ok());
|
||||
if matches!(
|
||||
provider,
|
||||
Some(LlmProviders::VERTEX_AI | LlmProviders::VERTEX_AI_BETA)
|
||||
) && let Some(cost) = lyria_generation_cost(model_info)
|
||||
{
|
||||
return Ok((0.0, cost));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -340,4 +340,8 @@ impl LlmProviders {
|
|||
pub fn as_str(self) -> &'static str {
|
||||
self.into()
|
||||
}
|
||||
|
||||
pub fn matches(self, name: Option<&str>) -> bool {
|
||||
name == Some(self.as_str())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
use serde_json::{Number, Value};
|
||||
|
||||
use crate::provider::LlmProviders;
|
||||
|
||||
fn rate(value: Option<&Value>) -> Option<f64> {
|
||||
match value? {
|
||||
Value::Number(value) => value.as_f64(),
|
||||
|
|
@ -61,7 +63,7 @@ pub fn with_default_cache_read_rate(model_info: &Value) -> Value {
|
|||
}
|
||||
|
||||
pub fn apply_provider_cache_read_default(model_info: &Value, provider: Option<&str>) -> Value {
|
||||
if provider == Some("fireworks_ai") {
|
||||
if LlmProviders::FIREWORKS_AI.matches(provider) {
|
||||
with_default_cache_read_rate(model_info)
|
||||
} else {
|
||||
model_info.clone()
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
use serde_json::Value;
|
||||
|
||||
use crate::provider::LlmProviders;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct DefaultToolRates {
|
||||
pub file_search_per_call: f64,
|
||||
|
|
@ -212,7 +214,7 @@ pub fn get_cost_for_file_search(
|
|||
return 0.0;
|
||||
}
|
||||
let storage = storage_gb.unwrap_or(0.0) * days.unwrap_or(0.0);
|
||||
if provider == Some("azure")
|
||||
if LlmProviders::AZURE.matches(provider)
|
||||
&& let Some(rate) =
|
||||
model_info.and_then(|info| number(info.get("file_search_cost_per_gb_per_day")))
|
||||
{
|
||||
|
|
@ -223,7 +225,7 @@ pub fn get_cost_for_file_search(
|
|||
{
|
||||
return rate;
|
||||
}
|
||||
if provider == Some("azure") {
|
||||
if LlmProviders::AZURE.matches(provider) {
|
||||
return storage * defaults.azure_file_search_per_gb_day;
|
||||
}
|
||||
defaults.file_search_per_call
|
||||
|
|
@ -243,7 +245,9 @@ pub fn get_cost_for_vector_store(
|
|||
let rate = model_info.and_then(|info| number(info.get("vector_store_cost_per_gb_per_day")));
|
||||
match rate {
|
||||
Some(rate) => storage * rate,
|
||||
None if provider == Some("azure") => storage * defaults.azure_vector_store_per_gb_day,
|
||||
None if LlmProviders::AZURE.matches(provider) => {
|
||||
storage * defaults.azure_vector_store_per_gb_day
|
||||
}
|
||||
None => 0.0,
|
||||
}
|
||||
}
|
||||
|
|
@ -255,7 +259,7 @@ pub fn get_cost_for_computer_use(
|
|||
model_info: Option<&Value>,
|
||||
defaults: DefaultToolRates,
|
||||
) -> f64 {
|
||||
if provider != Some("azure") {
|
||||
if !LlmProviders::AZURE.matches(provider) {
|
||||
return 0.0;
|
||||
}
|
||||
let input_rate = model_info
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ use crate::anthropic_cost::{
|
|||
};
|
||||
use crate::gemini_cost::{cost_per_google_maps_grounding_request, cost_per_web_search_request};
|
||||
use crate::groq_cost::cost_per_web_search_request as groq_web_search_cost;
|
||||
use crate::provider::LlmProviders;
|
||||
use crate::responses_usage::ChatUsage;
|
||||
use crate::tool_call_cost_tracking::{
|
||||
DefaultToolRates, ResponseKind, count_web_search_calls, extract_file_search_params,
|
||||
|
|
@ -115,7 +116,7 @@ fn provider_web_search_cost(
|
|||
}
|
||||
|
||||
fn maps_cost(request: BuiltInToolCostRequest<'_>, model_info: Option<&Value>) -> f64 {
|
||||
let supports_maps = matches!(request.provider, Some("gemini"))
|
||||
let supports_maps = LlmProviders::GEMINI.matches(request.provider)
|
||||
|| request
|
||||
.provider
|
||||
.is_some_and(|provider| provider.starts_with("vertex_ai"));
|
||||
|
|
@ -149,7 +150,7 @@ fn file_search_cost(request: BuiltInToolCostRequest<'_>, model_info: Option<&Val
|
|||
}
|
||||
|
||||
fn azure_assistant_cost(request: BuiltInToolCostRequest<'_>, model_info: Option<&Value>) -> f64 {
|
||||
if request.provider != Some("azure") {
|
||||
if !LlmProviders::AZURE.matches(request.provider) {
|
||||
return 0.0;
|
||||
}
|
||||
let vector = request
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue