mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-17 04:34:25 +00:00
* test(integration): move the openai cost tracking cases from cost_tracking_cases.json to CostTrackingTestCase literals Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(integration): move the remaining cost tracking cases, the cost map and the batch/realtime cases from cost_tracking_cases.json to Python literals (#45885) Co-authored-by: kerry <kerry@berri.ai> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: kerry <kerry@berri.ai> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
45 lines
1.9 KiB
Python
45 lines
1.9 KiB
Python
import json
|
|
from collections.abc import Mapping
|
|
from dataclasses import dataclass
|
|
from typing import Final
|
|
|
|
from integration.cost_calculation.cost_tracking_case import CostTrackingTestCase
|
|
from pydantic import JsonValue
|
|
|
|
COVERS: Final = "quota_management.spend_tracking.scripted_wire.logs_cost"
|
|
REQUEST_ID: Final = "$REQUEST_ID"
|
|
MODEL: Final = "$MODEL"
|
|
|
|
|
|
def sse_frames(*events: Mapping[str, JsonValue], done: bool = False) -> tuple[str, ...]:
|
|
frames: Final = tuple(
|
|
f"event: {event['type']}\ndata: {json.dumps(event)}" if "type" in event else f"data: {json.dumps(event)}"
|
|
for event in events
|
|
)
|
|
return (*frames, "data: [DONE]") if done else frames
|
|
|
|
|
|
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
class StreamParityTestCase:
|
|
"""The same request as two CostTrackingTestCases, `plain` answered with JSON and `streamed` answered with SSE,
|
|
that must both bill the one `expected` row."""
|
|
|
|
plain: CostTrackingTestCase
|
|
streamed: CostTrackingTestCase
|
|
|
|
def __post_init__(self) -> None:
|
|
if self.plain.expected != self.streamed.expected:
|
|
raise ValueError(f"{self.id}: plain and streamed cases expect different rows")
|
|
if self.streamed.request.get("stream") is not True or self.plain.request.get("stream") is True:
|
|
raise ValueError(f"{self.id}: streamed case must set stream: true and the plain case must not")
|
|
if (self.plain.model, self.plain.endpoint, self.plain.deployment) != (
|
|
self.streamed.model,
|
|
self.streamed.endpoint,
|
|
self.streamed.deployment,
|
|
):
|
|
raise ValueError(f"{self.id}: plain and streamed cases must share model, endpoint and deployment")
|
|
|
|
@property
|
|
def id(self) -> str:
|
|
"""The plain case's name, or the streamed case's own name when it is a variant beyond a `-stream` twin."""
|
|
return self.plain.name if self.streamed.name == f"{self.plain.name}-stream" else self.streamed.name
|