Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@
"integrations/guardrails/palo-alto-panw-prisma",
"integrations/guardrails/pillar",
"integrations/guardrails/prompt-security",
"integrations/guardrails/regex",
"integrations/guardrails/bring-your-own-guardrails"
]
},
Expand Down
97 changes: 97 additions & 0 deletions integrations/guardrails/regex.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
---
title: "Replace Custom Regex Patterns"
description: "Redact custom patterns with Regex in Portkey."
---

For more granular control over pattern redaction, you can create custom patterns using Portkey's **Regex Match** guardrail with redaction capabilities. This allows you to define specific regex patterns for sensitive information unique to your use case.

### Setting Up Custom Regex Patterns

<Frame>
<img src="/images/guardrails/regex.png" />
</Frame>

1. **Navigate to Guardrails**: Go to the `Guardrails` page and click `Create`
2. **Select Regex Match**: Choose the "Regex Replace" guardrail from the BASIC category
3. **Configure the Pattern**:
- **Regex Rule**: Enter your regex pattern to match specific data (e.g., `\b\d{3}-\d{2}-\d{4}\b` for SSN patterns)
- **Replacement Text**: Define what to replace matches with (e.g., `[REDACTED]`, `*****`, `[SSN_HIDDEN]`)

4. **Save the Guardrail**: Name your guardrail and save it to get the associated Guardrail ID

### Common Regex Patterns for Sensitive Data


| Pattern Type | Regex Pattern | Replacement Example |
|----------|---------------|-------------------|
| Credit Card | `\b\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}\b` | `[CREDIT_CARD]` |
| Social Security Number | `\b\d{3}-\d{2}-\d{4}\b` | `[SSN_REDACTED]` |
| Phone Numbers | `\b\d{3}[-.]\d{3}[-.]\d{4}\b` | `[PHONE_HIDDEN]` |
| Email Addresses | `\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b` | `[EMAIL_REDACTED]` |
| Custom Employee IDs | `EMP-\d{6}` | `[EMPLOYEE_ID]` |


### Adding to Your Config

Once you've created your custom regex pattern guardrail, add it to your Portkey config:

```json
{
"before_request_hooks": [
{"id": "your-guardrail-id"}
],
"after_request_hooks": [
{"id": "your-guardrail-id"}
]
}
```

<Note>
You can add the same guardrail to both `before_request_hooks` (input guardrails) and `after_request_hooks` (output guardrails) to scan and redact regex patterns in both user inputs and LLM responses.
</Note>

### Example Implementation

<Tabs>
<Tab title="NodeJS">
```js
const portkey = new Portkey({
apiKey: "PORTKEY_API_KEY",
config: "pc-***" // Config with custom regex pattern redaction
});

const response = await portkey.chat.completions.create({
model: "@your-model-slug",
messages: [
{
role: "user",
content: "My SSN is 123-45-6789 and credit card is 4532-1234-5678-9012"
}
]
});
```
</Tab>
<Tab title="Python">
```py
portkey = Portkey(
api_key="PORTKEY_API_KEY",
config="pc-***" # Config with custom regex pattern redaction
)

response = portkey.chat.completions.create(
model="@your-model-slug",
messages=[
{
"role": "user",
"content": "My SSN is 123-45-6789 and credit card is 4532-1234-5678-9012"
}
]
)
```
</Tab>
</Tabs>

With the custom regex guardrail configured, the input would be automatically transformed to:
```
"My SSN is [SSN_REDACTED] and credit card is [CREDIT_CARD]"
```
12 changes: 4 additions & 8 deletions product/guardrails/pii-redaction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Redact `Phone number`, `Email addresses`, `Location information`, `IP addresses`
Based on Patronus's EnterprisePII dataset, this guardrail can detect and redact confidential information typically found in business documents like meeting notes, commercial contracts, marketing emails, performance reviews, and more
</Card>

<Card title="Pangea" href="https://pangea.cloud/docs/redact/using-redact/redact-rules">
<Card title="Pangea" href="/integrations/guardrails/pangea">
Pangea's redact feature can redact PII like geographic locations, payment card industry (PCI) data, and many other types of sensitive information, with support for rule customization
</Card>
<Card title="AWS Bedrock Guardrails">
Expand Down Expand Up @@ -173,10 +173,6 @@ You can track request transformations through two key indicators in the request/
- Maintain records of what types of PII you're scanning for
- Document any specific compliance requirements being addressed

4. **Custom Regex Testing**:
- Test your regex patterns thoroughly before deploying
- Use regex testing tools to validate pattern accuracy
- Consider edge cases and variations in data formats

## Security Considerations

Expand Down Expand Up @@ -214,13 +210,13 @@ If you experience issues:
<AccordionGroup>

<Accordion title="Can I customize the redaction patterns?">
For pre-built guardrails, redaction patterns are standardized. However, you can create fully custom patterns using Portkey's Regex Match guardrail with redaction enabled.
Currently, redaction patterns are standardized and not customizable.
</Accordion>
<Accordion title="How does the system handle multiple instances of the same type of PII?">
Each instance receives a numbered identifier (e.g., `{{EMAIL_ADDRESS_1}}`, `{{EMAIL_ADDRESS_2}}`, etc.) or your custom replacement text for regex-based redaction.
Each instance receives a numbered identifier (e.g., `{{EMAIL_ADDRESS_1}}`, `{{EMAIL_ADDRESS_2}}`, etc.).
</Accordion>
<Accordion title="Does this feature impact request latency?">
Impact varies by guardrail provider and request complexity. Custom regex patterns with complex expressions may add minimal latency.
Impact varies by guardrail provider and request complexity. Custom regex patterns with complex expressions may add minimal latency.
</Accordion>
<Accordion title="Can I use this feature with any LLM?">
Yes, the feature works with any LLM supported by Portkey.
Expand Down