|
| 1 | +# GuardrailsAI Integration |
| 2 | + |
| 3 | +NeMo Guardrails provides out-of-the-box support for [GuardrailsAI](https://github.com/guardrails-ai/guardrails) validators, enabling comprehensive input and output validation using a rich ecosystem of community-built validators. GuardrailsAI offers validators for content safety, PII detection, toxic language filtering, jailbreak detection, topic restriction, and much more. |
| 4 | + |
| 5 | +The integration provides access to both built-in validators and the entire [Guardrails Hub](https://hub.guardrailsai.com/) ecosystem, allowing you to dynamically load and configure validators for your specific use cases. |
| 6 | + |
| 7 | +## Setup |
| 8 | + |
| 9 | +To use GuardrailsAI validators, you need to install the `guardrails-ai` package: |
| 10 | + |
| 11 | +```bash |
| 12 | +pip install guardrails-ai |
| 13 | +``` |
| 14 | + |
| 15 | +You may also need to install specific validators from the Guardrails Hub: |
| 16 | + |
| 17 | +```bash |
| 18 | +guardrails hub install guardrails/toxic_language |
| 19 | +guardrails hub install guardrails/detect_jailbreak |
| 20 | +guardrails hub install guardrails/guardrails_pii |
| 21 | +``` |
| 22 | + |
| 23 | +## Usage |
| 24 | + |
| 25 | +The GuardrailsAI integration uses a flexible configuration system that allows you to define validators with their parameters and metadata, then reference them in your input and output rails. |
| 26 | + |
| 27 | +### Configuration Structure |
| 28 | + |
| 29 | +Add GuardrailsAI validators to your `config.yml`: |
| 30 | + |
| 31 | +```yaml |
| 32 | +rails: |
| 33 | + config: |
| 34 | + guardrails_ai: |
| 35 | + validators: |
| 36 | + - name: toxic_language |
| 37 | + parameters: |
| 38 | + threshold: 0.5 |
| 39 | + validation_method: "sentence" |
| 40 | + metadata: {} |
| 41 | + - name: guardrails_pii |
| 42 | + parameters: |
| 43 | + entities: ["phone_number", "email", "ssn"] |
| 44 | + metadata: {} |
| 45 | + - name: competitor_check |
| 46 | + parameters: |
| 47 | + competitors: ["Apple", "Google", "Microsoft"] |
| 48 | + metadata: {} |
| 49 | +``` |
| 50 | +
|
| 51 | +### Input Rails |
| 52 | +
|
| 53 | +To use GuardrailsAI validators for input validation: |
| 54 | +
|
| 55 | +```yaml |
| 56 | +rails: |
| 57 | + input: |
| 58 | + flows: |
| 59 | + - guardrailsai check input $validator="guardrails_pii" |
| 60 | + - guardrailsai check input $validator="competitor_check" |
| 61 | +``` |
| 62 | +
|
| 63 | +### Output Rails |
| 64 | +
|
| 65 | +To use GuardrailsAI validators for output validation: |
| 66 | +
|
| 67 | +```yaml |
| 68 | +rails: |
| 69 | + output: |
| 70 | + flows: |
| 71 | + - guardrailsai check output $validator="toxic_language" |
| 72 | + - guardrailsai check output $validator="restricttotopic" |
| 73 | +``` |
| 74 | +
|
| 75 | +## Built-in Validators |
| 76 | +
|
| 77 | +The integration includes support for the following validators that are pre-registered in the NeMo Guardrails validator registry. For detailed parameter specifications and usage examples, refer to the official [GuardrailsAI Hub](https://hub.guardrailsai.com/) documentation for each validator: |
| 78 | +
|
| 79 | +- `competitor_check` - `hub://guardrails/competitor_check` |
| 80 | +- `detect_jailbreak` - `hub://guardrails/detect_jailbreak` |
| 81 | +- `guardrails_pii` - `hub://guardrails/guardrails_pii` |
| 82 | +- `one_line` - `hub://guardrails/one_line` |
| 83 | +- `provenance_llm` - `hub://guardrails/provenance_llm` |
| 84 | +- `regex_match` - `hub://guardrails/regex_match` |
| 85 | +- `restricttotopic` - `hub://tryolabs/restricttotopic` |
| 86 | +- `toxic_language` - `hub://guardrails/toxic_language` |
| 87 | +- `valid_json` - `hub://guardrails/valid_json` |
| 88 | +- `valid_length` - `hub://guardrails/valid_length` |
| 89 | + |
| 90 | +## Complete Example |
| 91 | + |
| 92 | +Here's a comprehensive example configuration: |
| 93 | + |
| 94 | +```yaml |
| 95 | +models: |
| 96 | + - type: main |
| 97 | + engine: openai |
| 98 | + model: gpt-4 |
| 99 | +
|
| 100 | +rails: |
| 101 | + config: |
| 102 | + guardrails_ai: |
| 103 | + validators: |
| 104 | + - name: toxic_language |
| 105 | + parameters: |
| 106 | + threshold: 0.5 |
| 107 | + validation_method: "sentence" |
| 108 | + metadata: {} |
| 109 | + - name: guardrails_pii |
| 110 | + parameters: |
| 111 | + entities: ["phone_number", "email", "ssn", "credit_card"] |
| 112 | + metadata: {} |
| 113 | + - name: competitor_check |
| 114 | + parameters: |
| 115 | + competitors: ["Apple", "Google", "Microsoft", "Amazon"] |
| 116 | + metadata: {} |
| 117 | + - name: restricttotopic |
| 118 | + parameters: |
| 119 | + valid_topics: ["technology", "science", "education"] |
| 120 | + metadata: {} |
| 121 | + - name: valid_length |
| 122 | + parameters: |
| 123 | + min: 10 |
| 124 | + max: 500 |
| 125 | + metadata: {} |
| 126 | +
|
| 127 | + input: |
| 128 | + flows: |
| 129 | + - guardrailsai check input $validator="guardrails_pii" |
| 130 | + - guardrailsai check input $validator="competitor_check" |
| 131 | +
|
| 132 | + output: |
| 133 | + flows: |
| 134 | + - guardrailsai check output $validator="toxic_language" |
| 135 | + - guardrailsai check output $validator="restricttotopic" |
| 136 | + - guardrailsai check output $validator="valid_length" |
| 137 | +``` |
| 138 | + |
| 139 | +## Custom Validators from Guardrails Hub |
| 140 | + |
| 141 | +You can use any validator from the [Guardrails Hub](https://hub.guardrailsai.com/) by specifying its hub path: |
| 142 | + |
| 143 | +```yaml |
| 144 | +rails: |
| 145 | + config: |
| 146 | + guardrails_ai: |
| 147 | + validators: |
| 148 | + - name: custom_validator_name |
| 149 | + parameters: |
| 150 | + # Custom parameters specific to the validator |
| 151 | + metadata: {} |
| 152 | +``` |
| 153 | + |
| 154 | +The integration will automatically fetch validator information from the hub if it's not in the built-in registry. |
| 155 | + |
| 156 | +## Performance Considerations |
| 157 | + |
| 158 | +- Validators are cached to improve performance on repeated use |
| 159 | +- Guard instances are reused when the same validator is called with identical parameters |
| 160 | +- Consider the latency impact when chaining multiple validators |
| 161 | + |
| 162 | +For a complete working example, see the [GuardrailsAI example configuration](https://github.com/NVIDIA/NeMo-Guardrails/tree/develop/examples/configs/guardrails_ai/). |
0 commit comments