Documentation Index
Fetch the complete documentation index at: https://mintlify.com/NVIDIA-NeMo/Guardrails/llms.txt
Use this file to discover all available pages before exploring further.
Getting Started with Colang 2.0
This guide will walk you through creating your first Colang 2.0 bot, from a simple “Hello World” to implementing guardrails.Prerequisites
Install NeMo Guardrails:Hello World
Let’s start with the simplest possible Colang 2.0 script.Your First Script
Create a file calledrails.co:
- Import statement:
import coreloads the standard library withuser saidandbot sayflows - main flow: The entry point of every Colang 2.0 script
- user said: Matches when the user says something specific
- bot say: Instructs the bot to respond with a message
Testing
Test your script using the NeMo Guardrails CLI:This example doesn’t use the LLM. You must type exactly “hi” to get a response. We’ll add LLM support next.
Adding LLM Support
To make your bot more flexible, integrate LLM support.LLM-Powered Hello World
Updaterails.co:
import llm: Adds LLM capabilitiesactivate llm continuation: Enables the LLM to generate responsesactivate greeting: Makes the greeting flow active- Separate intent flows:
user expressed greetingandbot express greetingdefine intents
Configuration
Createconfig.yml:
Testing with LLM
Understanding Flows
Flow Types
Colang 2.0 has several flow types:- Main flow: The entry point (
flow main) - Active flows: Activated and run in background
- Regular flows: Called explicitly or by event matching
Flow Syntax
Flow Events
Flows generate events:Started: When a flow beginsFinished: When a flow completes successfullyFailed: When a flow fails
Real Example: Interaction Loop
Fromexamples/v2_x/tutorial/interaction_loop/main.co:
- Action matching:
match UtteranceUserAction.Finished() - Flow references:
as $ref_action_1stores a reference - Parallel actions: Both
$ref_action_2and$ref_action_3start simultaneously - Multi-modal: Combines utterance and gesture
Adding Guardrails
Let’s add input guardrails to check user messages.Input Rails Example
Fromexamples/v2_x/tutorial/guardrails_1/rails.co:
import guardrails: Loads guardrails supportinput railsflow: Special flow called for every user input- Generation operator (
...): Uses LLM to evaluate safety abort: Stops processing if input is unsafe
The Generation Operator
The... operator invokes the LLM at runtime:
Flow Activation
Flows can be activated to run in the background:Working with Variables
Variables in Colang 2.0 start with$:
Global Variables
Use theglobal keyword for variables shared across flows:
Keywords Reference
Core Keywords
flow: Define a flowimport: Import a moduleactivate: Activate a flow to run in backgroundmatch: Wait for an eventsend: Send an eventstart: Start a flow/action without waitingawait: Start and wait for completionreturn: Return a value from a flowabort: Stop the current flowglobal: Declare a global variable
Control Flow
if/else: Conditional branchingwhen/or when: Event-based branchingand/or: Logical operators
Operators
...: Generation operator (LLM invocation)as: Store a reference->: Define return values
Standard Library Overview
The standard library provides essential flows:core Module
llm Module
guardrails Module
timing Module
Best Practices
- Always import required modules at the top of your file
- Define
mainflow as the entry point - Use activate for background flows
- Name flows descriptively: Past tense for user events, imperative for bot actions
- Use the
...operator for dynamic LLM decisions - Add docstrings to document complex flows
- Test incrementally as you build
Complete Example: Simple Bot
Here’s a complete bot with greeting, capabilities, and safety:Next Steps
Language Reference
Complete Colang 2.0 syntax and features
Migration Guide
Migrate from Colang 1.0 to 2.0
Overview
Learn about Colang 2.0 features
Examples
See more examples