mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
fix(azure_ai): price reference pixels from the resolved deployment model info
This commit is contained in:
parent
4dc2f3be1a
commit
6db3435299
3 changed files with 44 additions and 12 deletions
|
|
@ -1,5 +1,5 @@
|
|||
from collections.abc import Mapping
|
||||
from typing import Any, Final, cast
|
||||
from typing import Any, Final
|
||||
|
||||
import litellm
|
||||
from litellm.litellm_core_utils.llm_cost_calc.utils import (
|
||||
|
|
@ -10,21 +10,21 @@ from litellm.litellm_core_utils.llm_cost_calc.utils import (
|
|||
from litellm.types.utils import ImageResponse, ModelInfo
|
||||
|
||||
|
||||
def _input_cost_per_pixel(resolved: ModelInfo) -> float:
|
||||
deployment_price: Final = _get_cost_per_unit(resolved, "input_cost_per_pixel", default_value=None)
|
||||
def _pixel_rate(resolved: ModelInfo, cost_key: str) -> float:
|
||||
deployment_price: Final = _get_cost_per_unit(resolved, cost_key, default_value=None)
|
||||
if deployment_price is not None:
|
||||
return deployment_price
|
||||
model_cost_key: Final = resolved.get("key")
|
||||
shared_entry: Final = litellm.model_cost.get(model_cost_key) if model_cost_key is not None else None
|
||||
if shared_entry is None:
|
||||
return 0.0
|
||||
return shared_entry.get("input_cost_per_pixel") or 0.0
|
||||
return shared_entry.get(cost_key) or 0.0
|
||||
|
||||
|
||||
def _reference_cost(model_cost: Mapping[str, object], image_response: ImageResponse) -> float:
|
||||
def _reference_cost(resolved: ModelInfo, image_response: ImageResponse) -> float:
|
||||
pixels: Final = image_response._hidden_params.get("reference_pixels")
|
||||
rate: Final = model_cost.get("input_cost_per_reference_pixel") or 0.0
|
||||
return float(rate) * pixels if isinstance(pixels, int) and isinstance(rate, (int, float)) else 0.0
|
||||
rate: Final = _pixel_rate(resolved, "input_cost_per_reference_pixel")
|
||||
return rate * pixels if isinstance(pixels, int) else 0.0
|
||||
|
||||
|
||||
def cost_calculator(
|
||||
|
|
@ -57,11 +57,8 @@ def cost_calculator(
|
|||
from litellm.cost_calculator import default_image_cost_calculator
|
||||
|
||||
num_images: Final = n if n is not None else len(image_response.data or ())
|
||||
model_cost: Final = cast( # cast-ok: litellm.model_cost is an untyped shared dict
|
||||
Mapping[str, object], litellm.model_cost.get(_model_info.get("key") or model) or {}
|
||||
)
|
||||
output_cost_per_image: Final[float] = _model_info.get("output_cost_per_image") or 0.0
|
||||
input_cost_per_pixel: Final[float] = _input_cost_per_pixel(_model_info)
|
||||
input_cost_per_pixel: Final[float] = _pixel_rate(_model_info, "input_cost_per_pixel")
|
||||
width: Final = optional_params.get("width") if optional_params else None
|
||||
height: Final = optional_params.get("height") if optional_params else None
|
||||
pixel_size: Final = (
|
||||
|
|
@ -82,6 +79,6 @@ def cost_calculator(
|
|||
if input_cost_per_pixel
|
||||
else 0.0
|
||||
)
|
||||
return generated_cost + _reference_cost(model_cost, image_response)
|
||||
return generated_cost + _reference_cost(_model_info, image_response)
|
||||
|
||||
raise ValueError(f"image_response must be of type ImageResponse got type={type(image_response)}")
|
||||
|
|
|
|||
|
|
@ -3702,6 +3702,7 @@ class CustomPricingLiteLLMParams(MirroredPricingParams):
|
|||
output_cost_per_image_1024: float | None = None
|
||||
output_cost_per_image_1536: float | None = None
|
||||
input_cost_per_pixel: float | None = None
|
||||
input_cost_per_reference_pixel: float | None = None
|
||||
output_cost_per_pixel: float | None = None
|
||||
|
||||
# Include all ModelInfoBase fields as optional
|
||||
|
|
|
|||
|
|
@ -302,6 +302,40 @@ def test_flux2_flex_cost_prefers_deployment_input_cost_per_pixel() -> None:
|
|||
assert cost == pytest.approx(2e-07 * 2048 * 1024 * 2)
|
||||
|
||||
|
||||
def test_flux2_flex_reference_cost_prefers_deployment_rate() -> None:
|
||||
response: Final = ImageResponse(
|
||||
data=[ImageObject(b64_json="aW1n")], hidden_params={"reference_pixels": 1048576}, size="1024x1024"
|
||||
)
|
||||
|
||||
cost: Final = CostCalculatorUtils.route_image_generation_cost_calculator(
|
||||
model="FLUX.2-flex",
|
||||
completion_response=response,
|
||||
custom_llm_provider="azure_ai",
|
||||
size="1024x1024",
|
||||
call_type="image_edit",
|
||||
model_info={"input_cost_per_pixel": 2e-07, "input_cost_per_reference_pixel": 3e-07},
|
||||
)
|
||||
|
||||
assert cost == pytest.approx(2e-07 * 1048576 + 3e-07 * 1048576)
|
||||
|
||||
|
||||
def test_unlisted_azure_ai_model_bills_deployment_reference_rate() -> None:
|
||||
response: Final = ImageResponse(
|
||||
data=[ImageObject(b64_json="aW1n")], hidden_params={"reference_pixels": 1048576}, size="1024x1024"
|
||||
)
|
||||
|
||||
cost: Final = CostCalculatorUtils.route_image_generation_cost_calculator(
|
||||
model="unlisted-flux-deployment",
|
||||
completion_response=response,
|
||||
custom_llm_provider="azure_ai",
|
||||
size="1024x1024",
|
||||
call_type="image_edit",
|
||||
model_info={"input_cost_per_pixel": 1e-07, "input_cost_per_reference_pixel": 1e-07},
|
||||
)
|
||||
|
||||
assert cost == pytest.approx(1e-07 * 1048576 * 2)
|
||||
|
||||
|
||||
def test_unlisted_azure_ai_model_bills_deployment_input_cost_per_pixel() -> None:
|
||||
response: Final = ImageResponse(data=[ImageObject(b64_json="aW1n"), ImageObject(b64_json="aW1n")])
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue