Input Rails
This section explains how to create input rails in Colang 2.0
Definition
Input Rails are a type of rails that check 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 have to:
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 called 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) introduce some additional syntax elements:
Flow parameters and variables, start with a
$
sign, e.g.$input_text
,$input_safe
.Using the
await
operator 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
abort
keyword to make a flow fail, as opposed to finishing successfully.
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.