From 51639f76312250101372ddfe6788ac506ecfd4bf Mon Sep 17 00:00:00 2001 From: Evan Mattson <35585003+moonbox3@users.noreply.github.com> Date: Thu, 19 Sep 2024 13:55:03 -0400 Subject: [PATCH] Python: Check the Azure OpenAI settings API key for None (#8908) ### Motivation and Context Check the Azure OpenAI settings API key for None not the constructor API KEY param ### Description Need to check the Azure OpenAI Settings `api_key` attribute, not the incoming `api_key` parameter. ### Contribution Checklist - [X] The code builds clean without any errors or warnings - [X] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [X] All unit tests pass, and I have added new tests where possible - [X] I didn't break anyone :smile: --- .github/workflows/python-integration-tests.yml | 4 ---- .../ai/open_ai/services/azure_chat_completion.py | 16 ++++++++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/.github/workflows/python-integration-tests.yml b/.github/workflows/python-integration-tests.yml index 31ccd8db9e55..3b7e34e80df4 100644 --- a/.github/workflows/python-integration-tests.yml +++ b/.github/workflows/python-integration-tests.yml @@ -129,8 +129,6 @@ jobs: AZURE_OPENAI_TEXT_DEPLOYMENT_NAME: ${{ vars.AZURE_OPENAI_TEXT_DEPLOYMENT_NAME }} AZURE_OPENAI_API_VERSION: ${{ vars.AZURE_OPENAI_API_VERSION }} AZURE_OPENAI_ENDPOINT: ${{ secrets.AZURE_OPENAI_ENDPOINT }} - AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }} - AZURE_OPENAI_TOKEN_ENDPOINT: ${{ vars.AZURE_OPENAI_TOKEN_ENDPOINT }} BING_API_KEY: ${{ secrets.BING_API_KEY }} OPENAI_CHAT_MODEL_ID: ${{ vars.OPENAI_CHAT_MODEL_ID }} OPENAI_TEXT_MODEL_ID: ${{ vars.OPENAI_TEXT_MODEL_ID }} @@ -258,8 +256,6 @@ jobs: AZURE_OPENAI_TEXT_DEPLOYMENT_NAME: ${{ vars.AZURE_OPENAI_TEXT_DEPLOYMENT_NAME }} AZURE_OPENAI_API_VERSION: ${{ vars.AZURE_OPENAI_API_VERSION }} AZURE_OPENAI_ENDPOINT: ${{ secrets.AZURE_OPENAI_ENDPOINT }} - AZURE_OPENAI_API_KEY: ${{ secrets.AZURE_OPENAI_API_KEY }} - AZURE_OPENAI_TOKEN_ENDPOINT: ${{ vars.AZURE_OPENAI_TOKEN_ENDPOINT }} BING_API_KEY: ${{ secrets.BING_API_KEY }} OPENAI_CHAT_MODEL_ID: ${{ vars.OPENAI_CHAT_MODEL_ID }} OPENAI_TEXT_MODEL_ID: ${{ vars.OPENAI_TEXT_MODEL_ID }} 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 92dc436c9f86..e9a419247649 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 @@ -94,15 +94,23 @@ def __init__( if not azure_openai_settings.chat_deployment_name: raise ServiceInitializationError("chat_deployment_name is required.") - # If the api_key is none, and the ad_token is none, and the ad_token_provider is none, + # If the async_client is None, the api_key is none, the ad_token is none, and the ad_token_provider is none, # then we will attempt to get the ad_token using the default endpoint specified in the Azure OpenAI settings. - if api_key is None and ad_token_provider is None and azure_openai_settings.token_endpoint and ad_token is None: + if ( + async_client is None + and azure_openai_settings.api_key is None + and ad_token_provider is None + and ad_token is None + and azure_openai_settings.token_endpoint + ): ad_token = azure_openai_settings.get_azure_openai_auth_token( token_endpoint=azure_openai_settings.token_endpoint ) - if not azure_openai_settings.api_key and not ad_token and not ad_token_provider: - raise ServiceInitializationError("Please provide either api_key, ad_token or ad_token_provider") + if not async_client and not azure_openai_settings.api_key and not ad_token and not ad_token_provider: + raise ServiceInitializationError( + "Please provide either a custom client, or an api_key, an ad_token or an ad_token_provider" + ) super().__init__( deployment_name=azure_openai_settings.chat_deployment_name,