How to Build Python Chatbot with Rasa

In this Python ChatBot article we want to learn How to Build Python Chatbot with Rasa, so Chatbots are computer programs designed to simulate human conversation. they are becoming increasingly popular in customer service, ecommerce and other industries. building chatbot may seem like hard task, but with the right tools and a little bit of Python knowledge, it can be done quickly and easily. in this article we are going to learn building simple chatbot using Python and Rasa.

 

 

What is Rasa?

Rasa is an open source framework that allows you to build intelligent chatbots that can understand natural language and respond accordingly. it uses machine learning and natural language processing (NLP) to interpret user messages and generate appropriate responses.

 

 

So first of we need to install these libraries and we can use pip for the installation.

 

 

After installation you need to create new rasa Project, and you can use rasa init command in your terminal, after rasa init you will see this output in your terminal, and also i have not trained the model right, because when you are creating your Rasa project, it will ask you for model training, I have chosen No.

How to Build Python ChatBot with Rasa
How to Build Python ChatBot with Rasa

 

 

rasa init command will create several files and directories inside it, including:

  • data/nlu.yml: file where you can define the intents and examples for your chatbot
  • data/stories.yml: file where you can define the conversation flows for your chatbot
  • actions.py: Python file where you can define custom actions for your chatbot
  • config.yml: aonfiguration file where you can specify settings for your chatbot, such as the pipeline for your NLU and dialogue models

 

 

This will be the project structure after rasa init.

How to Build Python ChatBot with Rasa
How to Build Python ChatBot with Rasa

 

 

Once the project is set up, open the data directory and edit the nlu.yml file to add some training data for our chatbot:

These are just few example messages for each intent, but you can add more to improve the accuracy of your chatbot.

 

 

After that we need to define some actions that our chatbot can take in response to user input. Open actions.py file in the actions directory and replace the contents with the following code:

This is Python code defining an action called “ActionWeather” for Rasa chatbot.

action is triggered when the intent and the entity city are detected in the user’s message. it uses tracker object to get the value of the city entity, which represents name of city.

after that the code processes the information by using an API to get weather information for the specified city , and finally responds with message about the weather in the city using the “dispatcher.utter_message” method.

The “return []” statement at the end of the “run” method returns an empty list of events, which means that no further events will be executed after this action.

 

 

After that we need to define story that describes the conversation flow between the user and our chatbot. open the stories.yml file in the data directory and replace the contents with the following code:

 

 

 

Also you need add the action in endpoints.yml.

endpoints.yml is configuration file used by Rasa to define different endpoints that our bot can use. it includes endpoints for different components of bot such as server where the models are stored, server which runs custom actions, tracker store which is used to store conversations and the event broker which streams conversation events.

In our case we have added an action_endpoint to the file, which specifies the URL where our custom actions are hosted.

 

 

 

Now we need to train our model, you can use this command

 

 

 

After training you will see this output.

How to Build Python ChatBot with Rasa
How to Build Python ChatBot with Rasa

 

 

 

Now open two terminals, in the first one run the actions like this 

 

 

And in the second terminal run this code

 

 

Now you can ask the weather from ChatBot

How to Build Python ChatBot with Rasa
How to Build Python ChatBot with Rasa

 

 

 

Learn More on Python GUI

Leave a Comment