|
1 | 1 | from dataclasses import dataclass, field
|
2 |
| -from typing import TypeVar |
3 | 2 |
|
4 | 3 | from openai.types.responses.response_usage import InputTokensDetails, OutputTokensDetails
|
5 |
| -from pydantic import BaseModel |
6 |
| - |
7 |
| -T = TypeVar("T", bound=BaseModel) |
8 |
| - |
9 |
| - |
10 |
| -def add_numeric_fields(current: T, other: T) -> T: |
11 |
| - """ |
12 |
| - Add numeric fields from other to current. |
13 |
| - """ |
14 |
| - clone = current.model_copy() |
15 |
| - for key, v1 in current.model_dump().items(): |
16 |
| - v2 = getattr(other, key, 0) |
17 |
| - if isinstance(v1, (int, float)) and isinstance(v2, (int, float)): |
18 |
| - setattr(clone, key, (v1 or 0) + (v2 or 0)) |
19 |
| - return clone |
20 |
| - |
21 |
| - |
22 |
| -def add_input_tokens_details( |
23 |
| - current: InputTokensDetails, other: InputTokensDetails |
24 |
| -) -> InputTokensDetails: |
25 |
| - return add_numeric_fields(current, other) |
26 |
| - |
27 |
| - |
28 |
| -def add_output_tokens_details( |
29 |
| - current: OutputTokensDetails, other: OutputTokensDetails |
30 |
| -) -> OutputTokensDetails: |
31 |
| - return add_numeric_fields(current, other) |
32 | 4 |
|
33 | 5 |
|
34 | 6 | @dataclass
|
@@ -59,9 +31,12 @@ def add(self, other: "Usage") -> None:
|
59 | 31 | self.input_tokens += other.input_tokens if other.input_tokens else 0
|
60 | 32 | self.output_tokens += other.output_tokens if other.output_tokens else 0
|
61 | 33 | self.total_tokens += other.total_tokens if other.total_tokens else 0
|
62 |
| - self.input_tokens_details = add_input_tokens_details( |
63 |
| - self.input_tokens_details, other.input_tokens_details |
| 34 | + self.input_tokens_details = InputTokensDetails( |
| 35 | + cached_tokens=self.input_tokens_details.cached_tokens |
| 36 | + + other.input_tokens_details.cached_tokens |
64 | 37 | )
|
65 |
| - self.output_tokens_details = add_output_tokens_details( |
66 |
| - self.output_tokens_details, other.output_tokens_details |
| 38 | + |
| 39 | + self.output_tokens_details = OutputTokensDetails( |
| 40 | + reasoning_tokens=self.output_tokens_details.reasoning_tokens |
| 41 | + + other.output_tokens_details.reasoning_tokens |
67 | 42 | )
|
0 commit comments