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:
- Detects PII using both Presidio's built-in recognizers and a GLiNER-based recognizer
- Anonymizes detected PII to protect sensitive information
- Customizable entity types for targeted PII detection
- Provides detailed error spans for identified PII instances
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.
- Dependencies:
- guardrails-ai>=0.4.0
- gliner
- presidio-analyzer
- presidio-anonymizer
$ 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
- "CREDIT_CARD",
- "CRYPTO",
- "DATE_TIME",
- "EMAIL_ADDRESS",
- "IBAN_CODE",
- "IP_ADDRESS",
- "NRP",
- "LOCATION",
- "PERSON",
- "PHONE_NUMBER",
- "MEDICAL_LICENSE",
- "URL",
- "US_BANK_NUMBER",
- "US_DRIVER_LICENSE",
- "US_ITIN",
- "US_PASSPORT",
- "US_SSN",
- "UK_NHS",
- "ES_NIF",
- "ES_NIE",
- "IT_FISCAL_CODE",
- "IT_DRIVER_LICENSE",
- "IT_VAT_CODE",
- "IT_PASSPORT",
- "IT_IDENTITY_CARD",
- "PL_PESEL",
- "SG_NRIC_FIN",
- "SG_UEN",
- "AU_ABN",
- "AU_ACN",
- "AU_TFN",
- "AU_MEDICARE",
- "IN_PAN",
- "IN_AADHAAR",
- "IN_VEHICLE_REGISTRATION",
- "IN_VOTER",
- "IN_PASSPORT",
- "FI_PERSONAL_IDENTITY_CODE"
__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.
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.
Parameters
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.
- This method should not be called directly by the user. Instead, invoke
guard.parse(...)
where this method will be called internally for each associated Validator. - When invoking
guard.parse(...)
, ensure to pass the appropriatemetadata
dictionary that includes keys and values required by this validator. Ifguard
is associated with multiple validators, combine all necessary metadata into a single dictionary. -
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
ValidationResult
: APassResult
if no PII is detected, or aFailResult
with the anonymized text and error spans if PII is found.
Note:
Parameters
Returns
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.