From 2d6af6a7b1fa14a227a688293c4289bce6584a76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=B5=E3=81=81=E3=83=BC?= <47295014+ymuichiro@users.noreply.github.com> Date: Tue, 19 Nov 2024 18:09:52 +0900 Subject: [PATCH] Python: Introduced a new condition to yield `StreamingChatMessageContent` directly when usage data is available. --- .../open_ai/services/azure_chat_completion.py | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/python/semantic_kernel/connectors/ai/open_ai/services/azure_chat_completion.py b/python/semantic_kernel/connectors/ai/open_ai/services/azure_chat_completion.py index bd2a0ca51bab..6152f0eae3ae 100644 --- a/python/semantic_kernel/connectors/ai/open_ai/services/azure_chat_completion.py +++ b/python/semantic_kernel/connectors/ai/open_ai/services/azure_chat_completion.py @@ -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: @@ -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":