This site is not available on Mobile. Please return on a desktop browser.
Visit our main site at guardrailsai.com
Developed by | Guardrails AI |
---|---|
Date of development | September 2, 2024 |
Validator type | Format |
Blog | https://www.guardrailsai.com/blog/advanced-pii-and-jailbreak |
License | Apache 2 |
Input/Output | Output |
This validator is designed to detect and anonymize Personally Identifiable Information (PII) in LLM-generated text using state-of-the-art methods. Currently a combination of Presidio and GLiNER yields the highest performing results. It supports various entity types and can be configured to focus on specific PII categories.
Key features:
Use this validator to ensure that generated text does not inadvertently contain sensitive personal information, helping to maintain privacy and compliance with data protection regulations.
$ guardrails hub install hub://guardrails/guardrails_pii
In this example, we apply the validator to a string output generated by an LLM.
# Import Guard and Validator
from guardrails.hub import GuardrailsPII
from guardrails import Guard
# Setup Guard
guard = Guard().use(
GuardrailsPII(entities=["DATE_TIME"], on_fail="fix")
)
The following entities are supported by this validator. You may use any combination of them together in the validator init
__init__(self, entities: List[str], model_name: str = "urchade/gliner_small-v2.1", on_fail: Optional[Callable] = None)
Initializes a new instance of the GuardrailsPII class.
Parameters
entities
(List[str]): A list of entity types to detect and anonymize.model_name
(str, optional): The name of the GLiNER model to use. Defaults to "urchade/gliner_small-v2.1".on_fail
(Optional[Callable], optional): A callable to execute when the validation fails. Defaults to None.This validator uses Presidio and GLiNER to detect and anonymize PII in the generated text.
Key Properties
Property | Description |
---|---|
Name for format attribute | guardrails/guardrails_pii |
Supported data types | string |
Programmatic fix | Anonymized text |
validate(self, value: Any, metadata: Dict = {}) -> ValidationResult
Validates the given value
using the rules defined in this validator, relying on the metadata
provided to customize the validation process. This method is automatically invoked by guard.parse(...)
, ensuring the validation logic is applied to the input data.
Note:
guard.parse(...)
where this method will be called internally for each associated Validator.guard.parse(...)
, ensure to pass the appropriate metadata
dictionary that includes keys and values required by this validator. If guard
is associated with multiple validators, combine all necessary metadata into a single dictionary.Parameters
value
(Any): The input value to validate.
metadata
(Dict): A dictionary containing metadata required for validation. Keys and values must match the expectations of this validator.
Key | Type | Description | Default |
---|---|---|---|
entities | List[str] | List of entity types to detect and anonymize. | self.entities |
Returns
ValidationResult
: A PassResult
if no PII is detected, or a FailResult
with the anonymized text and error spans if PII is found.This method first retrieves the list of entities to detect from the metadata or falls back to the default entities set during initialization. It then uses the anonymize
method to process the input text and detect PII. If no PII is found, it returns a PassResult
. Otherwise, it returns a FailResult
with the anonymized text and error spans.