Let's explore how to enhance content moderation by chaining validators using the guardrails AI framework.
Validator chaining allows us to apply multiple content rules sequentially, ensuring comprehensive content
assessment.
We begin by importing necessary modules.
Guard is the core class from guardrails that orchestrates the validation process.
The competitor check and toxic language validators are specialized validators for identifying specific
content issues.
Setting up our validators is the first step in crafting a robust content moderation pipeline.
We define a list of competitors to watch for ensuring our content remains focused on our offerings.
Next, we initialize guard object, chaining our validators using guards use many method.
This setup ensures that any content we validate is checked both for unintended competitors and for toxic
language.
We have also defined a strategy to fix any competitor mentions and raise an exception if a toxic language
is found in the output.
With our validators configured, we proceed to validate a sample text.
This demonstrates the sequential application of our content rules addressing both competitive neutrality
and language positivity.
This is the text we are simulating as an output of LLM and that will be validated using guard object.
The text says my favorite phone is Sony Vue and followed by some toxic language.
As you can see, there is no mention of the competitor here and hence the guardrails.
I would pass this completed check, but should raise an exception as it detects the toxic language.
Let's run the application and see how the output works.
It takes a while to execute the program, but as you can see here in the output, the validation failed
for filled with errors.
The generated text was found to be toxic or inserting.
And if I could just change the validation strategy from exception to fix, the program should just filter
out the output.
In this case, the control should come here in the print statement and we should be able to see what
is the validated response output from guards validated object.
As you can see, the row output that we simulated is the one that we provided in the argument.
But the validated output is empty or none because we changed the strategy from exception to fix.
Validator chaining is a powerful feature of guardrails, enabling us to enforce multiple content guidelines
seamlessly.
By combining these two validators together, we ensure that our content upholds brand integrity and
fosters a positive community environment.