How to deploy a model in Azure OpenAI
Quick answer
To deploy a model in Azure OpenAI, create a deployment in the Azure portal under your OpenAI resource, specifying the model and deployment name. Then use the AzureOpenAI Python client with your api_key and azure_endpoint to call the deployed model by its deployment name.
PREREQUISITES
Python 3.8+Azure OpenAI resource with deployment createdAzure OpenAI API keypip install openai>=1.0
Setup
Install the openai Python package version 1.0 or higher. Set environment variables for your Azure OpenAI API key, endpoint, and deployment name.
AZURE_OPENAI_API_KEY: Your Azure OpenAI API keyAZURE_OPENAI_ENDPOINT: Your Azure OpenAI endpoint URL (e.g.,https://your-resource-name.openai.azure.com/)AZURE_OPENAI_DEPLOYMENT: The deployment name you created in Azure portal
pip install openai>=1.0 Step by step
This example shows how to call a deployed model in Azure OpenAI using the AzureOpenAI client. Replace environment variables with your actual values.
import os
from openai import AzureOpenAI
client = AzureOpenAI(
api_key=os.environ["AZURE_OPENAI_API_KEY"],
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
api_version="2024-02-01"
)
def main():
response = client.chat.completions.create(
model=os.environ["AZURE_OPENAI_DEPLOYMENT"],
messages=[{"role": "user", "content": "Explain the benefits of Azure OpenAI."}]
)
print("Response:", response.choices[0].message.content)
if __name__ == "__main__":
main() output
Response: Azure OpenAI provides scalable, secure, and integrated AI services that enable developers to build intelligent applications with ease.
Common variations
You can use different models by creating separate deployments in the Azure portal and referencing their deployment names in model=. For streaming responses, use stream=True in chat.completions.create(). Async calls require an async client or wrapper since the official SDK is synchronous.
response = client.chat.completions.create(
model=os.environ["AZURE_OPENAI_DEPLOYMENT"],
messages=[{"role": "user", "content": "Tell me a joke."}],
stream=True
)
for chunk in response:
print(chunk.choices[0].delta.get('content', ''), end='') output
Why did the AI go to school? To improve its neural network!
Troubleshooting
- 401 Unauthorized: Check your
AZURE_OPENAI_API_KEYand ensure it has access to the resource. - 404 Not Found: Verify your
AZURE_OPENAI_ENDPOINTand deployment name are correct. - Model not found: Confirm the deployment exists in the Azure portal and is active.
Key Takeaways
- Create model deployments in the Azure portal before calling them via API.
- Use the AzureOpenAI client with your endpoint, API key, and deployment name.
- Streaming and multiple deployments are supported by changing parameters and deployment names.