Dialog Rails

This section explains how to create dialog rails using Colang.

Definition

Dialog Rails are a type of rails enforcing the path that the dialog between the user and the bot should take. Typically, they involve three components:

  1. The definition of user messages, which includes the canonical forms, e.g., user expressed greeting, and potential utterances.

  2. The definition of bot messages, which includes the canonical forms, e.g., bot express greeting, and potential utterances.

  3. The definition of flows “connecting” user messages and the bot messages.

Note

The definitions of user and bot messages are themselves flows that use other pre-defined flows, e.g., user said and bot say.

The example below extends the Hello World example by creating the user expressed greeting and bot express greeting messages.

examples/v2_x/tutorial/hello_world_2/main.co
 1import core
 2
 3flow main
 4  user expressed greeting
 5  bot express greeting
 6
 7flow user expressed greeting
 8  user said "hi" or user said "hello"
 9
10flow bot express greeting
11  bot say "Hello world!"

Note

The recommended practice is to use past tense for matching external actions, like the user saying something, and present for bot actions that must be executed. See Flow Naming Convention for more details.

LLM Integration

While the example above has more structure, it is still rigid in the sense that it only works with the exact inputs “hi” and “hello”.

To enable the use of the LLM to drive the interaction for inputs that are not matched exactly by flows, you have to activate the llm continuation flow, which is part of the llm module in the Colang Standard Library (CSL).

examples/v2_x/tutorial/hello_world_3/main.co
 1import core
 2import llm
 3
 4flow main
 5  activate llm continuation
 6  activate greeting
 7
 8flow greeting
 9  user expressed greeting
10  bot express greeting
11
12flow user expressed greeting
13  user said "hi" or user said "hello"
14
15flow bot express greeting
16  bot say "Hello world!"

Flow activation is a core mechanism in Colang 2.0. In the above example, the greeting dialog rail is also encapsulated as a flow which is activated in the main flow. If a flow is not activated (or called explicitly by another flow), it will not be used.

Note

When a flow is activated it will start to monitor the stream of events and drive the interaction whenever there is a match.

Testing

$ nemoguardrails chat --config=examples/v2_x/tutorial/hello_world_3

> hello there!

Hello world!

> how are you?

I am an AI, so I don't have feelings like humans do. But thank you for asking! Is there something specific you would like to know or talk about?

First, you can see how the user utterance “hello there!” is matched to the flow user expressed greeting based on its similarity to the expected user answers. Secondly, any unexpected user utterance like “how are you?” will trigger the LLM to generate a suitable response. This is all automatically handled and taken care of by the flow llm continuation. To have more explicit control over the interaction loop checkout the Interaction Loop example.

Hint

To get a better understand of what’s happening under the hood, you can use the --verbose flag when launching the NeMo Guardrails CLI to show all the precessed events:

$ nemoguardrails chat --config=examples/v2_x/tutorial/hello_world_3 --verbose

The next example will show you how to describe multimodal rails using Colang.