Chatbots Using Python and Rasa

Chatbots have become an essential communication tool for businesses to interact with customers, offering efficient and convenient interaction methods. Python, with its rich ecosystem of development resources, has emerged as a top choice for building chatbots. Rasa is a specialized open-source framework that focuses on creating conversational AI with natural language understanding capabilities.

In this article, we will explore chatbot development using Python and Rasa. We'll cover the process of defining a chatbot's purpose, training it to understand natural language, and fine-tuning its responses. With these powerful tools, developers can create custom chatbots that deliver seamless user experiences for customer service, e-commerce, or any other domain.

Getting Started with Rasa

Rasa is available as a Python package and can be installed using pip. First, ensure you have Python 3.7 or higher installed, then run the following command ?

pip install rasa

Once installed, create a new Rasa project using the rasa init command ?

rasa init --no-prompt

This command creates a new Rasa project with the following directory structure ?

myproject/
??? actions/
??? data/
?   ??? nlu.yml
?   ??? rules.yml
?   ??? stories.yml
??? models/
??? tests/
??? config.yml
??? credentials.yml
??? domain.yml
??? endpoints.yml
??? README.md

The actions folder contains Python scripts for custom actions. The data folder contains training data in YAML format for NLU (Natural Language Understanding), stories, and rules. The models folder stores trained models, and domain.yml defines the chatbot's capabilities.

Understanding Rasa Components

Before creating a chatbot, let's understand the key components ?

  • Intents: User's intentions (e.g., greet, ask_weather)
  • Entities: Important data extracted from user messages (e.g., names, dates)
  • Slots: Memory to store information during conversations
  • Actions: What the bot does in response to user input
  • Stories: Example conversations that train the dialogue model
  • Rules: Fixed responses for specific conditions

Creating a Simple Chatbot

Let's create a simple greeting chatbot. First, define the domain in domain.yml ?

version: "3.1"

intents:
  - greet
  - goodbye
  - affirm
  - deny
  - mood_great
  - mood_unhappy

entities:
  - name

slots:
  name:
    type: text
    influence_conversation: true

responses:
  utter_greet:
  - text: "Hey! How are you?"

  utter_cheer_up:
  - text: "Here is something to cheer you up:"
    image: "https://i.imgur.com/nGF1K8f.jpg"

  utter_did_that_help:
  - text: "Did that help you?"

  utter_happy:
  - text: "Great, carry on!"

  utter_goodbye:
  - text: "Bye"

actions:
- utter_greet
- utter_cheer_up
- utter_did_that_help
- utter_happy
- utter_goodbye

session_config:
  session_expiration_time: 60
  carry_over_slots_to_new_session: true

Adding Training Data

Create training data in data/nlu.yml ?

version: "3.1"

nlu:
- intent: greet
  examples: |
    - hey
    - hello
    - hi
    - hello there
    - good morning
    - good evening
    - moin
    - hey there
    - let's go
    - hey dude
    - goodmorning
    - goodevening
    - good afternoon

- intent: goodbye
  examples: |
    - cu
    - good by
    - cee you later
    - good night
    - bye
    - goodbye
    - have a nice day
    - see you around
    - bye bye
    - see you later

- intent: affirm
  examples: |
    - yes
    - y
    - indeed
    - of course
    - that sounds good
    - correct

- intent: deny
  examples: |
    - no
    - n
    - never
    - I don't think so
    - don't like that
    - no way
    - not really

- intent: mood_great
  examples: |
    - perfect
    - great
    - amazing
    - feeling like a king
    - wonderful
    - I am feeling very good
    - I am great
    - I am amazing
    - I am going to save the world
    - super stoked
    - extremely good
    - so so perfect
    - so good
    - so perfect

- intent: mood_unhappy
  examples: |
    - my day was horrible
    - I am sad
    - I don't feel very well
    - I am disappointed
    - super sad
    - I'm so sad
    - sad
    - very sad
    - unhappy
    - not good
    - not very good
    - extremly sad
    - so saad
    - so sad

Create conversation stories in data/stories.yml ?

version: "3.1"

stories:
- story: happy path
  steps:
  - intent: greet
  - action: utter_greet
  - intent: mood_great
  - action: utter_happy

- story: sad path 1
  steps:
  - intent: greet
  - action: utter_greet
  - intent: mood_unhappy
  - action: utter_cheer_up
  - action: utter_did_that_help
  - intent: affirm
  - action: utter_happy

- story: sad path 2
  steps:
  - intent: greet
  - action: utter_greet
  - intent: mood_unhappy
  - action: utter_cheer_up
  - action: utter_did_that_help
  - intent: deny
  - action: utter_goodbye

Add rules in data/rules.yml ?

version: "3.1"

rules:
- rule: Say goodbye anytime the user says goodbye
  steps:
  - intent: goodbye
  - action: utter_goodbye

- rule: Say 'please hold on' whenever the user asks for help
  steps:
  - intent: greet
  - action: utter_greet

Training the Chatbot

Train your chatbot using the following command ?

rasa train

This command trains machine learning models based on your training data and saves them to the models/ directory.

Testing the Chatbot

Test your chatbot using the Rasa shell ?

rasa shell

Example conversation ?

Your input -> hello
Hey! How are you?

Your input -> I'm great
Great, carry on!

Your input -> goodbye  
Bye

Running the Action Server

For custom actions, you need to run the action server in a separate terminal ?

rasa run actions

Then start the Rasa server ?

rasa shell

Comparison of Rasa vs Other Chatbot Frameworks

Feature Rasa Dialogflow ChatterBot
Open Source Yes No Yes
On-premise Deployment Yes Limited Yes
Machine Learning Advanced Good Basic
Customization High Medium Medium

Conclusion

Python and Rasa provide powerful tools for creating intelligent chatbots with natural language understanding capabilities. By defining intents, entities, stories, and actions, developers can build conversational AI that delivers excellent user experiences. Rasa's open-source nature and advanced ML capabilities make it an ideal choice for businesses seeking customizable chatbot solutions.

Updated on: 2026-03-27T08:57:37+05:30

517 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements