|
| 1 | +--- |
| 2 | +title: "Azure Guardrails" |
| 3 | +description: "Integrate Microsoft Azure's powerful content moderation services & PII guardrails with Portkey" |
| 4 | +--- |
| 5 | + |
| 6 | +Microsoft Azure offers robust content moderation and PII readaction services that can now be seamlessly integrated with Portkey's guardrails ecosystem. This integration supports two powerful Azure services: |
| 7 | + |
| 8 | +<CardGroup cols={2}> |
| 9 | + <Card title="Azure Content Safety" icon="shield-check"> |
| 10 | + A comprehensive content moderation service that detects harmful content including hate speech, violence, sexual content, and self-harm references in text. |
| 11 | + </Card> |
| 12 | + <Card title="Azure PII Detection" icon="user-lock"> |
| 13 | + Advanced detection of personally identifiable information (PII) and protected health information (PHI) to safeguard sensitive data. |
| 14 | + </Card> |
| 15 | +</CardGroup> |
| 16 | + |
| 17 | +## Setting Up Azure Guardrails |
| 18 | + |
| 19 | +Follow these steps to integrate Azure's content moderation services with Portkey: |
| 20 | + |
| 21 | +### 1. Configure Azure Authentication |
| 22 | + |
| 23 | +Navigate to the **Integrations** page under **Settings** to set up your Azure credentials. You can authenticate using three different methods: |
| 24 | + |
| 25 | +- **API Key** - Uses a simple API key for authentication |
| 26 | +- **Entra** (formerly Azure AD) - Uses Azure Active Directory authentication |
| 27 | +- **Managed** - Uses managed identity authentication within Azure |
| 28 | + |
| 29 | +Each authentication method requires specific credentials from your Azure account: |
| 30 | + |
| 31 | +<Tabs> |
| 32 | + <Tab title="API Key"> |
| 33 | + - **Resource Name**: Your Azure resource name |
| 34 | + - **API Key**: Your Azure API key |
| 35 | + </Tab> |
| 36 | + <Tab title="Entra"> |
| 37 | + - **Resource Name**: Your Azure resource name |
| 38 | + - **Client ID**: Your Azure client ID |
| 39 | + - **Client Secret**: Your client secret |
| 40 | + - **Tenant ID**: Your Azure tenant ID |
| 41 | + </Tab> |
| 42 | + <Tab title="Managed"> |
| 43 | + - **Resource Name**: Your Azure resource name |
| 44 | + - **Client ID**: Your Azure client ID (for managed identity) |
| 45 | + </Tab> |
| 46 | +</Tabs> |
| 47 | + |
| 48 | +### 2. Create Azure Guardrail Checks |
| 49 | + |
| 50 | +Once authentication is set up, you can add Azure guardrail checks to your Portkey workflow: |
| 51 | + |
| 52 | +1. Navigate to the **Guardrails** page |
| 53 | +2. Search for either **Azure Content Safety** or **Azure PII Detection** |
| 54 | +3. Click **Add** to configure your chosen guardrail |
| 55 | +4. Configure the specific settings for your guardrail |
| 56 | +5. Save your configuration and create the guardrail |
| 57 | + |
| 58 | +<Note> |
| 59 | + Guardrail Actions allow you to orchestrate your guardrails logic. You can learn more about them [here](/product/guardrails#there-are-6-types-of-guardrail-actions) |
| 60 | +</Note> |
| 61 | + |
| 62 | +## Azure Content Safety |
| 63 | + |
| 64 | +Azure Content Safety analyzes text for harmful content across several categories. |
| 65 | + |
| 66 | +**Configuration Options** |
| 67 | + |
| 68 | +| Parameter | Description | Values | |
| 69 | +|-----------|-------------|--------| |
| 70 | +| Blocklist Names | Custom Blocklist names from your azure setup | `blocklist-1`, `blocklist-2`, `blocklist-3` | |
| 71 | +| API Version | Azure Content Safety API version | Default: `2024-09-01` | |
| 72 | +| Severity | Minimum severity threshold for flagging content | `2`, `4`, `6`, or `8` | |
| 73 | +| Categories | Content categories to monitor | Hate, SelfHarm, Sexual, Violence | |
| 74 | +| Timeout | Maximum time in milliseconds for the check | Default: `5000` | |
| 75 | + |
| 76 | +### Using Blocklists |
| 77 | + |
| 78 | +Blocklists allow you to define custom terms or patterns to be flagged. You'll need to create Content Safety blocklists in your Azure account first, then reference them in the Blocklist Names field. |
| 79 | + |
| 80 | +<Info> |
| 81 | +For more information on Azure Content Safety blocklists, visit the [official documentation](https://learn.microsoft.com/en-us/azure/ai-services/content-safety/quickstart-blocklist). |
| 82 | +</Info> |
| 83 | + |
| 84 | +## Azure PII Detection |
| 85 | + |
| 86 | +Azure PII Detection identifies and can help protect personal and health-related information in your content. |
| 87 | + |
| 88 | +**Configuration Options** |
| 89 | + |
| 90 | +| Parameter | Description | Values | |
| 91 | +|-----------|-------------|--------| |
| 92 | +| Domain | The type of sensitive information to detect | `none` (both PII and PHI) or `phi` (only PHI) | |
| 93 | +| API Version | Azure PII Detection API version | Default: `2024-11-01` | |
| 94 | +| Model Version | Version of the detection model to use | Default: `latest` | |
| 95 | +| Redact | Option to redact detected information | `true` or `false` | |
| 96 | +| Timeout | Maximum time in milliseconds for the check | Default: `5000` | |
| 97 | + |
| 98 | +## Add Guardrail ID to a Config and Make Your Request |
| 99 | + |
| 100 | +* When you save a Guardrail, you'll get an associated Guardrail ID - add this ID to the `input_guardrails` or `output_guardrails` params in your Portkey Config |
| 101 | +* Create these Configs in Portkey UI, save them, and get an associated Config ID to attach to your requests. [More here](/product/ai-gateway/configs). |
| 102 | + |
| 103 | +Here's an example configuration: |
| 104 | + |
| 105 | +```json |
| 106 | +{ |
| 107 | + "input_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"], |
| 108 | + "output_guardrails": ["guardrails-id-xxx", "guardrails-id-yyy"] |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +<Tabs> |
| 113 | + <Tab title="NodeJS"> |
| 114 | +```js |
| 115 | +const portkey = new Portkey({ |
| 116 | + apiKey: "PORTKEY_API_KEY", |
| 117 | + config: "pc-***" // Supports a string config id or a config object |
| 118 | +}); |
| 119 | +``` |
| 120 | + </Tab> |
| 121 | + <Tab title="Python"> |
| 122 | +```py |
| 123 | +portkey = Portkey( |
| 124 | + api_key="PORTKEY_API_KEY", |
| 125 | + config="pc-***" # Supports a string config id or a config object |
| 126 | +) |
| 127 | +``` |
| 128 | + </Tab> |
| 129 | + <Tab title="OpenAI NodeJS"> |
| 130 | +```js |
| 131 | +const openai = new OpenAI({ |
| 132 | + apiKey: 'OPENAI_API_KEY', |
| 133 | + baseURL: PORTKEY_GATEWAY_URL, |
| 134 | + defaultHeaders: createHeaders({ |
| 135 | + apiKey: "PORTKEY_API_KEY", |
| 136 | + config: "CONFIG_ID" |
| 137 | + }) |
| 138 | +}); |
| 139 | +``` |
| 140 | + </Tab> |
| 141 | + <Tab title="OpenAI Python"> |
| 142 | +```py |
| 143 | +client = OpenAI( |
| 144 | + api_key="OPENAI_API_KEY", # defaults to os.environ.get("OPENAI_API_KEY") |
| 145 | + base_url=PORTKEY_GATEWAY_URL, |
| 146 | + default_headers=createHeaders( |
| 147 | + provider="openai", |
| 148 | + api_key="PORTKEY_API_KEY", # defaults to os.environ.get("PORTKEY_API_KEY") |
| 149 | + config="CONFIG_ID" |
| 150 | + ) |
| 151 | +) |
| 152 | +``` |
| 153 | + </Tab> |
| 154 | +<Tab title="cURL"> |
| 155 | +```sh |
| 156 | +curl https://api.portkey.ai/v1/chat/completions \ |
| 157 | + -H "Content-Type: application/json" \ |
| 158 | + -H "Authorization: Bearer $OPENAI_API_KEY" \ |
| 159 | + -H "x-portkey-api-key: $PORTKEY_API_KEY" \ |
| 160 | + -H "x-portkey-config: $CONFIG_ID" \ |
| 161 | + -d '{ |
| 162 | + "model": "gpt-3.5-turbo", |
| 163 | + "messages": [{ |
| 164 | + "role": "user", |
| 165 | + "content": "Hello!" |
| 166 | + }] |
| 167 | + }' |
| 168 | +``` |
| 169 | + </Tab> |
| 170 | +</Tabs> |
| 171 | + |
| 172 | +For more, refer to the [Config documentation](/product/ai-gateway/configs). |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | +## Monitoring and Logs |
| 178 | + |
| 179 | +All guardrail actions and verdicts are visible in your Portkey logs, allowing you to: |
| 180 | + |
| 181 | +- Track which content has been flagged |
| 182 | +- See guardrail verdicts and actions |
| 183 | +- Monitor the performance of your content moderation pipeline |
| 184 | + |
| 185 | + |
| 186 | +<Frame> |
| 187 | + <img src="/images/product/ai-gateway/ai-34.gif"/> |
| 188 | +</Frame> |
| 189 | + |
| 190 | + |
| 191 | +## Using Azure Guardrails - Scenarios |
| 192 | + |
| 193 | +After setting up your guardrails, there are different ways to use them depending on your security requirements: |
| 194 | + |
| 195 | +### Detect and Monitor Only |
| 196 | + |
| 197 | +To simply detect but not block content: |
| 198 | +- Configure your guardrail actions without enabling "Deny" |
| 199 | +- Monitor the guardrail results in your Portkey logs |
| 200 | +- If any issues are detected, the response will include a `hook_results` object with details |
| 201 | + |
| 202 | +### Redact PII Automatically |
| 203 | + |
| 204 | +To automatically remove sensitive information: |
| 205 | +- Enable the `Redact` option for Azure PII Detection |
| 206 | +- When PII is detected, it will be automatically redacted and replaced with standardized identifiers |
| 207 | +- The response will include a `transformed` flag set to `true` in the results |
| 208 | + |
| 209 | +### Block Harmful Content |
| 210 | + |
| 211 | +To completely block requests that violate your policies: |
| 212 | +- Enable the `Deny` option in the guardrails action tab |
| 213 | +- If harmful content is detected, the request will fail with an appropriate status code |
| 214 | +- You can customize denial messages to provide guidance to users |
| 215 | + |
| 216 | + |
| 217 | +--- |
| 218 | + |
| 219 | +## Need Support? |
| 220 | + |
| 221 | +If you encounter any issues with Azure Guardrails, please reach out to our support team through the [Portkey community forum](https://discord.gg/portkey-llms-in-prod-1143393887742861333). |
0 commit comments