Colang Modeling Language
Colang is a modeling language specifically created for designing flexible, yet controllable, dialogue flows for conversational AI systems. It enables you to define guardrails, conversational patterns, and LLM behavior with a Python-like syntax.Why Colang?
Traditional dialog management techniques like flowcharts, state machines, and frame-based systems are not well-suited for modeling the highly flexible conversational flows expected from LLM-based systems like ChatGPT. Colang bridges this gap by providing:- Natural Language-Like Syntax: Easy to read and write, even for non-programmers
- LLM-Friendly: Designed specifically for controlling LLM-based conversations
- Flexible Yet Structured: Supports both strict flows and flexible LLM-generated responses
- Event-Driven: Based on messages and events rather than rigid state machines
Two versions of Colang are supported: 1.0 and 2.0. Colang 1.0 is the default. This guide covers both versions.
Colang 1.0 vs 2.0
- Colang 1.0
- Colang 2.0
Current default version with a simpler, more declarative syntax:
- Uses
definekeyword for all definitions - All flows are active by default
- More implicit behavior
Core Concepts
Before diving into syntax, understand these fundamental concepts:Utterance
Utterance
The raw text coming from the user or the bot.Example:
"Hello, how are you?"Intent (Canonical Form)
Intent (Canonical Form)
The structured representation of a user/bot utterance.Example:
user express greeting for inputs like “hi”, “hello”, “hey there”Flow
Flow
A sequence of messages and events with optional branching logic.Flows define how conversations should unfold.
Action
Action
Custom code that the bot can invoke, usually for connecting to third-party APIs or performing computations.
Context
Context
A key-value dictionary of data relevant to the conversation.Stored in variables like
$user_name or $authenticatedColang 1.0 Syntax
User Messages
Define canonical forms that represent user intents:Bot Messages
Define what the bot should say for different intents:If multiple utterances are defined, one is chosen randomly for variety.
Variables in Bot Messages
You can include context variables in responses:$variable and Jinja-style {{ variable }} syntax are supported.
Flows
Flows define conversation sequences and logic:Branching with when
Handle different user responses:
Conditional Logic with if
Make decisions based on context variables:
Actions
Call custom Python functions:Complete Example (Colang 1.0)
Here’s a real example from the NeMo Guardrails codebase:Colang 2.0 Syntax
Colang 2.0 introduces significant syntax changes for more explicit control.Key Differences
No 'define' Keyword
Directly use
flow without define:Explicit Activation
Flows must be activated:
Past Tense for Events
User events use past tense:
Imperative for Actions
Bot actions use imperative:
User Intents (2.0)
Bot Responses (2.0)
The Generation Operator (...)
Colang 2.0 introduces the ... operator for LLM-generated content:
Main Entry Point (2.0)
Every Colang 2.0 script needs amain flow:
Complete Example (Colang 2.0)
From the ABC Bot example:Real-World Examples
Example 1: Jailbreak Detection
Example 2: PII Masking
Example 3: Fact Checking
Best Practices
Start Simple
Begin with basic flows and gradually add complexity as needed.
Use Subflows
Break complex flows into reusable subflows for maintainability.
Name Clearly
Use descriptive names for intents and flows:
user ask about refund policy not user intent 1.Test Incrementally
Test each flow individually before combining them.
Choosing a Version
Set the Colang version in yourconfig.yml:
Most existing examples and documentation use Colang 1.0. Start there unless you need 2.0’s advanced features.
Next Steps
Architecture Deep Dive
Learn how the Colang runtime processes events
Actions Guide
Create custom Python actions for your flows
Colang 1.0 Reference
Complete syntax reference for Colang 1.0
Colang 2.0 Reference
Complete syntax reference for Colang 2.0