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 | Feb 15, 2024 |
Validator type | Format |
Blog | |
License | Apache 2 |
Input/Output | Output |
This package uses the py-readability-metrics package to parse text and find its readability as a US grade level number (0-12).
Use this validator when you're requesting longer passages from your LLM. These algorithms are unreliable and will throw errors when trying to grade passages under 100 words.
$ guardrails hub install hub://guardrails/reading_level
In this example, we apply the validator to a string output generated by an LLM.
# Import Guard and Validator
from guardrails import Guard
from guardrails.hub import ReadingLevel
# Setup Guard
guard = Guard().use(
ReadingLevel, min=4, max=8, on_fail="exception")
guard.validate(
"""In a bustling bakery filled with the sweet scent of muffins and cookies, lived a mischievous little yeast named Sprout.
Sprout wasn't like the other yeasts who patiently puffed up dough. He longed for adventure!
One day, during a mixing frenzy, Sprout jumped on a batch of bread dough and hitched a ride.
He soared through the oven, dodging flames like a tiny acrobat, and emerged golden brown and bubbly.
Landing on a plate, he saw a wide-eyed boy about to take a bite! Sprout winked,
"This bread might taste extra bouncy today!" The boy laughed, taking a bite, and his eyes widened
with delight. From then on, Sprout continued his bakery escapades, adding a sprinkle of fun to every loaf!
"""
) # Validator passes
try:
guard.validate(
"""A Jay venturing into a yard where Peacocks used to walk, found there a number of feathers
which had fallen from the Peacocks when they were moulting. He tied them all to his tail and strutted
down towards the Peacocks. When he came near them they soon discovered the cheat, and striding up to him
pecked at him and plucked away his borrowed plumes. So the Jay could do no better than go back to the
other Jays, who had watched his behaviour from a distance; but they were equally annoyed with him,
and told him:"It is not only fine feathers that make fine birds.
"""
) # Validator fails
except Exception as e:
print(e)
Output:
Validation failed for field with errors: Reading level is 10, which is above the maximum of 8
__init__(self, min, max, on_fail="noop")
Initializes a new instance of the ReadingLevel class.
Parameters
min
(int): Minimum reading US Grade levelmax
(int): Maximum reading US Grade levelon_fail
(str, Callable): The policy to enact when a validator fails. If str
, must be one of reask
, fix
, filter
, refrain
, noop
, exception
or fix_reask
. Otherwise, must be a function that is called when the validator fails.__call__(self, value, metadata={}) -> ValidationOutcome
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:
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.
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.This validator does not require any metadata
The validator playground is available to authenticated users. Please log in to use it.