Skip to main content
This guide will walk you through creating your first guardrails-protected LLM application using NeMo Guardrails.

Prerequisites

1

Install NeMo Guardrails

If you haven’t already, install NeMo Guardrails:
See the Installation Guide for detailed instructions.
2

Get an OpenAI API Key

For this quickstart, we’ll use OpenAI’s GPT models. Set your API key:
NeMo Guardrails supports many LLM providers. See the Configuration Guide for other options.

Your First Guardrails Configuration

A guardrails configuration consists of two main components:
  1. config.yml - Defines the LLM model and active guardrails
  2. rails.co or main.co - Contains Colang definitions for dialog flows

Create the Configuration Directory

Create config.yml

Create a file named config.yml with the following content:
config.yml
This configures NeMo Guardrails to use OpenAI’s GPT-3.5 model as the main LLM.

Create rails.co

Now create rails.co with basic dialog guardrails:
rails.co
This Colang configuration defines:
  • A greeting flow that responds to user greetings
  • A topical rail that prevents political discussions

Using the Python API

Now let’s use the guardrails configuration in Python:

Interactive Chat

You can also test your configuration using the built-in CLI chat interface:
This starts an interactive session where you can chat with your guardrails-protected LLM:

Running a Guardrails Server

You can also run your configuration as an HTTP server:
The server starts on http://localhost:8000 and provides an OpenAI-compatible API:

Adding Input and Output Rails

Let’s enhance our configuration with input and output moderation rails. Update your config.yml:
config.yml
Create rails/input_rails.co:
rails/input_rails.co
Create rails/output_rails.co:
rails/output_rails.co
These built-in guardrails use LLM self-checking to detect jailbreak attempts and verify factual accuracy.

Real-World Example: ABC Company Bot

Here’s a complete example based on the ABC Bot from the source code - an employee handbook assistant with comprehensive guardrails:
Expected Output:

Understanding the Configuration Structure

A complete guardrails configuration typically follows this structure:
You can also use a single rails.co file instead of the rails/ directory for simpler configurations.

Next Steps

Configuration Guide

Learn about advanced configuration options, LLM models, and rail types

Colang Language Guide

Master the Colang modeling language for dialog control

Guardrails Library

Explore built-in guardrails for safety, security, and compliance

Examples

Browse comprehensive examples in the GitHub repository

Common Patterns

Create custom Python actions in actions.py:
actions.py
Use in Colang:
Handle complex multi-turn dialogs:
Add retrieval rails for RAG applications:
config.yml
rails/retrieval.co
Wrap guardrails around LangChain chains:

Troubleshooting

Issue: Your defined rails aren’t being applied.Solutions:
  • Ensure your user message examples in Colang closely match actual user input
  • Check that rails are registered in config.yml under the appropriate section
  • Use verbose=True when creating LLMRails to see debug output:
Issue: Errors when calling OpenAI.Solutions:
  • Verify your API key is set: echo $OPENAI_API_KEY
  • Check your OpenAI account has available credits
  • Try a different model (e.g., gpt-3.5-turbo instead of gpt-3.5-turbo-instruct)
Issue: Errors loading Colang files.Solutions:
  • Check indentation (use spaces, not tabs)
  • Ensure flow definitions have proper structure
  • Validate strings are properly quoted
  • Look for detailed error messages in the output

Need Help?

Visit the FAQ or ask questions on GitHub Discussions