|
| 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 | +``` |
0 commit comments