LLMRails
TheLLMRails class is the primary interface for creating and managing guardrails in NeMo Guardrails. It provides methods for generating responses with guardrails applied.
Constructor
A
RailsConfig object containing the guardrails configuration.An optional LLM engine to use. If provided, this will be used as the main LLM and will take precedence over any main LLM specified in the config.
Whether the logging should be verbose or not.
Methods
generate_async
Generate a completion or next message asynchronously.The prompt to be used for completion.
The history of messages to be used to generate the next message. Messages have the format:
Options specific for the generation.
The state object that should be used as the starting point.
If specified, and the config supports streaming, the provided handler will be used for streaming.
The completion (when a prompt is provided) or the next message.
generate
Synchronous version ofgenerate_async.
stream_async
Stream the response tokens asynchronously.Whether to include metadata in the streamed chunks.
If provided, uses this external generator for streaming.
An async iterator that yields token chunks as strings, or dicts if
include_metadata=True.check_async
Run rails on messages to check for policy violations.List of message dicts with ‘role’ and ‘content’ fields.
Optional list of rail types to run (e.g.,
[RailType.INPUT] or [RailType.OUTPUT]). When not provided, automatically determines which rails to run based on message roles.Contains:
status: PASSED, MODIFIED, or BLOCKEDcontent: The final content after rails processingrail: Name of the rail that blocked (if blocked)
check
Synchronous version ofcheck_async.
register_action
Register a custom action for the rails configuration.The action function to register.
The name to use for the action. If not provided, uses the function name.
register_filter
Register a custom filter for the rails configuration.register_embedding_provider
Register a custom embedding provider.The embedding model class.
The name of the embedding engine.
explain
Returns detailed information about the latest generation.An object containing detailed explanation information including LLM calls, activated rails, and Colang history.
Attributes
The rails configuration object.
The main LLM engine being used.
The Colang runtime instance.
Whether verbose logging is enabled.