Skip to content

Commit

Permalink
Python: Introduced a new condition to yield `StreamingChatMessageCont…
Browse files Browse the repository at this point in the history
…ent` directly when usage data is available.
  • Loading branch information
ymuichiro committed Nov 19, 2024
1 parent 5197f0b commit 2d6af6a
Showing 1 changed file with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from typing import Any, TypeVar
from uuid import uuid4

from semantic_kernel.contents import AuthorRole

if sys.version_info >= (3, 12):
from typing import override # pragma: no cover
else:
Expand Down Expand Up @@ -148,14 +150,30 @@ async def _inner_get_streaming_chat_message_contents(
if not isinstance(response, AsyncStream):
raise ServiceInvalidResponseError("Expected an AsyncStream[ChatCompletionChunk] response.")
async for chunk in response:
if len(chunk.choices) == 0:
if len(chunk.choices) == 0 and chunk.usage is None:
continue

assert isinstance(chunk, ChatCompletionChunk) # nosec
chunk_metadata = self._get_metadata_from_streaming_chat_response(chunk)
yield [
self._create_streaming_chat_message_content(chunk, choice, chunk_metadata) for choice in chunk.choices
]

if chunk.usage is not None:
yield [
StreamingChatMessageContent(
role=AuthorRole.ASSISTANT,
content="",
choice_index=i,
inner_content=chunk,
ai_model_id=settings.ai_model_id,
metadata=chunk_metadata,
)
for i in range(settings.number_of_responses or 1)
]

else:
yield [
self._create_streaming_chat_message_content(chunk, choice, chunk_metadata)
for choice in chunk.choices
]

@classmethod
def from_dict(cls, settings: dict[str, Any]) -> "AzureChatCompletion":
Expand Down

0 comments on commit 2d6af6a

Please sign in to comment.