|
1 | | -# gravitee-policy-ai-prompt-guard-rails |
| 1 | + |
| 2 | +<!-- GENERATED CODE - DO NOT ALTER THIS OR THE FOLLOWING LINES --> |
| 3 | +# AI - Prompt Guard Rails |
| 4 | + |
| 5 | +[](https://download.gravitee.io/#graviteeio-apim/plugins/policies/gravitee-policy-ai-prompt-guard-rails/) |
| 6 | +[](https://github.com/gravitee-io/gravitee-policy-ai-prompt-guard-rails/blob/master/LICENSE.txt) |
| 7 | +[](https://github.com/gravitee-io/gravitee-policy-ai-prompt-guard-rails/releases) |
| 8 | +[](https://circleci.com/gh/gravitee-io/gravitee-policy-ai-prompt-guard-rails) |
| 9 | + |
| 10 | +## Overview |
| 11 | +This policy uses an AI-powered text classification model to evaluate user prompts for potentially inappropriate or malicious content. It can detect a wide range of violations, such as profanity, sexually explicit language, harmful intent, and jailbreak prompt injections, which are adversarial inputs crafted to bypass AI safety mechanisms. |
| 12 | + |
| 13 | +Depending on configuration, when a prompt is flagged: |
| 14 | + |
| 15 | +* **Blocked and flagged** – the request is denied at the gateway |
| 16 | +* **Allowed but flagged** – the request proceeds but is logged for monitoring |
| 17 | + |
| 18 | +>**_NOTE_**: You may face an error when using this policy using the Gravitee's docker image. This is due to the fact that the default image are based on Alpine Linux, which does not support the ONNX Runtime. To resolve this issue you need to use the Gravitee's docker image based on Debian, which is available at `graviteeio/apim-gateway:4.8.0-debian`. |
| 19 | +
|
| 20 | +## Content Checks |
| 21 | + |
| 22 | +The Content Checks property specifies the classification labels that are applied to evaluate prompts. You should choose Labels in alignment with the selected model's capabilities and the intended filtering goals. For example, filtering for profanity while omitting toxicity checks. |
| 23 | + |
| 24 | +Supported labels are documented in the model’s card or configuration file. |
| 25 | + |
| 26 | + |
| 27 | +## AI Model Resource |
| 28 | + |
| 29 | +The policy requires an **AI Model Text Classification Resource** to be defined at the API level. This resource serves as the classification engine for evaluating prompts' content during policy execution. |
| 30 | + |
| 31 | +For more information about creating and managing resources, go to [Resources](https://documentation.gravitee.io/apim/policies/resources) |
| 32 | + |
| 33 | +After the resource is created, the policy must be configured with the corresponding name using the **AI Model Resource Name** property. |
| 34 | + |
| 35 | +>**_NOTE_**: The policy will load the model while handling the first request made to the API. Therefore, this first call will take longer than usual, as it includes the model loading time. Subsequent requests will be processed faster. |
| 36 | +
|
| 37 | + |
| 38 | +## Notice |
| 39 | + |
| 40 | +This plugin allows usage of models based on meta LLama4: |
| 41 | + |
| 42 | +* [gravitee-io/Llama-Prompt-Guard-2-22M-onxx](https://huggingface.co/gravitee-io/Llama-Prompt-Guard-2-22M-onnx) |
| 43 | +* [gravitee-io/Llama-Prompt-Guard-2-86M-onxx](https://huggingface.co/gravitee-io/Llama-Prompt-Guard-2-86M-onnx) |
| 44 | + |
| 45 | +> Llama 4 is licensed under the Llama 4 Community License, Copyright © Meta Platforms, Inc. All Rights Reserved. |
| 46 | +
|
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +## Phases |
| 51 | +The `ai-prompt-guard-rails` policy can be applied to the following API types and flow phases. |
| 52 | + |
| 53 | +### Compatible API types |
| 54 | + |
| 55 | +* `PROXY` |
| 56 | + |
| 57 | +### Supported flow phases: |
| 58 | + |
| 59 | +* Request |
| 60 | + |
| 61 | +## Compatibility matrix |
| 62 | +Strikethrough text indicates that a version is deprecated. |
| 63 | + |
| 64 | +| Plugin version| APIM| Java version | |
| 65 | +| --- | --- | --- | |
| 66 | +|1.0.0 and after|4.8.x and after|21 | |
| 67 | + |
| 68 | + |
| 69 | +## Configuration options |
| 70 | + |
| 71 | + |
| 72 | +#### |
| 73 | +| Name <br>`json name` | Type <br>`constraint` | Mandatory | Default | Description | |
| 74 | +|:----------------------|:-----------------------|:----------:|:---------|:-------------| |
| 75 | +| Content Checks<br>`contentChecks`| string| | | Comma-separated list of model labels (e.g., TOXIC,OBSCENE)| |
| 76 | +| Prompt Location<br>`promptLocation`| string| | | Prompt Location| |
| 77 | +| Request Policy<br>`requestPolicy`| enum (string)| | `LOG_REQUEST`| Request Policy<br>Values: `BLOCK_REQUEST` `LOG_REQUEST`| |
| 78 | +| Resource Name<br>`resourceName`| string| | | The resource name loading the Text Classification model| |
| 79 | +| Sensitivity threshold<br>`sensitivityThreshold`| number| | `0.5`| | |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +## Examples |
| 85 | + |
| 86 | +*Only log the request when inappropriate prompt detected* |
| 87 | +```json |
| 88 | +{ |
| 89 | + "api": { |
| 90 | + "definitionVersion": "V4", |
| 91 | + "type": "PROXY", |
| 92 | + "name": "AI - Prompt Guard Rails example API", |
| 93 | + "resources": [ |
| 94 | + { |
| 95 | + "name": "ai-model-text-classification-resource", |
| 96 | + "type": "ai-model-text-classification", |
| 97 | + "configuration": "{\"model\":{\"type\":\"MINILMV2_TOXIC_JIGSAW_MODEL\"}}", |
| 98 | + "enabled": true |
| 99 | + } |
| 100 | + ], |
| 101 | + "flows": [ |
| 102 | + { |
| 103 | + "name": "Common Flow", |
| 104 | + "enabled": true, |
| 105 | + "selectors": [ |
| 106 | + { |
| 107 | + "type": "HTTP", |
| 108 | + "path": "/", |
| 109 | + "pathOperator": "STARTS_WITH" |
| 110 | + } |
| 111 | + ], |
| 112 | + "request": [ |
| 113 | + { |
| 114 | + "name": "AI - Prompt Guard Rails", |
| 115 | + "enabled": true, |
| 116 | + "policy": "ai-prompt-guard-rails", |
| 117 | + "configuration": |
| 118 | + { |
| 119 | + "resourceName": "ai-model-text-classification-resource", |
| 120 | + "promptLocation": "{#request.jsonContent.prompt}", |
| 121 | + "contentChecks": "identity_hate,insult,obscene,severe_toxic,threat,toxic", |
| 122 | + "requestPolicy": "LOG_REQUEST" |
| 123 | + } |
| 124 | + } |
| 125 | + ] |
| 126 | + } |
| 127 | + ] |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +``` |
| 132 | +*Block request when inappropriate prompt detected* |
| 133 | +```json |
| 134 | +{ |
| 135 | + "api": { |
| 136 | + "definitionVersion": "V4", |
| 137 | + "type": "PROXY", |
| 138 | + "name": "AI - Prompt Guard Rails example API", |
| 139 | + "resources": [ |
| 140 | + { |
| 141 | + "name": "ai-model-text-classification-resource", |
| 142 | + "type": "ai-model-text-classification", |
| 143 | + "configuration": "{\"model\":{\"type\":\"MINILMV2_TOXIC_JIGSAW_MODEL\"}}", |
| 144 | + "enabled": true |
| 145 | + } |
| 146 | + ], |
| 147 | + "flows": [ |
| 148 | + { |
| 149 | + "name": "Common Flow", |
| 150 | + "enabled": true, |
| 151 | + "selectors": [ |
| 152 | + { |
| 153 | + "type": "HTTP", |
| 154 | + "path": "/", |
| 155 | + "pathOperator": "STARTS_WITH" |
| 156 | + } |
| 157 | + ], |
| 158 | + "request": [ |
| 159 | + { |
| 160 | + "name": "AI - Prompt Guard Rails", |
| 161 | + "enabled": true, |
| 162 | + "policy": "ai-prompt-guard-rails", |
| 163 | + "configuration": |
| 164 | + { |
| 165 | + "resourceName": "ai-model-text-classification-resource", |
| 166 | + "promptLocation": "{#request.jsonContent.prompt}", |
| 167 | + "contentChecks": "identity_hate,insult,obscene,severe_toxic,threat,toxic", |
| 168 | + "requestPolicy": "BLOCK_REQUEST" |
| 169 | + } |
| 170 | + } |
| 171 | + ] |
| 172 | + } |
| 173 | + ] |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +``` |
| 178 | +*Provide a custom sensitivity threshold for inappropriate prompts* |
| 179 | +```json |
| 180 | +{ |
| 181 | + "api": { |
| 182 | + "definitionVersion": "V4", |
| 183 | + "type": "PROXY", |
| 184 | + "name": "AI - Prompt Guard Rails example API", |
| 185 | + "resources": [ |
| 186 | + { |
| 187 | + "name": "ai-model-text-classification-resource", |
| 188 | + "type": "ai-model-text-classification", |
| 189 | + "configuration": "{\"model\":{\"type\":\"MINILMV2_TOXIC_JIGSAW_MODEL\"}}", |
| 190 | + "enabled": true |
| 191 | + } |
| 192 | + ], |
| 193 | + "flows": [ |
| 194 | + { |
| 195 | + "name": "Common Flow", |
| 196 | + "enabled": true, |
| 197 | + "selectors": [ |
| 198 | + { |
| 199 | + "type": "HTTP", |
| 200 | + "path": "/", |
| 201 | + "pathOperator": "STARTS_WITH" |
| 202 | + } |
| 203 | + ], |
| 204 | + "request": [ |
| 205 | + { |
| 206 | + "name": "AI - Prompt Guard Rails", |
| 207 | + "enabled": true, |
| 208 | + "policy": "ai-prompt-guard-rails", |
| 209 | + "configuration": |
| 210 | + { |
| 211 | + "resourceName": "ai-model-text-classification-resource", |
| 212 | + "promptLocation": "{#request.jsonContent.prompt}", |
| 213 | + "sensitivityThreshold": 0.1, |
| 214 | + "contentChecks": "identity_hate,insult,obscene,severe_toxic,threat,toxic", |
| 215 | + "requestPolicy": "BLOCK_REQUEST" |
| 216 | + } |
| 217 | + } |
| 218 | + ] |
| 219 | + } |
| 220 | + ] |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +``` |
| 225 | + |
| 226 | + |
| 227 | +## Changelog |
| 228 | + |
| 229 | + |
0 commit comments