Regular Expression Detection Integration
The NVIDIA NeMo Guardrails library provides out-of-the-box support for content moderation based on regular expression (regex) pattern matching. This integration enables you to detect and block content that matches specific patterns in user inputs, bot outputs, and retrieved knowledge base chunks.
Overview
Regex detection is useful for scenarios such as:
- Blocking specific keywords or phrases
- Detecting patterns like Social Security Numbers, credit card numbers, or other sensitive data formats
- Filtering profanity or inappropriate content using basic regex pattern matching
Setup
No additional packages are required. The regex detection rails is built into the NVIDIA NeMo Guardrails library.
Usage
The regex detection rails uses a flexible configuration that allows you to define specific regex patterns for input, output, and retrieval checking.
Configuration Structure
Add the regex configuration to your config.yml file:
Configuration Options
Input Rails
To detect regex patterns in user input:
Output Rails
To detect regex patterns in bot output:
Retrieval Rails
When a chunk matches any of the configured retrieval patterns, that chunk is removed from the retrieval results and is not passed to the model. Only chunks that do not match the patterns are kept and used as context.
To detect regex patterns in retrieved knowledge base chunks:
Complete Example
Here’s a comprehensive example configuration:
Return Value
The detect_regex_pattern action returns a RegexDetectionResult with the following fields:
For example, if the input text "my SSN is 123-45-6789" is checked against a pattern \\d{3}-\\d{2}-\\d{4}, the result would be:
Regex Pattern Tips
When writing Python regex patterns, keep in mind:
-
Escape backslashes: In YAML, use double backslashes (
\\) for regex escape sequences (e.g.,\\bfor word boundary,\\dfor digit). -
Word boundaries: Use
\\bto match whole words only. For example,\\bpassword\\bmatches “password” but not “passwords” or “mypassword”. -
Character classes: Use
[_-]?to match optional separators (e.g.,api[_-]?keymatches “apikey”, “api-key”, and “api_key”). -
Common patterns:
- SSN:
\\d{3}-\\d{2}-\\d{4} - Credit card:
\\d{4}[- ]?\\d{4}[- ]?\\d{4}[- ]?\\d{4} - Email:
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,} - Phone:
\\(?\\d{3}\\)?[-.\\s]?\\d{3}[-.\\s]?\\d{4}
- SSN:
For more information about Python regex rules, please refer to the official documentation.
Regex Rail Behavior
When a regex rail has a regex pattern match, the following steps happen (depending on rails type):
Input and output rails
- The action returns a
RegexDetectionResultwithis_match=Trueand the matched pattern(s) indetections. - The bot responds with:
"I'm sorry, I can't respond to that."(using the standardbot refuse to respondmessage). - The flow is aborted.
Retrieval rails
- Chunks that match any retrieval pattern are removed from the retrieval results. Only non-matching chunks are passed to the model as context.
NOTE: The matched pattern(s) are available in result["detections"] for logging and debugging. They are also logged at INFO level by the action.