forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python: support Azure AD auth (microsoft#340)
AAD tokens offer greater authentication security and is used by several products. Add support for Azure Active Directory auth for the `Azure*` backends.
- Loading branch information
Showing
10 changed files
with
482 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
python/tests/unit/ai/open_ai/services/test_azure_chat_completion.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# Copyright (c) Microsoft. All rights reserved. | ||
|
||
from logging import Logger | ||
from unittest.mock import Mock | ||
|
||
from pytest import raises | ||
|
||
from semantic_kernel.ai.open_ai.services.azure_chat_completion import ( | ||
AzureChatCompletion, | ||
) | ||
from semantic_kernel.ai.open_ai.services.open_ai_chat_completion import ( | ||
OpenAIChatCompletion, | ||
) | ||
|
||
|
||
def test_azure_chat_completion_init() -> None: | ||
deployment_name = "test_deployment" | ||
endpoint = "https://test-endpoint.com" | ||
api_key = "test_api_key" | ||
api_version = "2023-03-15-preview" | ||
logger = Logger("test_logger") | ||
|
||
# Test successful initialization | ||
azure_chat_completion = AzureChatCompletion( | ||
deployment_name=deployment_name, | ||
endpoint=endpoint, | ||
api_key=api_key, | ||
api_version=api_version, | ||
logger=logger, | ||
) | ||
|
||
assert azure_chat_completion._endpoint == endpoint | ||
assert azure_chat_completion._api_version == api_version | ||
assert azure_chat_completion._api_type == "azure" | ||
assert isinstance(azure_chat_completion, OpenAIChatCompletion) | ||
|
||
|
||
def test_azure_chat_completion_init_with_empty_deployment_name() -> None: | ||
# deployment_name = "test_deployment" | ||
endpoint = "https://test-endpoint.com" | ||
api_key = "test_api_key" | ||
api_version = "2023-03-15-preview" | ||
logger = Logger("test_logger") | ||
|
||
with raises(ValueError, match="The deployment name cannot be `None` or empty"): | ||
AzureChatCompletion( | ||
deployment_name="", | ||
endpoint=endpoint, | ||
api_key=api_key, | ||
api_version=api_version, | ||
logger=logger, | ||
) | ||
|
||
|
||
def test_azure_chat_completion_init_with_empty_api_key() -> None: | ||
deployment_name = "test_deployment" | ||
endpoint = "https://test-endpoint.com" | ||
# api_key = "test_api_key" | ||
api_version = "2023-03-15-preview" | ||
logger = Logger("test_logger") | ||
|
||
with raises(ValueError, match="The Azure API key cannot be `None` or empty`"): | ||
AzureChatCompletion( | ||
deployment_name=deployment_name, | ||
endpoint=endpoint, | ||
api_key="", | ||
api_version=api_version, | ||
logger=logger, | ||
) | ||
|
||
|
||
def test_azure_chat_completion_init_with_empty_endpoint() -> None: | ||
deployment_name = "test_deployment" | ||
# endpoint = "https://test-endpoint.com" | ||
api_key = "test_api_key" | ||
api_version = "2023-03-15-preview" | ||
logger = Logger("test_logger") | ||
|
||
with raises(ValueError, match="The Azure endpoint cannot be `None` or empty"): | ||
AzureChatCompletion( | ||
deployment_name=deployment_name, | ||
endpoint="", | ||
api_key=api_key, | ||
api_version=api_version, | ||
logger=logger, | ||
) | ||
|
||
|
||
def test_azure_chat_completion_init_with_invalid_endpoint() -> None: | ||
deployment_name = "test_deployment" | ||
endpoint = "http://test-endpoint.com" | ||
api_key = "test_api_key" | ||
api_version = "2023-03-15-preview" | ||
logger = Logger("test_logger") | ||
|
||
with raises(ValueError, match="The Azure endpoint must start with https://"): | ||
AzureChatCompletion( | ||
deployment_name=deployment_name, | ||
endpoint=endpoint, | ||
api_key=api_key, | ||
api_version=api_version, | ||
logger=logger, | ||
) | ||
|
||
|
||
def test_azure_chat_completion_setup_open_ai() -> None: | ||
import sys | ||
|
||
deployment_name = "test_deployment" | ||
endpoint = "https://test-endpoint.com" | ||
api_key = "test_api_key" | ||
api_version = "2023-03-15-preview" | ||
logger = Logger("test_logger") | ||
|
||
azure_chat_completion = AzureChatCompletion( | ||
deployment_name=deployment_name, | ||
endpoint=endpoint, | ||
api_key=api_key, | ||
api_version=api_version, | ||
logger=logger, | ||
) | ||
|
||
mock_openai = Mock() | ||
sys.modules["openai"] = mock_openai | ||
|
||
azure_chat_completion._setup_open_ai() | ||
|
||
assert mock_openai.api_type == "azure" | ||
assert mock_openai.api_key == api_key | ||
assert mock_openai.api_base == endpoint | ||
assert mock_openai.api_version == api_version |
Oops, something went wrong.