How nvidia/nemo-guardrails works
Main Components to Illustrate
• Core Guardrails Engine – This is the backbone of NeMo Guardrails. It includes:
• LLMRails (the public API for invoking the guardrails)
• RailsConfig (configuration loader that parses the guardrails configuration folder with its YAML files, .co files for Colang definitions, actions.py for custom logic, etc.)
• Colang parser and runtime (contained within the “nemoguardrails/colang” directories for versions 1.0 and 2.x, plus transformer and grammar components)
• Modules handling input, dialog, output, retrieval, and execution rails (with each type of rail functioning as a pipeline layer to modify, filter, or enhance the LLM requests/responses)
• LLM Connector and Provider Abstraction:
• LLM modules under “nemoguardrails/llm” that manage provider-specific integrations (OpenAI, Llama, etc.)
• Embedding providers and the integration of external vector search libraries (e.g., through “nemoguardrails/embeddings”)
• Actions and Custom Processing:
• The “actions” modules (found both in nemoguardrails/actions and within the rails, library subdirectories) that represent custom code executing validation, summarization, math operations, or integration with external APIs
• The guardrails library components (such as for jailbreak detection, self-check, fact-checking, etc.) that allow rapid extension of functionality
• Server and CLI Layers:
• The built-in CLI (nemoguardrails/cli) to trigger chat sessions, run evaluation tasks or launch an actions server
• The Server API (nemoguardrails/server) which exposes HTTP endpoints; includes datastore integrations (memory, redis) and asynchronous processing of LLM calls
• Integration with Third-Party Tools:
• Connectors for LangChain (found under “nemoguardrails/integrations/langchain”) which wrap guardrails around external chains
• Docker and devcontainer configurations (e.g., in the Dockerfile, .devcontainer folder) that indicate containerized deployment
Relationships and Interactions
• Input Flow:
• User input (either via chat-ui or an API call) is received and then passed to the guardrails engine.
• The input rail layer (with its Colang-based definitions from .co files) processes the received message.
• Dialog Processing:
• After pre-checks, the dialog rails guide the LLM’s behavior (by combining the loaded configurations and dynamic factors, e.g., as defined in the Colang scripts).
• The server’s actions (custom functions from actions.py) may be triggered, modifying or rejecting requested flows.
• Output Flow:
• The processed output then moves through the output rails which apply self-checks (fact-checking, moderation etc.) before returning the final content.
• The evaluation layer logs and monitors responses and flows (using modules in nemoguardrails/evaluate and nemoguardrails/logging).
• Asynchronous and API Communication:
• The core methods (synchronous and async versions of LLMRails.generate) illustrate the async-first design.
Important Architectural Patterns and Design Principles
• Modularity: Each rail type (input, dialog, output, retrieval, execution) is designed as an independent layer that can be combined or replaced.
• Configuration-driven behavior: The extensive use of configuration folders (YAML, .co Colang files, custom actions) means that the behavior is largely externalized.
• Asynchronous Processing: Emphasize async APIs in the core LLM interactions and guardrails processing.
Relevant Technologies, Frameworks, or Libraries
• Python (3.8+), Docker, and containerization (via Dockerfile and devcontainer)
• Annoy (for vector-based indexing in embeddings)
• External services for LLM providers, and integration with LangChain for chains
• Testing and CI/CD pipelines (evidenced by GitHub workflows, GitLab CI, pytest etc.)