Skip to content
Open
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
5 changes: 4 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 Down
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 Down Expand Up @@ -41,3 +43,10 @@ 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_invalid_add() -> None:
"""Test adding unsupported types raises TypeError."""
usage = CompletionUsage(prompt_tokens=10, completion_tokens=20)
with pytest.raises(TypeError):
_ = usage + 5
Loading