|
| 1 | +from pydantic import BaseModel |
| 2 | + |
1 | 3 | import dspy |
2 | 4 | from dspy.utils.usage_tracker import UsageTracker, track_usage |
3 | 5 |
|
@@ -221,3 +223,105 @@ def test_merge_usage_entries_with_none_values(): |
221 | 223 | assert total_usage["gpt-4o-mini"]["completion_tokens_details"]["audio_tokens"] == 1 |
222 | 224 | assert total_usage["gpt-4o-mini"]["completion_tokens_details"]["accepted_prediction_tokens"] == 1 |
223 | 225 | assert total_usage["gpt-4o-mini"]["completion_tokens_details"]["rejected_prediction_tokens"] == 1 |
| 226 | + |
| 227 | + |
| 228 | +def test_merge_usage_entries_with_pydantic_models(): |
| 229 | + """Test merging usage entries with Pydantic model objects, like `PromptTokensDetailsWrapper` from litellm.""" |
| 230 | + tracker = UsageTracker() |
| 231 | + |
| 232 | + # Here we define a simplified version of the Pydantic models from litellm to avoid the dependency change on litellm. |
| 233 | + class CacheCreationTokenDetails(BaseModel): |
| 234 | + ephemeral_5m_input_tokens: int |
| 235 | + ephemeral_1h_input_tokens: int |
| 236 | + |
| 237 | + class PromptTokensDetailsWrapper(BaseModel): |
| 238 | + audio_tokens: int | None |
| 239 | + cached_tokens: int |
| 240 | + text_tokens: int | None |
| 241 | + image_tokens: int | None |
| 242 | + cache_creation_tokens: int |
| 243 | + cache_creation_token_details: CacheCreationTokenDetails |
| 244 | + |
| 245 | + # Add usage entries for different models |
| 246 | + usage_entries = [ |
| 247 | + { |
| 248 | + "model": "gpt-4o-mini", |
| 249 | + "usage": { |
| 250 | + "prompt_tokens": 1117, |
| 251 | + "completion_tokens": 46, |
| 252 | + "total_tokens": 1163, |
| 253 | + "prompt_tokens_details": PromptTokensDetailsWrapper( |
| 254 | + audio_tokens=None, |
| 255 | + cached_tokens=3, |
| 256 | + text_tokens=None, |
| 257 | + image_tokens=None, |
| 258 | + cache_creation_tokens=0, |
| 259 | + cache_creation_token_details=CacheCreationTokenDetails( |
| 260 | + ephemeral_5m_input_tokens=5, ephemeral_1h_input_tokens=0 |
| 261 | + ), |
| 262 | + ), |
| 263 | + "completion_tokens_details": {}, |
| 264 | + }, |
| 265 | + }, |
| 266 | + { |
| 267 | + "model": "gpt-4o-mini", |
| 268 | + "usage": { |
| 269 | + "prompt_tokens": 800, |
| 270 | + "completion_tokens": 100, |
| 271 | + "total_tokens": 900, |
| 272 | + "prompt_tokens_details": PromptTokensDetailsWrapper( |
| 273 | + audio_tokens=None, |
| 274 | + cached_tokens=3, |
| 275 | + text_tokens=None, |
| 276 | + image_tokens=None, |
| 277 | + cache_creation_tokens=0, |
| 278 | + cache_creation_token_details=CacheCreationTokenDetails( |
| 279 | + ephemeral_5m_input_tokens=5, ephemeral_1h_input_tokens=0 |
| 280 | + ), |
| 281 | + ), |
| 282 | + "completion_tokens_details": None, |
| 283 | + }, |
| 284 | + }, |
| 285 | + { |
| 286 | + "model": "gpt-4o-mini", |
| 287 | + "usage": { |
| 288 | + "prompt_tokens": 800, |
| 289 | + "completion_tokens": 100, |
| 290 | + "total_tokens": 900, |
| 291 | + "prompt_tokens_details": PromptTokensDetailsWrapper( |
| 292 | + audio_tokens=None, |
| 293 | + cached_tokens=3, |
| 294 | + text_tokens=None, |
| 295 | + image_tokens=None, |
| 296 | + cache_creation_tokens=0, |
| 297 | + cache_creation_token_details=CacheCreationTokenDetails( |
| 298 | + ephemeral_5m_input_tokens=5, ephemeral_1h_input_tokens=0 |
| 299 | + ), |
| 300 | + ), |
| 301 | + "completion_tokens_details": { |
| 302 | + "reasoning_tokens": 1, |
| 303 | + "audio_tokens": 1, |
| 304 | + "accepted_prediction_tokens": 1, |
| 305 | + "rejected_prediction_tokens": 1, |
| 306 | + }, |
| 307 | + }, |
| 308 | + }, |
| 309 | + ] |
| 310 | + |
| 311 | + for entry in usage_entries: |
| 312 | + tracker.add_usage(entry["model"], entry["usage"]) |
| 313 | + |
| 314 | + total_usage = tracker.get_total_tokens() |
| 315 | + |
| 316 | + assert total_usage["gpt-4o-mini"]["prompt_tokens"] == 2717 |
| 317 | + assert total_usage["gpt-4o-mini"]["completion_tokens"] == 246 |
| 318 | + assert total_usage["gpt-4o-mini"]["total_tokens"] == 2963 |
| 319 | + assert total_usage["gpt-4o-mini"]["prompt_tokens_details"]["cached_tokens"] == 9 |
| 320 | + assert ( |
| 321 | + total_usage["gpt-4o-mini"]["prompt_tokens_details"]["cache_creation_token_details"]["ephemeral_5m_input_tokens"] |
| 322 | + == 15 |
| 323 | + ) |
| 324 | + assert total_usage["gpt-4o-mini"]["completion_tokens_details"]["reasoning_tokens"] == 1 |
| 325 | + assert total_usage["gpt-4o-mini"]["completion_tokens_details"]["audio_tokens"] == 1 |
| 326 | + assert total_usage["gpt-4o-mini"]["completion_tokens_details"]["accepted_prediction_tokens"] == 1 |
| 327 | + assert total_usage["gpt-4o-mini"]["completion_tokens_details"]["rejected_prediction_tokens"] == 1 |
0 commit comments