Skip to main content
Guardrails can easily be integrated into flows for chatbots to help protect against common unwanted output like profanity and toxic language.

Setup

As a prerequisite we install the necessary validators from PyPI and gradio which we will integrate with for an interface.

Step 1: Download PDF and load it as string

To download this example as a Jupyter notebook, click here.
In this example, we will set up Guardrails with a chat model that can answer questions about the card agreement.

Step 2: Initialize Guard

The guard will execute LLM calls and ensure the response meets the requirements of the model and its validation.

Step 3: Initialize base message to LLM

Next we create a system message to guide the LLM’s behavior and give it the document for analysis.

Step 4: Integrate guard into UX

Here we use gradio to implement a simple chat interface.
The above code will yield a chat interface a user may interact with and ask questions about the document.

Step 5: Test guard validation

Let’s see what happens with perhaps some more malicious input from the user trying to force the model into returning some profanity like the prompt below.
We can examine the guard’s history and see the raw LLM output clearly has profanity in it. Validation has failed and our handling has worked successfully despite the model following the user’s instructions.
Output: