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 the Hub:

Step 1: Initialize Guard

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

Step 2: 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 3: 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 4: Test guard validation

Let’s see what happens with perhaps some more malicious input from the user trying to get the chatbot to generate inappropriate content. When a user tries to prompt the chatbot to generate profanity or toxic language, the guard will catch it and return a safe response instead.

Benefits

Using Guardrails in a chatbot provides:
  1. Content safety - Automatically filters profanity and toxic language
  2. User protection - Prevents harmful content from reaching users
  3. Brand safety - Maintains appropriate tone and language
  4. Compliance - Helps meet content moderation requirements
  5. Flexibility - Easy to add or modify validators as needs change

Next steps

You can extend this example by:
  • Adding more validators from the Guardrails Hub
  • Implementing custom validators for domain-specific content
  • Adding streaming support for real-time validation
  • Integrating with your existing chat infrastructure