Skip to content

Commit 4bbb3a2

Browse files
Adding azure documentation, and minor changes. (#31)
* Adding azure documentation, and minor changes. * Fixing test case, and adding better exception message.
1 parent 2e36536 commit 4bbb3a2

File tree

4 files changed

+71
-6
lines changed

4 files changed

+71
-6
lines changed

aisuite/providers/azure_provider.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
import urllib.request
22
import json
3+
import os
4+
35
from aisuite.provider import Provider
46
from aisuite.framework import ChatCompletionResponse
57

68

79
class AzureProvider(Provider):
810
def __init__(self, **config):
9-
self.base_url = config.get("base_url")
10-
self.api_key = config.get("api_key")
11+
self.base_url = config.get("base_url") or os.getenv("AZURE_BASE_URL")
12+
self.api_key = config.get("api_key") or os.getenv("AZURE_API_KEY")
1113
if not self.api_key:
12-
raise ValueError("api_key is required in the config")
14+
raise ValueError("For Azure, api_key is required.")
15+
if not self.base_url:
16+
raise ValueError(
17+
"For Azure, base_url is required. Check your deployment page for a URL like this - https://<model-deployment-name>.<region>.models.ai.azure.com"
18+
)
1319

1420
def chat_completions_create(self, model, messages, **kwargs):
15-
# TODO: Need to decide if we need to use base_url or just ignore it.
16-
# TODO: Remove the hardcoded region name to use environment variable.
1721
url = f"https://{model}.westus3.models.ai.azure.com/v1/chat/completions"
22+
url = f"https://{self.base_url}/chat/completions"
1823
if self.base_url:
1924
url = f"{self.base_url}/chat/completions"
2025

examples/client.ipynb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@
128128
"source": [
129129
"client2 = ai.Client({\"azure\" : {\n",
130130
" \"api_key\": os.environ[\"AZURE_API_KEY\"],\n",
131+
" \"base_url\": \"https://mistral-large-2407.westus3.models.ai.azure.com/v1/\",\n",
131132
"}});\n",
132-
"azure_model = \"azure:aisuite-Meta-Llama-3-8B-Inst\"\n",
133+
"azure_model = \"azure:mistral-large-2407\"\n",
133134
"response = client2.chat.completions.create(model=azure_model, messages=messages)\n",
134135
"print(response.choices[0].message.content)"
135136
]

guides/azure.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Azure AI
2+
3+
To use Azure AI with the `aisuite` library, you'll need to set up an Azure account and configure your environment for Azure AI services.
4+
5+
## Create an Azure Account and deploy a model from AI Studio
6+
7+
1. Visit [Azure Portal](https://portal.azure.com/) and sign up for an account if you don't have one.
8+
2. Create a project and resource group.
9+
3. Choose a model from https://ai.azure.com/explore/models and deploy it. You can choose serverless deployment option.
10+
4. Give a deployment name. Lets say you choose to deploy Mistral-large-2407. You could leave the deployment names as "mistral-large-2407" or give a custom name.
11+
5. You can see the deployment from project/deployment option. Note the Target URI from the Endpoint panel. It should look something like this - "https://aisuite-Mistral-large-2407.westus3.models.ai.azure.com".
12+
6. Also note, that is provides a Chat completion URL. It should look like this - https://aisuite-Mistral-large-2407.westus3.models.ai.azure.com/v1/chat/completions
13+
14+
15+
## Obtain Necessary Details & set environment variables.
16+
17+
After creating your deployment, you'll need to gather the following information:
18+
19+
1. API Key: Found in the "Keys and Endpoint" section of your Azure OpenAI resource.
20+
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/`
21+
22+
23+
Set the following environment variables:
24+
25+
```shell
26+
export AZURE_API_KEY="your-api-key"
27+
export AZURE_BASE_URL="https://deployment-name.region-name.models.ai.azure.com/v1"
28+
```
29+
30+
## Create a Chat Completion
31+
32+
With your account set up and environment configured, you can send a chat completion request:
33+
34+
```python
35+
import aisuite as ai
36+
37+
# Either set the environment variables or set the below two parameters.
38+
# Setting the params in ai.Client() will override the values from environment vars.
39+
client = ai.Client(
40+
base_url=os.environ["AZURE_OPENAI_BASE_URL"],
41+
api_key=os.environ["AZURE_OPENAI_API_KEY"]
42+
)
43+
44+
model = "azure:aisuite-Mistral-large-2407" # Replace with your deployment name.
45+
# The model name must match the deployment name in the base-url.
46+
47+
messages = [
48+
{"role": "system", "content": "You are a helpful assistant."},
49+
{"role": "user", "content": "What's the weather like today?"},
50+
]
51+
52+
response = client.chat.completions.create(
53+
model=model,
54+
messages=messages,
55+
)
56+
57+
print(response.choices[0].message.content)
58+
```

tests/client/test_client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def test_client_chat_completions(
4646
},
4747
ProviderNames.AZURE: {
4848
"api_key": "azure-api-key",
49+
"base_url": "https://model.ai.azure.com",
4950
},
5051
ProviderNames.GROQ: {
5152
"api_key": "groq-api-key",

0 commit comments

Comments
 (0)