Azure OpenAI requires registration. Customers who wish to use Azure OpenAI are required to submit a registration form.
- Create an Azure account.
- Request access to Azure OpenAI service.
- Install Terraform.
Terraform supports a number of different methods for authenticating to Azure, see here.
You will need the az
binary in order to create Terraform resources using the Azure provider.
sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
sudo yum install -y https://packages.microsoft.com/config/rhel/9.0/packages-microsoft-prod.rpm
sudo yum install azure-cli
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install terraform
Azure OpenAI service resources provisioned by Terraform code in this repository will be accessible from all networks, including the Internet.
If you wish to restrict access to your IP only, you can do so by modifying the file main.tf
and updating the network ACL block, e.g.:
network_acls {
default_action = "DENY"
ip_rules = ["1.2.3.4"]
}
Terraform state is stored by default in a local file named terraform.tfstate
. This repository does not use a remote state file.
You must do this before running Terraform:
az login
terraform init -upgrade
terraform apply
Retrieve your OpenAI endpoint URL.
terraform output azure_openai_endpoint
Only one key is necessary to make an API call.
terraform output openai_primary_access_key
When regenerating the first key, you can use the second key for continued access to the service.
terraform output openai_secondary_access_key
Use openai-demo.py
script to submit a prompt to your Azure OpenAI service and receive an output.
Install openai
using pip
:
pip install openai==0.28
Export environment variables:
cd ./terraform/
export AZURE_OPENAI_ENDPOINT=$(terraform output -raw azure_openai_endpoint)
export AZURE_OPENAI_API_KEY=$(terraform output -raw openai_primary_access_key)
Run the Python script:
./openai-demo.py
You should get output similar to the one below.
{
"choices": [
{
"content_filter_results": {
"hate": {
"filtered": false,
"severity": "safe"
},
"self_harm": {
"filtered": false,
"severity": "safe"
},
"sexual": {
"filtered": false,
"severity": "safe"
},
"violence": {
"filtered": false,
"severity": "safe"
}
},
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"message": {
"content": "The capital city of the UK is London.",
"role": "assistant"
}
}
],
"created": 1713805619,
"id": "chatcmpl-9GrQ37I3RExn2umvVIoIkL71auwRk",
"model": "gpt-35-turbo",
"object": "chat.completion",
"prompt_filter_results": [
{
"prompt_index": 0,
"content_filter_results": {
"hate": {
"filtered": false,
"severity": "safe"
},
"self_harm": {
"filtered": false,
"severity": "safe"
},
"sexual": {
"filtered": false,
"severity": "safe"
},
"violence": {
"filtered": false,
"severity": "safe"
}
}
}
],
"system_fingerprint": null,
"usage": {
"completion_tokens": 9,
"prompt_tokens": 27,
"total_tokens": 36
}
}