mirror of https://github.com/microsoft/autogen.git
Add Azure Active Directory auth for Python samples (#315)
* add Azure Active Directory auth for samples * Update README
This commit is contained in:
parent
1f9d5177d3
commit
1c95443db5
|
@ -28,6 +28,7 @@ dependencies = [
|
|||
[tool.hatch.envs.default]
|
||||
installer = "uv"
|
||||
dependencies = [
|
||||
"azure-identity",
|
||||
"aiofiles",
|
||||
"chess",
|
||||
"colorama",
|
||||
|
|
|
@ -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:
|
||||
|
||||
```bash
|
||||
hatch shell
|
||||
```
|
||||
|
||||
Then, you need to set the `OPENAI_API_KEY` environment variable to your OpenAI API key.
|
||||
|
||||
```bash
|
||||
export OPENAI_API_KEY=your_openai_api_key
|
||||
```
|
||||
### Using Azure OpenAI API
|
||||
|
||||
For Azure OpenAI API, you need to set the following environment variables:
|
||||
|
||||
```bash
|
||||
export AZURE_OPENAI_API_KEY=your_azure_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
|
||||
```
|
||||
|
||||
By default, OpenAI API is used.
|
||||
To use Azure OpenAI API, set the `OPENAI_API_TYPE`
|
||||
environment variable to `azure`.
|
||||
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:
|
||||
|
||||
```bash
|
||||
export OPENAI_API_TYPE=azure
|
||||
export AZURE_OPENAI_API_KEY=your_azure_openai_api_key
|
||||
```
|
||||
|
||||
### Using OpenAI API
|
||||
|
||||
For OpenAI API, you need to set the following environment variables.
|
||||
|
||||
```bash
|
||||
export OPENAI_API_TYPE=openai
|
||||
export OPENAI_API_KEY=your_openai_api_key
|
||||
```
|
||||
|
||||
### Running
|
||||
|
|
|
@ -11,6 +11,7 @@ from agnext.components.models import (
|
|||
OpenAIChatCompletionClient,
|
||||
UserMessage,
|
||||
)
|
||||
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
|
||||
from typing_extensions import Literal
|
||||
|
||||
from .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: ignore
|
||||
raise ValueError(f"Unknown API type: {api_type}")
|
||||
|
|
Loading…
Reference in New Issue