Run the NeMo Guardrails Library with the Python APIs#
This section covers how to use the NeMo Guardrails library programmatically through the Python API. Learn about the core classes, generation methods, and advanced features for integrating guardrails into your applications.
Core Classes#
The NeMo Guardrails library 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) or starting the nemoguardrails CLI chat or server, the toolkit loads the configuration files you created in the previous chapter Configure Rails.
Quick Start#
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("path/to/config")
# Create the LLMRails instance
rails = LLMRails(config)
# Generate a response
response = rails.generate(messages=[
{"role": "user", "content": "Hello! How are you?"}
])
print(response["content"])
Sections#
Load guardrails configurations with RailsConfig and generate responses with LLMRails.
Configure generation behavior with options for logging, LLM parameters, and rail selection.
Stream LLM responses in real-time with the stream_async method and output rails support.
Use generate_events for low-level control over guardrails execution and event handling.
When to Use Each API#
API |
Use Case |
|---|---|
|
Standard chat interactions with messages |
|
Real-time token streaming for responsive UIs |
|
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.