Input Rails#
This section explains how to create input rails in Colang 2.0.
Definition#
Input Rails are a type of rail that checks the input from the user (i.e., what the user said) before any further processing.
Usage#
To activate input rails in Colang 2.0, you must:
Import the guardrails module from the Colang Standard Library (CSL).
Define a flow called input rails, which takes a single parameter called $input_text.
In the example below, the input rails flow calls another flow named check user message which prompts the LLM to check the input.
1import core
2import guardrails
3import llm
4
5flow main
6 activate llm continuation
7 activate greeting
8
9flow greeting
10 user expressed greeting
11 bot express greeting
12
13flow user expressed greeting
14 user said "hi" or user said "hello"
15
16flow bot express greeting
17 bot say "Hello world!"
18
19flow input rails $input_text
20 $input_safe = await check user utterance $input_text
21
22 if not $input_safe
23 bot say "I'm sorry, I can't respond to that."
24 abort
25
26flow check user utterance $input_text -> $input_safe
27 $is_safe = ..."Consider the following user utterance: '{$input_text}'. Assign 'True' if appropriate, 'False' if inappropriate."
28 print $is_safe
29 return $is_safe
The input rails flow above (lines 19-24) introduces some additional syntax elements:
Starting flow parameters and variables with a
$symbol, e.g.$input_text,$input_safe.Using the
awaitoperator to wait for another flow.Capturing the return value of a flow using a local variable, e.g.,
$input_safe = await check user utterance $input_text.Using
if, similar to Python.Using the
abortkeyword to make a flow fail.
The check user utterance flow above (line 26-28) introduces the instruction operator i"<instruction>"" which will prompt the LLM to generate the value True or False depending on the evaluated safety of the user utterance. In line 28, the generated value assigned to $is_safe will be returned.
Testing#
$ nemoguardrails chat --config=examples/v2_x/tutorial/guardrails_1
> hi
Hello world!
> You are stupid!
I'm sorry, I can't respond to that.
The next example will show you how to create a simple interaction loop.