This site is not available on Mobile. Please return on a desktop browser.
Visit our main site at guardrailsai.com
Developed by | jenisys |
---|---|
Date of development | 2024-06-22 |
Validator type | rule-following |
License | MIT |
Input/Output | Output |
This guardrails validator provides support for cucumber-expressions, a simpler, more readable "regular expression" dialect with the following features:
parameter_type
(s) for common types, like: {int}
, {float}
, ...parameter_type
(s) with own regular expressions
,
type conversion and transformationapple/orange
(matches: apple
or orange
)apple(s)
(matches: apple
, apples
)This validator is similar to regex_match, but often easier to use because:
regular expressions
)SEE ALSO:
Check if text follows a specified schema (described by this cucumber-expression).
Dependencies:
Dev Dependencies:
$ guardrails hub install hub://guardrails/cucumber_expression_match
In this example, we apply the validator to a string output generated by an LLM.
# -- FILE: use_guardrails_cucumber_expression_match.py
from guardrails import Guard, OnFailAction
from guardrails.hub import CucumberExpressionMatch
from cucumber_expressions.parameter_type import ParameterType
# -- SETUP GUARD:
positive_number = ParameterType("positive_number", regexp=r"\d+", type=int)
guard = Guard().use(CucumberExpressionMatch,
expression="I buy {positive_number} apple(s)/banana(s)/orange(s)",
parameter_types=[positive_number],
on_fail=OnFailAction.EXCEPTION
)
# -- VALIDATOR PASSES: Good cases
guard.validate("I buy 0 apples") # Guardrail passes
guard.validate("I buy 1 apple") # Guardrail passes
guard.validate("I buy 1 banana") # Guardrail passes
guard.validate("I buy 2 bananas") # Guardrail passes
guard.validate("I buy 1 orange") # Guardrail passes
guard.validate("I buy 3 oranges") # Guardrail passes
# -- VALIDATOR FAILS: Bad cases
try:
guard.validate("I buy 2 melons") # Guardrail fails: Unexpected fruit
guard.validate("I buy -10 apples") # Guardrail fails: Negative number
except Exception as e:
print(e)