Core Classes Reference
This guide covers the two fundamental classes in the NeMo Guardrails library: RailsConfig for loading configurations and LLMRails for generating responses with guardrails.
RailsConfig
The RailsConfig class represents a complete guardrails configuration, including models, rails, flows, prompts, and other settings. This class requires to load the configuration from a directory or a single file you created in the previous chapter About Configuring Guardrails.
Loading Configuration from a Directory
The most common way to load a configuration is from a directory containing config.yml and Colang files:
Expected directory structure:
Loading from a Single File
You can also load from a single YAML file:
Loading from Content
For dynamic configurations or testing, load directly from strings:
Loading from a Dictionary
You can also provide configuration as a Python dictionary:
Combining Configurations
Configurations can be combined using the + operator:
This is useful for:
- Adding rails to a base configuration.
- Layering environment-specific settings.
- Combining shared and application-specific configurations.
Key Configuration Properties
LLMRails
The LLMRails class is the main interface for generating responses with guardrails applied.
Initialization
Constructor parameters:
Using a Custom LLM
You can provide your own LLM instance:
When providing an LLM via the constructor, it takes precedence over any main LLM specified in the configuration.
Generating Responses
Using Messages (Chat Format)
Using a Prompt (Completion Format)
With Conversation History
Passing Context
You can pass additional context using the context role:
You can access context variables in Colang flows using $variable_name syntax:
Alternatively, you can access context variables through the context parameter. For example, you can set up an action function that uses a variable extracted from the context parameter as follows:
For detailed information about context variables, see Action Parameters: The Context Parameter and Colang 1.0 Language Syntax: Variables.
Asynchronous Generation
For async contexts, use generate_async:
Streaming Responses
For real-time token streaming:
For detailed streaming configuration, refer to Streaming.
Event-based Generation
For low-level control using events:
For detailed event-based API usage, refer to Event-Based API.
Generation Options
Fine-tune generation behavior using the options parameter:
For detailed options, refer to Generation Options.
Checking Messages Against Rails
The check_async() and check() methods validate messages against input and output rails without triggering full LLM generation:
By default, the methods automatically detect which rails to run based on message roles. You can override this with the rail_types parameter:
For detailed usage, method signatures, and examples, refer to Check Messages.
Registering Custom Actions
You can register custom Python functions as actions:
For detailed action registration, refer to Custom Actions.
Registering Embedding Search Providers
For custom knowledge base search:
Complete Example
Related Resources
- Generation Options - Fine-grained control over generation
- Check Messages - Validate messages against rails
- Streaming - Real-time token streaming
- Event-Based API - Low-level event control
- Tools Integration - Integrating LangChain tools
- About Configuring Guardrails - Complete configuration reference