Kong / Kong/developer.konghq.com

(ai-gateway): Create how-to for blocking AI content using Azure Content Safety with AI Custom guardrails

Open Beginner friendly
#4,683 0 comments 0 reactions 0 assignees View on GitHub
internal product:ai-gateway
Dominant language
Ruby
Stars
28
Forks
121
Avg merge
1d 4h
Merged PRs (30d)
313

Description

The AI Custom Guardrail plugin currently has one how-to guide covering the Mistral AI Moderation API. The plugin supports any HTTP-based guardrail service, single-direction guarding, and metrics logging — none of which are covered by a dedicated guide. This issue tracks three guides to fill that gap.

**What it covers:**
- Configure the AI Custom Guardrail plugin with Azure AI Content Safety as the guardrail service
- Set harm category rejection levels (`Hate`, `Violence`) via `config.params`
- Write a `category_names` function to build the request payload and a `check_response` function to parse the Azure response
- Validate that a request violating a harm category returns 400

Context:

```yaml
_format_version: "3.0"
plugins:
- name: ai-custom-guardrail
config:
guarding_mode: BOTH
text_source: concatenate_all_content
params:
content_safety_url: ${{ env "DECK_CONTENT_SAFETY_URL" }}
content_safety_key: ${{ env "DECK_CONTENT_SAFETY_KEY" }}
categories: '[{ "name": "Hate", "rejection_level": 2}, { "name": "Violence",
"rejection_level": 2}]'
output_type: FourSeverityLevels
azure_api_version: '2023-10-01'
request:
url: "$(conf.params.content_safety_url)"
headers:
Ocp-Apim-Subscription-Key: "$(conf.params.content_safety_key)"
body:
categories: "$(category_names)"
text: "$(content)"
outputType: "$(conf.params.output_type)"
queries:
api-version: "$(conf.params.azure_api_version)"
response:
block: "$(check_response.block)"
block_message: "$(check_response.block_message)"
functions:
category_names: |
return function(conf)
local category_names = {}
for _, v in ipairs(conf.params.categories or {}) do
if v.rejection_level >= 2 then
table.insert(category_names, v.name)
end
end
return category_names
end
check_response: |
return function(resp, conf)
local res = {
block = true,
block_message = "content passed safety check",
}
if
((type(resp.categoriesAnalysis) ~= "table") or (#resp.categoriesAnalysis == 0))
and ((type(resp.blocklistsMatch) ~= "table") or (#resp.blocklistsMatch == 0))
then
return res, "content safety introspection is invalid"
end
local reason = ""
local ok = true
for _, v in ipairs(resp.categoriesAnalysis or EMPTY_T) do
local category_name = v.category
local failure_level = nil
for _, category in ipairs(conf.params.categories or {}) do
if category.name == category_name then
failure_level = category.rejection_level
break
end
end
if failure_level and (v.severity >= failure_level) then
if not ok then
reason = reason .. "; "
end
ok = false
reason = reason .. "breached category [" .. category_name .. "] at level " .. failure_level
end
end
for _, v in ipairs(resp.blocklistsMatch or EMPTY_T) do
local blocklist_name = v.blocklistName or "unknown_name"
ok = false
reason = reason .. "breached blocklist [" .. blocklist_name .. "]"
end
return {
block = not ok,
block_message = reason,
}
end
```

https://deploy-preview-4545--kongdeveloper.netlify.app/plugins/ai-custom-guardrail/examples/azure-content-safety/

Size: S

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the existing Mistral AI Moderation how-to and the Azure Content Safety example at the linked preview URL. Add the requested guides covering Azure configuration, category thresholds, request and response functions, single-direction guarding, metrics, and validation of a blocked request returning 400.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, lua
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.