From 076c792be15da0172ef823ea69ca8be8d3b275f1 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: Thu, 21 Nov 2024 08:40:12 +0900 Subject: [PATCH 1/2] Python: In Azure OpenAI, stream_options is now enabled, so the override of _inner_get_streaming_chat_message_contents has been removed. --- .../open_ai/services/azure_chat_completion.py | 51 ++----------------- 1 file changed, 5 insertions(+), 46 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..18f03c5ac6e9 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 @@ -3,17 +3,17 @@ import json import logging import sys -from collections.abc import AsyncGenerator, Mapping +from collections.abc import Mapping from copy import deepcopy from typing import Any, TypeVar from uuid import uuid4 if sys.version_info >= (3, 12): - from typing import override # pragma: no cover + pass # pragma: no cover else: - from typing_extensions import override # pragma: no cover + pass # pragma: no cover -from openai import AsyncAzureOpenAI, AsyncStream +from openai import AsyncAzureOpenAI from openai.lib.azure import AsyncAzureADTokenProvider from openai.types.chat.chat_completion import ChatCompletion, Choice from openai.types.chat.chat_completion_chunk import ChatCompletionChunk @@ -23,24 +23,19 @@ from semantic_kernel.connectors.ai.open_ai.prompt_execution_settings.azure_chat_prompt_execution_settings import ( AzureChatPromptExecutionSettings, ) -from semantic_kernel.connectors.ai.open_ai.prompt_execution_settings.open_ai_prompt_execution_settings import ( - OpenAIChatPromptExecutionSettings, -) from semantic_kernel.connectors.ai.open_ai.services.azure_config_base import AzureOpenAIConfigBase from semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion_base import OpenAIChatCompletionBase from semantic_kernel.connectors.ai.open_ai.services.open_ai_handler import OpenAIModelTypes from semantic_kernel.connectors.ai.open_ai.services.open_ai_text_completion_base import OpenAITextCompletionBase from semantic_kernel.connectors.ai.open_ai.settings.azure_open_ai_settings import AzureOpenAISettings from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings -from semantic_kernel.contents.chat_history import ChatHistory from semantic_kernel.contents.chat_message_content import ChatMessageContent from semantic_kernel.contents.function_call_content import FunctionCallContent from semantic_kernel.contents.function_result_content import FunctionResultContent from semantic_kernel.contents.streaming_chat_message_content import StreamingChatMessageContent from semantic_kernel.contents.text_content import TextContent from semantic_kernel.contents.utils.finish_reason import FinishReason -from semantic_kernel.exceptions.service_exceptions import ServiceInitializationError, ServiceInvalidResponseError -from semantic_kernel.utils.telemetry.model_diagnostics.decorators import trace_streaming_chat_completion +from semantic_kernel.exceptions.service_exceptions import ServiceInitializationError logger: logging.Logger = logging.getLogger(__name__) @@ -121,42 +116,6 @@ def __init__( client=async_client, ) - @override - @trace_streaming_chat_completion(OpenAIChatCompletionBase.MODEL_PROVIDER_NAME) - async def _inner_get_streaming_chat_message_contents( - self, - chat_history: "ChatHistory", - settings: "PromptExecutionSettings", - ) -> AsyncGenerator[list["StreamingChatMessageContent"], Any]: - """Override the base method. - - This is because the latest Azure OpenAI API GA version doesn't support `stream_option` - yet and it will potentially result in errors if the option is included. - This method will be called instead of the base method. - TODO: Remove this method when the `stream_option` is supported by the Azure OpenAI API. - GitHub Issue: https://github.com/microsoft/semantic-kernel/issues/8996 - """ - if not isinstance(settings, OpenAIChatPromptExecutionSettings): - settings = self.get_prompt_execution_settings_from_settings(settings) - assert isinstance(settings, OpenAIChatPromptExecutionSettings) # nosec - - settings.stream = True - settings.messages = self._prepare_chat_history_for_request(chat_history) - settings.ai_model_id = settings.ai_model_id or self.ai_model_id - - response = await self._send_request(settings) - if not isinstance(response, AsyncStream): - raise ServiceInvalidResponseError("Expected an AsyncStream[ChatCompletionChunk] response.") - async for chunk in response: - if len(chunk.choices) == 0: - 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 - ] - @classmethod def from_dict(cls, settings: dict[str, Any]) -> "AzureChatCompletion": """Initialize an Azure OpenAI service from a dictionary of settings. From 40587bede84303a90c01943b62986719323c0ce5 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: Fri, 22 Nov 2024 09:53:16 +0900 Subject: [PATCH 2/2] Python: Removed unnecessary imports from azure_chat_completion.py --- .../connectors/ai/open_ai/services/azure_chat_completion.py | 6 ------ 1 file changed, 6 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 18f03c5ac6e9..73e1a8fe62b7 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 @@ -2,17 +2,11 @@ import json import logging -import sys from collections.abc import Mapping from copy import deepcopy from typing import Any, TypeVar from uuid import uuid4 -if sys.version_info >= (3, 12): - pass # pragma: no cover -else: - pass # pragma: no cover - from openai import AsyncAzureOpenAI from openai.lib.azure import AsyncAzureADTokenProvider from openai.types.chat.chat_completion import ChatCompletion, Choice