Overview of the NeMo Guardrails Library Python API#
The NeMo Guardrails library Python API provides two core classes for running guardrails:
RailsConfig: Loads and manages guardrails configuration from files or content.LLMRails: The main interface for generating responses with guardrails applied.
Upon initializing the core classes (RailsConfig and LLMRails), the library loads the configuration files you created in the previous chapter About Configuring Guardrails.
Quick Start#
The following steps show how to run a sample guardrailed chat request using the NeMo Guardrails library Python API.
Prerequisites#
Meet the following prerequisites to use the NeMo Guardrails library Python API.
If you haven’t already, install the NeMo Guardrails library with the
nvidiaextra, following the instructions in Install the NeMo Guardrails Library.Set up an environment variable for your NVIDIA API key.
export NVIDIA_API_KEY="your-nvidia-api-key"This is required to access NVIDIA-hosted models on build.nvidia.com. The provided example configurations (examples/configs) and code examples throughout the documentation use NVIDIA-hosted models.
Run a Sample Guardrailed Chat Request#
The following example shows the minimal code to load the prepared configuration files in the config directory and generate a response using the LLMRails class.
from nemoguardrails import LLMRails, RailsConfig
# Load configuration from the config directory
config = RailsConfig.from_path("examples/configs")
# Create the LLMRails instance
rails = LLMRails(config)
# Generate a response
response = rails.generate(messages=[
{
"role": "user",
"content": "What is the capital of France?",
"config_id": "content_safety"
}
])
print(response["content"])
When to Use Each API#
API |
Use Case |
|---|---|
|
Standard chat interactions with messages |
|
Real-time token streaming |
|
Low-level event control for custom integrations |
Synchronous vs Asynchronous#
The NeMo Guardrails library provides both synchronous and asynchronous methods:
Synchronous |
Asynchronous |
Description |
|---|---|---|
|
|
Generate responses from messages |
|
|
Generate events from event history |
- |
|
Stream tokens asynchronously |
Note
Use asynchronous methods (generate_async, stream_async) in async contexts for better performance. The synchronous generate() method cannot be called from within an async context.