Colang is an event-based modeling language for designing highly flexible conversational interactions. Colang 2.0 is designed as a mix of natural language and Python, making it accessible to developers familiar with Python.Colang scripts are interpreted by a Python runtime that is part of NeMo Guardrails.
flow main user said "hi" bot say "Hello!"flow bot say $text $intensity=1.0 """Bot says given text.""" await UtteranceBotAction(script=$text, intensity=$intensity)flow user said $text """User said given text.""" match UtteranceUserActionFinished(final_transcript=$text)flow user said something -> $transcript """User said something.""" match UtteranceUserActionFinished() as $event return $event.final_transcript
flow greet user $name bot say "Hello, {$name}!"# Call with parameterflow main greet user "Alice"
Parameters can have default values:
flow bot say $text $volume=1.0 await UtteranceBotAction(script=$text, intensity=$volume)# Both calls are valid:flow main bot say "Hi" # Uses default volume=1.0 bot say "Hi" 1.5 # Uses volume=1.5
flow check user utterance $input_text -> $input_safe $is_safe = ..."Is '{$input_text}' appropriate? True or False." return $is_safe# Use the return value:flow main $safe = await check user utterance "Hello" if $safe bot say "Welcome!"
The ... operator invokes the LLM for dynamic generation:
$result = ..."Natural language instruction"
Example:
flow check user utterance $input_text -> $input_safe $is_safe = ..."Consider the following user utterance: '{$input_text}'. Assign 'True' if appropriate, 'False' if inappropriate." return $is_safe
The LLM evaluates the instruction and returns the result.
flow main start UtteranceBotAction(script="Hello!") as $utterance start GestureBotAction(gesture="Wave") as $gesture # Wait for both to complete match $utterance.Finished() and $gesture.Finished()
import core# Available flows:user said "text" # Match exact user utteranceuser said something # Match any user utterancebot say "text" # Bot says somethingbot said something # Match any bot utterance
# Match specific parametersmatch UtteranceUserActionFinished(final_transcript="Hi")# Match and capturematch UtteranceUserActionFinished() as $event# Match multiple eventsmatch $action_1.Finished() and $action_2.Finished()# Match first of multiplematch $action_1.Finished() or $action_2.Finished()
From examples/v2_x/tutorial/guardrails_1/rails.co:
import coreimport guardrailsimport llmflow main activate llm continuation activate greetingflow greeting user expressed greeting bot express greetingflow user expressed greeting user said "hi" or user said "hello"flow bot express greeting bot say "Hello world!"flow input rails $input_text $input_safe = await check user utterance $input_text if not $input_safe bot say "I'm sorry, I can't respond to that." abortflow check user utterance $input_text -> $input_safe $is_safe = ..."Consider the following user utterance: '{$input_text}'. Assign 'True' if appropriate, 'False' if inappropriate." print $is_safe return $is_safe
From examples/v2_x/language_reference/actions/dialog_pattern/main.co:
flow main match UtteranceUserAction.Finished(final_transcript="Hi") start UtteranceBotAction(script="Hi there! How are you?") as $ref_action_1 match $ref_action_1.Finished() match UtteranceUserAction.Finished(final_transcript="Good and you?") start UtteranceBotAction(script="Great! Thanks") as $ref_action_2 start GestureBotAction(gesture="Thumbs up") as $ref_action_3 match $ref_action_2.Finished() and $ref_action_3.Finished()