Skip to content

Commit 9cd139c

Browse files
committed
Add concat functionality for usage
1 parent 8b1735d commit 9cd139c

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/agentex/lib/utils/completions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Choice,
1010
ToolCall,
1111
Completion,
12+
Usage,
1213
ToolCallRequest,
1314
)
1415

@@ -21,6 +22,8 @@ def _concat_chunks(_a: None, b: Any):
2122
@_concat_chunks.register
2223
def _(a: Completion, b: Completion) -> Completion:
2324
a.choices = [_concat_chunks(*c) for c in zip(a.choices, b.choices, strict=False)]
25+
a.usage = _concat_chunks(a.usage, b.usage)
26+
2427
return a
2528

2629

@@ -35,6 +38,17 @@ def _(a: Choice, b: Choice) -> Choice:
3538
a.finish_reason = a.finish_reason or b.finish_reason
3639
return a
3740

41+
@_concat_chunks.register
42+
def _(a: Usage | None, b: Usage | None) -> Usage | None:
43+
if a is not None and b is not None:
44+
return Usage(
45+
prompt_tokens=a.prompt_tokens + b.prompt_tokens,
46+
completion_tokens=a.completion_tokens + b.completion_tokens,
47+
total_tokens=a.total_tokens + b.total_tokens,
48+
)
49+
else:
50+
return a or b
51+
3852

3953
@_concat_chunks.register
4054
def _(a: Delta, b: Delta) -> Delta:

0 commit comments

Comments
 (0)