Let's explore another example wherein we will use guardrails.
AI's framework to validate the output generated by LLM, to ensure that it doesn't inadvertently mention
competitor's name in the output.
The program begins by importing the necessary libraries.
In this case, it imports base model and field classes from parenthesis, which are used to define models
and their fields.
Followed by importing a competitor check class from guardrails validator.
And then we are importing guard object and reach print for formatting the output.
Let's initialize the validator.
Here we use competitor check to ensure our product description doesn't mention the competitor's name.
We list our competitors Named Burger Haven and Sub Wraps and specify an action to take if the validation
fails, such as fixing the error or throwing an exception.
Next, we define a parent model for our products.
This model includes product name and description.
Importantly, we apply our validator to the description field, ensuring it adheres to the specifications.
Now, with our model and validators in place, we create a guard object.
This object will check LLM generated outputs to ensure they match our parenting model, essentially
enforcing our validation rules.
Let's simulate receiving an LLM generated product.
Description.
Our goal is to validate this output against our rules, ensuring no mention of competitors.
We use our guard object to parse and validate the output m.
Upon validation.
If the output mentions a competitor, our guard will attempt to raise an exception based on our failed
strategy.
Let's run this example.
And see the output.
As you can see, the validation failed for the description field because it found the name of competitor
here in the description.
Let's change the strategy from raising an exception to fixing the output and run this program again.
As you can see here.
The output validated by guardrails AI framework removes the description details because it has mention
of the compiler.
So it fixed the output by removing the required details.
This is how the computer checks work in AI guardrails.