Skip to content

Commit

Permalink
Merge pull request #186 from level3ai/gongmingqm10/support-azure-apiv…
Browse files Browse the repository at this point in the history
…ersion
  • Loading branch information
ksolo authored Feb 6, 2025
2 parents bd6b23f + 5519214 commit 89623b5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 4 additions & 0 deletions aisuite/providers/azure_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class AzureProvider(Provider):
def __init__(self, **config):
self.base_url = config.get("base_url") or os.getenv("AZURE_BASE_URL")
self.api_key = config.get("api_key") or os.getenv("AZURE_API_KEY")
self.api_version = config.get("api_version") or os.getenv("AZURE_API_VERSION")
if not self.api_key:
raise ValueError("For Azure, api_key is required.")
if not self.base_url:
Expand All @@ -93,6 +94,9 @@ def __init__(self, **config):
def chat_completions_create(self, model, messages, **kwargs):
url = f"{self.base_url}/chat/completions"

if self.api_version:
url = f"{url}?api-version={self.api_version}"

# Remove 'stream' from kwargs if present
kwargs.pop("stream", None)

Expand Down
5 changes: 4 additions & 1 deletion guides/azure.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ After creating your deployment, you'll need to gather the following information:

1. API Key: Found in the "Keys and Endpoint" section of your Azure OpenAI resource.
2. Base URL: This can be obtained from your deployment details. It will look something like this - `https://aisuite-Mistral-large-2407.westus3.models.ai.azure.com/v1/`
3. API Version: Optional configuration and mainly introduced for Azure OpenAI services. Once specified, the `api-version` query parameters will be added in the end of the API request.


Set the following environment variables:

```shell
export AZURE_API_KEY="your-api-key"
export AZURE_BASE_URL="https://deployment-name.region-name.models.ai.azure.com/v1"
export AZURE_API_VERSION="=2024-08-01-preview"
```

## Create a Chat Completion
Expand All @@ -38,7 +40,8 @@ import aisuite as ai
# Setting the params in ai.Client() will override the values from environment vars.
client = ai.Client(
base_url=os.environ["AZURE_OPENAI_BASE_URL"],
api_key=os.environ["AZURE_OPENAI_API_KEY"]
api_key=os.environ["AZURE_OPENAI_API_KEY"],
api_version=os.environ["AZURE_API_VERSION"]
)

model = "azure:aisuite-Mistral-large-2407" # Replace with your deployment name.
Expand Down

0 comments on commit 89623b5

Please sign in to comment.