Skip to content

Commit d7f21df

Browse files
author
gitName
committed
work in progress
1 parent 90ea746 commit d7f21df

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

articles/api-management/api-management-policies.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ More information about policies:
5959

6060
|Policy |Description | Classic | V2 | Consumption |Self-hosted |Workspace |
6161
|---------|---------|---------|---------|---------|--------|------|
62+
| [Enforce content safety checks on LLM requests](llm-content-safety-policy.md) | Enforces content safety checks on LLM requests (prompts) by transmitting them to the [Azure AI Content Safety](/azure/ai-services/content-safety/overview) service before sending to the backend LLM. | Yes | Yes | Yes | Yes | Yes |
6263
| [Validate content](validate-content-policy.md) | Validates the size or content of a request or response body against one or more API schemas. The supported schema formats are JSON and XML. | Yes | Yes | Yes | Yes | Yes |
6364
| [Validate GraphQL request](validate-graphql-request-policy.md) | Validates and authorizes a request to a GraphQL API. | Yes | Yes | Yes | Yes | No |
6465
| [Validate OData request](validate-odata-request-policy.md) | Validates a request to an OData API to ensure conformance with the OData specification. | Yes | Yes | Yes | Yes | Yes |

articles/api-management/llm-content-safety-policy.md

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,20 @@ ms.service: azure-api-management
88
ms.collection: ce-skilling-ai-copilot
99
ms.custom:
1010
ms.topic: article
11-
ms.date: 01/24/2025
11+
ms.date: 01/27/2025
1212
ms.author: danlep
1313
---
1414

15-
# Enforce content safety checks on LLM requests and responses
15+
# Enforce content safety checks on LLM requests
1616

1717
[!INCLUDE [api-management-availability-premium-dev-standard-basic-premiumv2-standardv2-basicv2](../../includes/api-management-availability-premium-dev-standard-basic-premiumv2-standardv2-basicv2.md)]
1818

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:
19+
The `llm-content-safety` policy enforces content safety checks on LLM requests (prompts) by transmitting them to the [Azure AI Content Safety](/azure/ai-services/content-safety/overview) service before sending to the backend LLM. When enabled and AI Content Safety detects malicious content, API Management blocks the request and returns a `403` error code.
2020

21-
* Block requests that contain harmful content or hate speech
22-
* Apply a blocklist to prevent specific content from being sent
21+
Use the policy in scenarios such as the following:
22+
23+
* Block requests that contain predefined categories of harmful content or hate speech
24+
* Apply custom blocklists to prevent specific content from being sent
2325
* Shield against prompts that match attack patterns
2426

2527
> [!NOTE]
@@ -30,9 +32,7 @@ The `llm-content-safety` policy enforces content safety checks by leveraging Azu
3032
## Prerequisites
3133

3234
* 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-
35+
* An API Management [backend](backends.md) configured to route content safety API calls and authenticate to the Azure AI Content Safety service.
3636

3737

3838
## Policy statement
@@ -56,24 +56,24 @@ The `llm-content-safety` policy enforces content safety checks by leveraging Azu
5656

5757
| Attribute | Description | Required | Default |
5858
| -------------- | ----------------------------------------------------------------------------------------------------- | -------- | ------- |
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 |
59+
| backend-id | Identifier (name) of the Azure AI Content Safety backend to route content-safety API calls to. Policy expressions are allowed. | Yes | N/A |
6060
| shield-prompt | If set to `true`, content is checked for user attacks. Otherwise, skip this check. Policy expressions are allowed. | No | `false` |
6161

6262

6363
## Elements
6464

6565
| Element | Description | Required |
6666
| -------------- | -----| -------- |
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 |
67+
| categories | A list of `category` elements that specify settings for blocking requests when the category is detected. | No |
68+
| blocklists | A list of [blocklist](/azure/ai-services/content-safety/how-to/use-blocklist) `id` elements from the Azure AI Content Safety instance for which detection causes the request to be blocked. Policy expressions are allowed. | No |
6969

7070
### categories attributes
7171

7272
| Attribute | Description | Required | Default |
7373
| -------------- | ----------------------------------------------------------------------------------------------------- | -------- | ------- |
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/>
74+
| output-type | Specifies how severity levels are returned by Azure AI 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 eight levels: 0,1,2,3,4,5,6,7.<br/><br/>
7777
Policy expressions are allowed. | No | `FourSeverityLevels` |
7878

7979

@@ -82,7 +82,7 @@ Policy expressions are allowed. | No | `FourSeverityLevels` |
8282
| Attribute | Description | Required | Default |
8383
| -------------- | ----------------------------------------------------------------------------------------------------- | -------- | ------- |
8484
| 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 |
85+
| threshold | Specifies the threshold value for this category at which request are blocked. Requests 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 |
8686

8787

8888
## Usage
@@ -93,15 +93,32 @@ Policy expressions are allowed. | No | `FourSeverityLevels` |
9393

9494
### Usage notes
9595

96+
* The policy runs on a concatenation of all text content in the request.
97+
* If the request exceeds the character limit of Azure AI Content Safety, or an internal error occurs, a `500` error is returned.
9698
* This policy can be used multiple times per policy definition.
9799

98-
## Examples
100+
## Example
101+
102+
The following example enforces content safety checks on LLM requests using the Azure AI Content Safety service. The policy blocks requests that contain hate speech or violence with a severity level of 4 or higher. The `shield-prompt` attribute is set to `true` to check for adversarial attacks.
99103

104+
```xml
105+
<policies>
106+
<inbound>
107+
<llm-content-safety backend-id="content-safety-backend" shield-prompt="true">
108+
<categories output-type="EightSeverityLevels">
109+
<category name="Hate" threshold="4" />
110+
<category name="Violence" threshold="4" />
111+
</categories>
112+
</llm-content-safety>
113+
</inbound>
114+
</policies>
115+
116+
```
100117

101118
## Related policies
102119

103120
* [Rate limiting and quotas](api-management-policies.md#rate-limiting-and-quotas)
104-
* [azure-openai-token-limit](azure-openai-token-limit-policy.md) policy
121+
* [llm-token-limit](llm-token-limit-policy.md) policy
105122
* [llm-emit-token-metric](llm-emit-token-metric-policy.md) policy
106123

107124
[!INCLUDE [api-management-policy-ref-next-steps](../../includes/api-management-policy-ref-next-steps.md)]
@@ -111,9 +128,9 @@ Policy expressions are allowed. | No | `FourSeverityLevels` |
111128
Questions
112129
113130
1. Inbound only for now?
131+
1. Checks are only on *requests*?
114132
1. GW and SKU support?
115133
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?
134+
1. Should we categorize this as a content validation policy?
118135
119136
-->

0 commit comments

Comments
 (0)