How do Validators work?
Each validator is a method that encodes some criteria, and checks if a given value meets that criteria.- If the value passes the criteria defined, the validator returns
PassResult. In most cases this means returning that value unchanged. In very few advanced cases, there may be a a value override (the specific validator will document this). - If the value does not pass the criteria, a
FailResultis returned. In this case, the validator applies the user-configuredon_failpolicies (see On-Fail Policies).
Runtime Metadata
Occasionally, validators need additional metadata that is only available during runtime. Metadata could be data generated during the execution of a validator (important if you’re writing your own validators), or could just be a container for runtime arguments. As an example, theExtractedSummarySentencesMatch validator accepts a filepaths property in the metadata dictionary to specify what source files to compare the summary against to ensure similarity. Unlike arguments which are specified at validator initialization, metadata is specified when calling guard.validate or guard.__call__ (this is the guard() function).
Provenance_LLM and DetectPII validators require metadata.
Custom Validators
Custom validators can extend the ability of Guardrails beyond the hub. Documentation for them can be found here.Submitting a Validator to the Hub
There are two ways to create a new validator and submit it to the Hub.- For lightweight validators, use the
hubCLI to create a new validator and submit it to the Hub. - For more complex validators, clone the Validator-Template repository and register the validator via the Guardrails Hub website.
Creating a new validator using the hub CLI
The hub CLI provides a simple way to create a new validator and submit it to the Hub. The hub CLI will create a new validator in the current directory and submit it to the Hub.
To create a new validator using the hub CLI, run the following command:
my_validator.py in the current directory. The file will contain a template and instructions for creating a new validator.
Creating a new validator using the Validator-Template repository
For more complex validators, you can clone the Validator-Template repository and register the validator via a Google Form on the Guardrails Hub website.Installing Validators
Guardrails Hub
Validators can be combined together into Input and Output Guards that intercept the inputs and outputs of LLMs. There are a large collection of Validators which can be found at the Guardrails Hub.
README to find the install link.
Using CLI
You can install a validator using the Guardrails CLI. For example the Toxic Language validator can be installed with:
This will not download local models if you opted into remote inferencing during guardrails configure
If you want to control if associated models are downloaded or not you can use theAfter installing the validator with the CLI you can start to use the validator in your guards:--install-local-modelsor--no-install-local-modelsflags respectively duringguardrails hub install
In Code Installs
You can also install validators using the Guardrails SDK which simplifies development particularly when using Jupyter Notebooks.In Code Installs - Pattern A
After aninstall invocation you can import a validator as you typically would:
In Code Installs - Pattern B
You can also extract the validator directly from the installed module as follows:install SDK always installs the validator module so it’s recommended for the install to be in a separate code block when using Notebooks.