A programming framework for agentic AI
Add Azure Active Directory auth for Python samples (#315)
Eric Zhu1c95443db56d
diff --git a/python/pyproject.toml b/python/pyproject.tomlindex 7a99641ad..92ba49de7 100644--- a/python/pyproject.toml+++ b/python/pyproject.toml@@ -28,6 +28,7 @@ dependencies = [[tool.hatch.envs.default]installer = "uv"dependencies = [+ "azure-identity","aiofiles","chess","colorama",diff --git a/python/samples/README.md b/python/samples/README.mdindex 6f89a600a..0badca53d 100644--- a/python/samples/README.md+++ b/python/samples/README.md@@ -62,32 +62,39 @@ We provide examples on how to integrate other agents with the platform:### Prerequisites-First, you need a shell with AGNext and the examples dependencies installed.-To do this, in the example directory, run:+First, you need a shell with AGNext and required dependencies installed.+To do this, in the samples directory, run:```bashhatch shell```-Then, you need to set the `OPENAI_API_KEY` environment variable to your OpenAI API key.+### Using Azure OpenAI API++For Azure OpenAI API, you need to set the following environment variables:```bash-export OPENAI_API_KEY=your_openai_api_key+export OPENAI_API_TYPE=azure+export AZURE_OPENAI_ENDPOINT=your_azure_openai_endpoint+export AZURE_OPENAI_API_VERSION=your_azure_openai_api_version```-For Azure OpenAI API, you need to set the following environment variables:+By default, we use Azure Active Directory (AAD) for authentication.+You need to run `az login` first to authenticate with Azure.+You can also+use API key authentication by setting the following environment variables:```bashexport AZURE_OPENAI_API_KEY=your_azure_openai_api_key-export AZURE_OPENAI_ENDPOINT=your_azure_openai_endpoint```-By default, OpenAI API is used.-To use Azure OpenAI API, set the `OPENAI_API_TYPE`-environment variable to `azure`.+### Using OpenAI API++For OpenAI API, you need to set the following environment variables.```bash-export OPENAI_API_TYPE=azure+export OPENAI_API_TYPE=openai+export OPENAI_API_KEY=your_openai_api_key```### Runningdiff --git a/python/samples/common/utils.py b/python/samples/common/utils.pyindex 9a7caf199..c91b18b55 100644--- a/python/samples/common/utils.py+++ b/python/samples/common/utils.py@@ -11,6 +11,7 @@ from agnext.components.models import (OpenAIChatCompletionClient,UserMessage,)+from azure.identity import DefaultAzureCredential, get_bearer_token_providerfrom typing_extensions import Literalfrom .types import (@@ -115,13 +116,27 @@ def get_chat_completion_client_from_envs(**kwargs: Any) -> ChatCompletionClient:elif api_type == "azure":# Check Azure API key.azure_api_key = os.getenv("AZURE_OPENAI_API_KEY")- if azure_api_key is None:- raise ValueError("AZURE_OPENAI_API_KEY is not set")- kwargs["api_key"] = azure_api_key+ if azure_api_key is not None:+ kwargs["api_key"] = azure_api_key+ else:+ # Try to use token from Azure CLI.+ token_provider = get_bearer_token_provider(+ DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"+ )+ kwargs["azure_ad_token_provider"] = token_provider# Check Azure API endpoint.azure_api_endpoint = os.getenv("AZURE_OPENAI_API_ENDPOINT")if azure_api_endpoint is None:raise ValueError("AZURE_OPENAI_API_ENDPOINT is not set")kwargs["azure_endpoint"] = azure_api_endpoint+ # Get Azure API version.+ kwargs["api_version"] = os.getenv("AZURE_OPENAI_API_VERSION", "2024-06-01")+ # Set model capabilities.+ if "model_capabilities" not in kwargs or kwargs["model_capabilities"] is None:+ kwargs["model_capabilities"] = {+ "vision": True,+ "function_calling": True,+ "json_output": True,+ }return AzureOpenAIChatCompletionClient(**kwargs) # type: ignoreraise ValueError(f"Unknown API type: {api_type}")