In this blog post, I’ll show you on how to set up a content filtering configuration in Azure OpenAI Studio and apply it to your OpenAI deployment entirely using Terraform. Since there isn’t a native Terraform resource available for content filtering configurations, I will be using azapi_resource
to assist with this.
The content filtering system in Azure OpenAI Service is designed to complement its core models. It operates by subjecting both the prompt and completion to a combination of classification models, which are geared towards identifying and stopping the generation of harmful content. This system is capable of detecting and responding to various categories of potentially harmful content present in both the input prompts and the generated completions.
Read more about this here
Time to Terraform
To get started, we’ll need to create a resource group and a cognitive account:
resource "azurerm_resource_group" "rg" {
name = "tamops-openai-rg"
location = "West Europe"
}
resource "azurerm_cognitive_account" "cognitive_account" {
name = "tamops-ca"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
kind = "OpenAI"
sku_name = "S0"
depends_on = [
azurerm_resource_group.rg
]
}
Next, we’ll deploy the GPT 3.5 Turbo model. This involves configuring the cognitive deployment using Terraform:
resource "azurerm_cognitive_deployment" "cognitive_deployment" {
name = "tamops-gpt35"
cognitive_account_id = azurerm_cognitive_account.cognitive_account.id
rai_policy_name = "tamopscontentfilter"
model {
format = "OpenAI"
name = "gpt-35-turbo"
version = "0301"
}
scale {
type = "Standard"
}
depends_on = [
azurerm_resource_group.rg,
azurerm_cognitive_account.cognitive_account
]
}
Now to configure the content filter, notice below the reference of rai_policy_name
, this is the name of the content policy / RAI name, that we will deploy using the below azapi_resource
:
resource "azapi_resource" "content_filter" {
type = "Microsoft.CognitiveServices/accounts/raiPolicies@2023-10-01-preview"
name = "tamopscontentfilter"
parent_id = azurerm_cognitive_account.cognitive_account.id
schema_validation_enabled = false
body = jsonencode({
properties = {
mode = "Default",
basePolicyName = "Microsoft.Default",
contentFilters = [
{ name = "hate", blocking = true, enabled = true, allowedContentLevel = "High", source = "Prompt" },
{ name = "sexual", blocking = true, enabled = true, allowedContentLevel = "High", source = "Prompt" },
{ name = "selfharm", blocking = true, enabled = true, allowedContentLevel = "High", source = "Prompt" },
{ name = "violence", blocking = true, enabled = true, allowedContentLevel = "High", source = "Prompt" },
{ name = "hate", blocking = true, enabled = true, allowedContentLevel = "High", source = "Completion" },
{ name = "sexual", blocking = true, enabled = true, allowedContentLevel = "High", source = "Completion" },
{ name = "selfharm", blocking = true, enabled = true, allowedContentLevel = "High", source = "Completion" },
{ name = "violence", blocking = true, enabled = true, allowedContentLevel = "High", source = "Completion" },
{ name = "jailbreak", blocking = true, enabled = true, source = "Prompt" },
{ name = "protected_material_text", blocking = true, enabled = true, source = "Completion" },
{ name = "protected_material_code", blocking = true, enabled = true, source = "Completion" }
]
}
})
depends_on = [
azurerm_resource_group.rg,
azurerm_cognitive_account.cognitive_account,
azurerm_cognitive_deployment.cognitive_deployment
]
}
Reviewing in Azure AI Studio – we can see the available content filter tamopscontentfilter
has been created successfully:
Reviewing the deployment, we can see the content filter has also been assigned successfully – awesome!
With Terraform and AzAPI, deploying a content filtering configuration in Azure OpenAI Studio becomes a seamless process 🙂 – thanks for viewing!
GitHub Repository containing example Terraform from this blog post