|
| 1 | +--- |
| 2 | +title: Azure API Management policy reference - llm-content-safety |
| 3 | +description: Reference for the llm-content-safety policy available for use in Azure API Management. Provides policy usage, settings, and examples. |
| 4 | +services: api-management |
| 5 | +author: dlepow |
| 6 | + |
| 7 | +ms.service: azure-api-management |
| 8 | +ms.collection: ce-skilling-ai-copilot |
| 9 | +ms.custom: |
| 10 | +ms.topic: article |
| 11 | +ms.date: 01/24/2025 |
| 12 | +ms.author: danlep |
| 13 | +--- |
| 14 | + |
| 15 | +# Enforce content safety checks on LLM requests and responses |
| 16 | + |
| 17 | +[!INCLUDE [api-management-availability-premium-dev-standard-basic-premiumv2-standardv2-basicv2](../../includes/api-management-availability-premium-dev-standard-basic-premiumv2-standardv2-basicv2.md)] |
| 18 | + |
| 19 | +The `llm-content-safety` policy enforces content safety checks by leveraging Azure Content Safety. When enabled and the policy detects malicious content, API management returns a `403` error code. Use the policy in scenarios such as the following: |
| 20 | + |
| 21 | +* Block requests that contain harmful content or hate speech |
| 22 | +* Apply a blocklist to prevent specific content from being sent |
| 23 | +* Shield against prompts that match attack patterns |
| 24 | + |
| 25 | +> [!NOTE] |
| 26 | +> Currently, this policy is in preview. |
| 27 | +
|
| 28 | +[!INCLUDE [api-management-policy-generic-alert](../../includes/api-management-policy-generic-alert.md)] |
| 29 | + |
| 30 | +## Prerequisites |
| 31 | + |
| 32 | +* An [Azure AI Content Safety](/azure/ai-services/content-safety/) resource. |
| 33 | +* API Management [backend](backends.md) configured to route content-safety API calls to the Azure Content Safety service. |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +## Policy statement |
| 39 | + |
| 40 | +```xml |
| 41 | +<llm-content-safety backend-id="name of backend entity" shield-prompt="true | false" > |
| 42 | +<categories output-type="FourSeverityLevels | EightSeverityLevels"> |
| 43 | + <category name="Hate | SelfHarm | Sexual | Violence" threshold="integer" /> |
| 44 | + <!-- If there are multiple categories, add more category elements --> |
| 45 | + [...] |
| 46 | +</categories> |
| 47 | +<blocklists> |
| 48 | + <id>blocklist-identifier</id> |
| 49 | + <!-- If there are multiple blocklists, add more id elements --> |
| 50 | + [...] |
| 51 | +</blocklists> |
| 52 | +</llm-content-safety> |
| 53 | +``` |
| 54 | + |
| 55 | +## Attributes |
| 56 | + |
| 57 | +| Attribute | Description | Required | Default | |
| 58 | +| -------------- | ----------------------------------------------------------------------------------------------------- | -------- | ------- | |
| 59 | +| backend-id | Identifier (name) of the Azure Content Safety backend to route content-safety API calls to. Policy expressions are allowed. | Yes | N/A | |
| 60 | +| shield-prompt | If set to `true`, content is checked for user attacks. Otherwise, skip this check. Policy expressions are allowed. | No | `false` | |
| 61 | + |
| 62 | + |
| 63 | +## Elements |
| 64 | + |
| 65 | +| Element | Description | Required | |
| 66 | +| -------------- | -----| -------- | |
| 67 | +| categories | A list of `category` elements that specify settings for blocking messages when the category is detected. | No | |
| 68 | +| blocklists | A list of blocklist `id` elements for which detection will cause the message to be blocked. Policy expressions are allowed. | No | |
| 69 | + |
| 70 | +### categories attributes |
| 71 | + |
| 72 | +| Attribute | Description | Required | Default | |
| 73 | +| -------------- | ----------------------------------------------------------------------------------------------------- | -------- | ------- | |
| 74 | +| output-type | Specifies how severity levels are returned by Azure Content Safety. The attribute must have one of the following values.<br/><br/> |
| 75 | +- `FourSeverityLevels`: Output severities in four levels, 0,2,4,6.<br/> |
| 76 | +- `EightSeverityLevels`: Output severities in four levels, 0,1,2,3,4,5,6,7.<br/><br/> |
| 77 | +Policy expressions are allowed. | No | `FourSeverityLevels` | |
| 78 | + |
| 79 | + |
| 80 | +### category attributes |
| 81 | + |
| 82 | +| Attribute | Description | Required | Default | |
| 83 | +| -------------- | ----------------------------------------------------------------------------------------------------- | -------- | ------- | |
| 84 | +| name | Specifies the name of this category. The attribute must have one of the following values: `Hate`, `SelfHarm`, `Sexual`, `Violence`. Policy expressions are allowed. | Yes | N/A | |
| 85 | +| threshold | Specifies the threshold value for this category at which messages are blocked. Messages with content severities less than the threshold are not blocked. The value must be between 0 and 7. Policy expressions are allowed. | Yes | N/A | |
| 86 | + |
| 87 | + |
| 88 | +## Usage |
| 89 | + |
| 90 | +- [**Policy sections:**](./api-management-howto-policies.md#sections) inbound |
| 91 | +- [**Policy scopes:**](./api-management-howto-policies.md#scopes) global, workspace, product, API, operation |
| 92 | +- [**Gateways:**](api-management-gateways-overview.md) classic, v2, consumption, self-hosted, workspace |
| 93 | + |
| 94 | +### Usage notes |
| 95 | + |
| 96 | +* This policy can be used multiple times per policy definition. |
| 97 | + |
| 98 | +## Examples |
| 99 | + |
| 100 | + |
| 101 | +## Related policies |
| 102 | + |
| 103 | +* [Rate limiting and quotas](api-management-policies.md#rate-limiting-and-quotas) |
| 104 | +* [azure-openai-token-limit](azure-openai-token-limit-policy.md) policy |
| 105 | +* [llm-emit-token-metric](llm-emit-token-metric-policy.md) policy |
| 106 | + |
| 107 | +[!INCLUDE [api-management-policy-ref-next-steps](../../includes/api-management-policy-ref-next-steps.md)] |
| 108 | + |
| 109 | + |
| 110 | +<!-- |
| 111 | +Questions |
| 112 | +
|
| 113 | +1. Inbound only for now? |
| 114 | +1. GW and SKU support? |
| 115 | +1. Preview for now? |
| 116 | +1. How does admin create/config/apply a custom blocklist? Where is it? |
| 117 | +1. Is this a content validation policy? |
| 118 | +
|
| 119 | +--> |
0 commit comments