mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
fix(cost): read batch rates with Python's _batch_rate conversion
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a6644925f3
commit
a57afedff3
2 changed files with 25 additions and 11 deletions
|
|
@ -6,6 +6,7 @@ use crate::generic_usage::{get_billable_input_tokens, parse_prompt_tokens_detail
|
|||
use crate::pricing::Rate;
|
||||
use crate::regional_uplift::get_regional_uplift_multiplier;
|
||||
use crate::responses_usage::ChatUsage;
|
||||
use crate::wire::py_float;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum ThresholdPolicy {
|
||||
|
|
@ -209,17 +210,10 @@ pub fn batch_cost_calculator(
|
|||
}
|
||||
|
||||
fn model_rate(model_info: &Value, key: &str) -> Rate {
|
||||
let Some(value) = model_info.get(key) else {
|
||||
return Rate::Missing;
|
||||
};
|
||||
match value {
|
||||
Value::Number(value) => value.as_f64().map_or(Rate::Missing, Rate::Value),
|
||||
Value::String(value) => value
|
||||
.trim()
|
||||
.parse::<f64>()
|
||||
.map_or(Rate::Missing, Rate::Value),
|
||||
_ => Rate::Missing,
|
||||
}
|
||||
model_info
|
||||
.get(key)
|
||||
.and_then(py_float)
|
||||
.map_or(Rate::Missing, Rate::Value)
|
||||
}
|
||||
|
||||
fn threshold(value: &str) -> Option<u64> {
|
||||
|
|
|
|||
|
|
@ -829,3 +829,23 @@ fn default_image_cost_calculator_strips_every_provider_prefix_like_python_replac
|
|||
.unwrap();
|
||||
assert!((cost - 0.5).abs() < 1e-12);
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[case::boolean_rate(json!(true), 100.0)]
|
||||
#[case::padded_string_rate(json!(" 0.5 "), 50.0)]
|
||||
#[case::unparseable_string_is_unset(json!("n/a"), 0.0)]
|
||||
fn batch_rates_convert_like_python_batch_rate(
|
||||
#[case] rate: serde_json::Value,
|
||||
#[case] expected: f64,
|
||||
) {
|
||||
let model_info =
|
||||
json!({"input_cost_per_token_batches": rate, "output_cost_per_token_batches": 0.0});
|
||||
let usage = ChatUsage {
|
||||
prompt_tokens: 100,
|
||||
completion_tokens: 0,
|
||||
total_tokens: 100,
|
||||
..ChatUsage::default()
|
||||
};
|
||||
let cost = batch_cost_from_model_info(&model_info, &usage, Some("openai"), None).unwrap();
|
||||
assert!((cost.prompt - expected).abs() < 1e-12);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue