Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion python/semantic_kernel/connectors/ai/completion_usage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) Microsoft. All rights reserved.

from typing import Any

from openai.types import CompletionUsage as OpenAICompletionUsage
from openai.types.completion_usage import CompletionTokensDetails, PromptTokensDetails
Expand Down Expand Up @@ -29,8 +30,10 @@ def from_openai(cls, openai_completion_usage: OpenAICompletionUsage):
else None,
)

def __add__(self, other: "CompletionUsage") -> "CompletionUsage":
def __add__(self, other: Any) -> "CompletionUsage":
"""Combine two CompletionUsage instances by summing their token counts."""
if not isinstance(other, CompletionUsage):
return NotImplemented

def _merge_details(cls, a, b):
"""Merge two details objects by summing their fields."""
Expand All @@ -54,3 +57,16 @@ def _merge_details(cls, a, b):
CompletionTokensDetails, self.completion_tokens_details, other.completion_tokens_details
),
)

def __radd__(self, other: Any) -> "CompletionUsage":
"""Support right-addition, enabling sum() operations."""
if other == 0:
return self
if isinstance(other, CompletionUsage):
return other.__add__(self)
return NotImplemented

@property
def total_tokens(self) -> int:
"""Total number of tokens used (prompt_tokens + completion_tokens)."""
return (self.prompt_tokens or 0) + (self.completion_tokens or 0)
24 changes: 24 additions & 0 deletions python/tests/unit/connectors/ai/test_completion_token_usage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Copyright (c) Microsoft. All rights reserved.

import pytest

from semantic_kernel.connectors.ai.completion_usage import CompletionUsage


Expand All @@ -11,6 +13,7 @@ def test_completion_usage() -> None:
# Check that the prompt tokens and completion tokens are set correctly
assert usage.prompt_tokens == 10
assert usage.completion_tokens == 20
assert usage.total_tokens == 30

# Create another CompletionUsage object
other_usage = CompletionUsage(prompt_tokens=5, completion_tokens=15)
Expand All @@ -21,6 +24,7 @@ def test_completion_usage() -> None:
# Check that the total prompt tokens and completion tokens are correct
assert total_usage.prompt_tokens == 15
assert total_usage.completion_tokens == 35
assert total_usage.total_tokens == 50


def test_completion_usage_empty() -> None:
Expand All @@ -31,6 +35,7 @@ def test_completion_usage_empty() -> None:
# Check that the prompt tokens and completion tokens are None
assert usage.prompt_tokens is None
assert usage.completion_tokens is None
assert usage.total_tokens == 0

# Create another CompletionUsage object with empty values
other_usage = CompletionUsage(prompt_tokens=5, completion_tokens=None)
Expand All @@ -41,3 +46,22 @@ def test_completion_usage_empty() -> None:
# Check that the total prompt tokens and completion tokens are None
assert total_usage.prompt_tokens == 5
assert total_usage.completion_tokens == 0


def test_completion_usage_sum() -> None:
"""Test summing a sequence of CompletionUsage instances with sum()."""
u1 = CompletionUsage(prompt_tokens=10, completion_tokens=20)
u2 = CompletionUsage(prompt_tokens=5, completion_tokens=15)
u3 = CompletionUsage(prompt_tokens=2, completion_tokens=3)

total = sum([u1, u2, u3])
assert total.prompt_tokens == 17
assert total.completion_tokens == 38
assert total.total_tokens == 55


def test_completion_usage_invalid_add() -> None:
"""Test adding unsupported types raises TypeError."""
usage = CompletionUsage(prompt_tokens=10, completion_tokens=20)
with pytest.raises(TypeError):
_ = usage + 5
Loading