Introduction to Colang 2.0

Welcome to the first lesson of our course on Colang 2.0. Building on your knowledge of Colang 1.0, this lesson will highlight the key improvements and new features introduced in Colang 2.0, a version designed to further enhance the capabilities of NeMo Guardrails. With its more Pythonic syntax and improved structure, Colang 2.0 streamlines the process of creating efficient prompts and managing conversational flows. By the end of this lesson, you will understand the major differences from Colang 1.0 and be ready to leverage Colang 2.0 in your projects.

Configuration in Colang 2.0

First, let's cover how to enable Colang 2.0. In the familiar config.yml, we will need to add the key colang_version and set it to "2.x". Here's the configuration file we will be using in this course:

In this configuration:

  • colang_version specifies the version of Colang being used.
  • Under models, a list of models is defined. Each model has:
    • type: The role of the model, such as main.
    • engine: The engine powering the model, e.g., openai.
    • model: The specific model to be used, in this case, gpt-4o-mini.

This setup ensures that your application utilizes the specified model configurations effectively.

Basics of Colang 2.0

With Colang 2.0 properly configured and ready to go, it's time to cover the major differences. Colang 2.0 represents a significant evolution from its predecessor, introducing a more Pythonic syntax and structure. Let's explore the fundamental concepts, syntax, and terminology of this new version.

Drawing inspiration from Python, you might recognize some terms:

  • Script: A bit of Colang code
  • Module: A single .co file
  • Package: A folder of Colang files

Colang Standard Library (CSL):
Colang 2.0 comes with a built-in Colang Standard Library (CSL), which provides a collection of core flows and actions that help you build conversational logic more efficiently. By importing the CSL flows, you gain access to these reusable building blocks, making it easier to create robust and maintainable conversational applications.

Introducing the Main Flow

In Colang 2.0, the script starts with the main flow. This is the entry point for your conversational logic, similar to the main() function in Python. The main flow is the only flow enabled by default, and it is where you define the primary behavior of your bot.

Here’s a simple example of a main flow in Colang 2.0:

  • import core: This line imports the Fundamental Core Flows module from the CSL, giving you access to the required flows and actions.
  • flow main: This defines the main flow, which is the starting point of your conversation logic.
  • user said "hi": This is a trigger. When the user says "hi", the following action is executed.
  • bot say "Hello World!": This is the bot’s response to the user’s input.

This structure makes it easy to define how your bot should respond to specific user inputs, and the main flow ensures there is always a clear entry point for your conversational application.

Automatic Flow Restart

One important behavior in Colang 2.0 is that all activated flows (like the main flow) are automatically restarted after they finish. This means that once the flow reaches the end, it will start again from the top, allowing your bot to continuously handle new user inputs.

Consider the following example:

When this flow runs, the conversation will look like this:

After the bot says "Hello World!", the main flow restarts, and the bot says "Welcome!" again, waiting for the next user input. This automatic restart ensures that your bot is always ready to engage in a new conversation cycle.

Adding New Flows

To add a new flow, all you need to do is to declare it with flow followed with the canonical form (intent) of the action. For example:

Note: The user said and bot say constructs are themselves flows built on top of pre-defined flows from the CSL. It's good practice to use past tense for external (user) actions, and present tense for internal (bot) actions to be executed.

Flow Activation

Flow activation is a fundamental mechanism in Colang 2.0. In contrast to Colang 1.0, where all flows are active by default, all flows in Colang 2.0 are disabled. Only the main flow is activated by default, and all other flows must be explicitly activated for them to run.

Summary and Preparation for Practice

In this lesson, you learned about the key features and improvements in Colang 2.0, including its Python-inspired syntax, updated configuration, and the introduction of the Colang Standard Library (CSL). We covered how to set up your project, define the main flow as the entry point, and manage flow activation and restarts. With these foundational concepts, you are now prepared to apply Colang 2.0 in practice and build more robust conversational applications using NeMo Guardrails. Good luck!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal