This guide will walk you through creating your first guardrails-protected LLM application using NeMo Guardrails.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/NVIDIA-NeMo/Guardrails/llms.txt
Use this file to discover all available pages before exploring further.
Prerequisites
Install NeMo Guardrails
If you haven’t already, install NeMo Guardrails:See the Installation Guide for detailed instructions.
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:- config.yml - Defines the LLM model and active guardrails
- rails.co or main.co - Contains Colang definitions for dialog flows
Create the Configuration Directory
Create config.yml
Create a file namedconfig.yml with the following content:
config.yml
Create rails.co
Now createrails.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:Running a Guardrails Server
You can also run your configuration as an HTTP server: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 yourconfig.yml:
config.yml
rails/input_rails.co:
rails/input_rails.co
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:Understanding the Configuration Structure
A complete guardrails configuration typically follows this structure: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
Define Custom Actions
Define Custom Actions
Create custom Python actions in Use in Colang:
actions.py:actions.py
Multi-Turn Conversations
Multi-Turn Conversations
Handle complex multi-turn dialogs:
RAG with Guardrails
RAG with Guardrails
Add retrieval rails for RAG applications:
config.yml
rails/retrieval.co
LangChain Integration
LangChain Integration
Wrap guardrails around LangChain chains:
Troubleshooting
Rails not triggering
Rails not triggering
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.ymlunder the appropriate section - Use
verbose=Truewhen creatingLLMRailsto see debug output:
OpenAI API errors
OpenAI API errors
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-turboinstead ofgpt-3.5-turbo-instruct)
Colang syntax errors
Colang syntax errors
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