Skip to main content
This mini-project introduces the basics of dialogue management and shows how to combine a simple state machine with an LLM API.

Learning Objectives

  • Understand dialogue management in conversational systems
  • Learn how a chatbot keeps track of conversation state
  • Build a simple rule-based state machine
  • Integrate an LLM API for generating natural responses
  • Create a chatbot that can handle greetings, questions, help, and exit commands

1. What is a Conversational Chatbot?

A conversational chatbot is a program that interacts with users through natural language. A basic chatbot usually performs these steps:
For example:

2. Dialogue Management Basics

Dialogue management controls the flow of a conversation. The chatbot needs to answer questions such as:
  • What is the user trying to do?
  • What happened previously?
  • What state is the conversation currently in?
  • What should the chatbot do next?
For example:
A dialogue manager decides how the chatbot moves from one state to another.

3. What is a Dialogue State?

A dialogue state represents the current stage of a conversation. For this project, we can use these states: Example:

4. Rule-Based Intent Detection

A simple chatbot can detect user intent using keywords. Example:
This approach is called rule-based intent detection. It is simple and useful for controlling predictable conversation flows.

5. What is a State Machine?

A state machine is a system that moves between predefined states based on events or user input. Example:
In Python, we can represent the state like this:
Then update it based on the user’s intent:

6. Why Combine Rules with an LLM?

A rule-based chatbot is good at controlling the conversation. An LLM is good at generating natural and intelligent responses. By combining them:
The state machine controls what should happen, while the LLM helps generate how the chatbot should respond.

7. Project Structure

For this mini-project:
The .env file stores the API key. Example:
Install the required libraries:

8. Complete chatbot.ipynb Code

Step 1: Import Libraries


Step 2: Load the API Key

Make sure your .env file contains:

Step 3: Define the Chatbot States


Step 4: Create Intent Detection

This function uses simple rules to identify the user’s intent.

Step 5: Create the State Manager

This function updates the chatbot’s current state.

Step 6: Create the LLM Response Function

The chatbot sends the user’s question to the LLM.

Step 7: Add Rule-Based Responses

Some simple intents do not need an LLM call.

9. Main Chatbot Loop

Now combine the rule-based system, state machine, and LLM.

10. Complete Code


11. Example Conversation


12. Key Concepts Used

Dialogue Management

Controls the flow of the conversation and decides what the chatbot should do next.

Intent Detection

Identifies what the user wants.

State Machine

Tracks the current stage of the conversation.

Rule-Based System

Handles predictable commands without calling the LLM.

LLM Integration

Handles open-ended questions.

13. Project Flow