Azure OpenAI Services is a suite of artificial intelligence (AI) services provided by Microsoft and OpenAI that enable developers to easily add cutting-edge AI capabilities to their applications. These services include natural language processing, machine learning, and computer vision. By using Azure OpenAI Services, developers can enhance their applications with advanced AI capabilities without having to create their own models or algorithms.
Terraform is an open-source tool for deploying and managing infrastructure as code. It enables developers to define and provision infrastructure in a declarative way, using a high-level configuration language. By using Terraform to deploy Azure OpenAI Services resources, developers can automate the deployment process, manage their configuration, and ensure consistency across their environments.
In this article, we’ll walk through how to set up an Azure OpenAI Services account, write Terraform code to deploy the resources, and test the deployment.
Setting up Azure OpenAI Services
Before we can deploy Azure OpenAI Services resources, we need to create an Azure OpenAI Services account and obtain an API key. Here’s how to do that:
- Go to the Azure portal and sign in with your Azure account.
- Click on “Create a resource” in the left-hand menu and search for “OpenAI Services”.
- Select “OpenAI Services” from the search results and click on “Create”.
- Choose a subscription, resource group, name, and location for your Azure OpenAI Services account.
- Review and create the account.
- Once the account is created, click on “Keys and endpoints” in the left-hand menu to obtain your API key.
Make note of your API key, as we’ll use it in the Terraform code later.
Setting up Terraform
Next, we need to set up Terraform on our local machine and configure it to work with Azure. Here’s how to do that:
- Download and install Terraform from the official website.
- Open a terminal or command prompt and navigate to the directory where you want to store your Terraform code.
- Run the command
terraform init
to initialize the Terraform working directory and download the necessary plugins. - Configure Terraform to work with Azure by setting the following environment variables:
export ARM_SUBSCRIPTION_ID="your_subscription_id"
export ARM_CLIENT_ID="your_client_id"
export ARM_CLIENT_SECRET
- Replace “your_subscription_id”, “your_client_id”, and “your_client_secret” with your actual Azure subscription ID, client ID, and client secret. You can obtain these values from the Azure portal by going to “Azure Active Directory” and selecting “App registrations”.
Now that we’ve set up Azure OpenAI Services and Terraform, we can start writing the Terraform code to deploy the resources.
Writing the Terraform code
Here’s an example of how to write Terraform code to deploy Azure OpenAI Services:
# Configure the Azure provider
provider "azurerm" {
features {}
}
# Create a resource group
resource "azurerm_resource_group" "example" {
name = "example-resource-group"
location = "eastus"
}
# Create an Azure OpenAI Services resource
resource "azurerm_openai" "example" {
name = "example-openai-resource"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
sku = "standard"
api_key = "YOUR_API_KEY_HERE"
workspace_name = "example-workspace"
custom_settings = <<SETTINGS
{
"model": "text-davinci-002",
"temperature": 0.5
}
SETTINGS
}
# Output the endpoint and API key for the Azure OpenAI Services resource
output "openai_endpoint" {
value = azurerm_openai.example.endpoint
}
output "openai_api_key" {
value = azurerm_openai.example.api_key
}
This code defines a resource group and an Azure OpenAI Services resource. The azurerm_openai
resource is configured with a standard SKU, an API key, and a workspace name. The custom_settings
block specifies additional settings for the OpenAI model, such as the model to use and the temperature parameter. You’ll need to replace “YOUR_API_KEY_HERE” with your actual API key.
The output
blocks at the end of the code print out the endpoint and API key for the Azure OpenAI Services resource, which can be used to authenticate and access the resource.
Deploying the resources
To deploy the Azure OpenAI Services resource using Terraform, follow these steps:
- Save the Terraform code to a file with a
.tf
extension (e.g.,main.tf
). - Open a terminal or command prompt and navigate to the directory where you saved the Terraform code.
- Run the command
terraform init
to initialize the Terraform working directory and download the necessary plugins. - Run the command
terraform plan
to generate an execution plan for the resources to be created. - Review the plan and make sure it looks correct.
- Run the command
terraform apply
to create the resources in Azure. - Review the output to make sure the deployment was successful.
Testing the deployment
Once the deployment is complete, you can test the Azure OpenAI Services resource to make sure it’s working as expected. You can do this using the Azure portal or a testing tool such as Postman.
To test the resource using the Azure portal, follow these steps:
- Go to the Azure portal and navigate to your Azure OpenAI Services resource.
- Click on “Quick start” in the left-hand menu and follow the instructions to test the resource.
To test the resource using Postman, follow these steps:
- Install Postman from the official website.
- Open Postman and create a new request.
- Set the request method to
POST
and the request URL to the endpoint of your Azure OpenAI Services resource. - Set the
Content-Type
header toapplication/json
.
- Set the
Authorization
header toBearer <your_api_key>
, where<your_api_key>
is your actual API key. - In the request body, specify the text you want to analyze using the OpenAI model. For example:
{
"model": "text-davinci-002",
"prompt": "What is the meaning of life?",
"temperature": 0.5,
"max_tokens": 10,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0
}
- Send the request and review the response to see the results of the OpenAI analysis.
In this article, we’ve walked through how to deploy Azure OpenAI Services using Terraform. By using Terraform to automate the deployment process, we can manage our infrastructure as code and ensure consistency across our environments. We’ve also seen how to test the deployment using the Azure portal and Postman.
Azure OpenAI Services provides a powerful set of AI capabilities that can enhance our applications in meaningful ways. By using Terraform to deploy these resources, we can streamline our development process and focus on building great applications. We encourage you to try out Azure OpenAI Services and Terraform for yourself and see the benefits firsthand.
Contact us for more information or visit our blog.