Input Rails

View as Markdown

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:

  1. Import the guardrails module from the Colang Standard Library (CSL).
  2. 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.

examples/v2_x/tutorial/guardrails_1/main.co

import core
import guardrails
import llm
flow main
activate llm continuation
activate greeting
flow greeting
user expressed greeting
bot express greeting
flow 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."
abort
flow 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

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 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.

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.