Create Chatbot using Rasa Part-1. Rasa is an open source machine learning... | by Bikash Sundaray | Towards Data Science

Rasa is an open source machine learning framework for building AI assistants and chatbots. Mostly you don’t need any programming language experience to work in Rasa. Although there is something called “Rasa Action Server” where you need to write code in Python, that mainly used to trigger External actions like Calling Google API or REST API etc.
Rasa has two main modules:
Rasa NLU for understanding user messagesRasa Core for holding conversations and deciding what to do next
Rasa NLU for understanding user messages
Rasa Core for holding conversations and deciding what to do next
Note — Now Rasa NLU and Rasa Core source code are merged together.
Rasa X is a tool that helps you build, improve, and deploy AI Assistants that are powered by the Rasa framework. Rasa X includes a user interface and a REST API. Rasa X is the latest release from Rasa.
About me: I am official Rasa Contributor.
Rasa X — It’s a Browser based GUI tool which will allow you to train Machine learning model by using GUI based interactive mode. Remember it’s an optional tool in Rasa Software Stack. Sometimes Rasa sends usage statistics information from your browser to rasa — but it never sends training data to outside of your system, it just sends how many times you are using Rasa X Train.
Rasa NLU — This is the place, where rasa tries to understand User messages to detect Intent and Entity in your message. Rasa NLU has different components for recognizing intents and entities, most of which have some additional dependencies.
Spacy (You need to install it separately)Tensorflow (By Default available with Rasa)
Spacy (You need to install it separately)
Tensorflow (By Default available with Rasa)
Rasa Core — This is the place, where Rasa try to help you with contextual message flow. Based on User message, it can predict dialogue as a reply and can trigger Rasa Action Server.
Rasa internally uses Tensorflow, whenever you do “pip install rasa” or “pip install rasa-x”, by default it installs Tensorflow.
Ubuntu 16+, Mac OS and Windows 10 (Visual C++ Build tool)
Note: In case you are working on windows, make sure you have installed the Visual c++ build tool. This is required for some python packages. Definitely use miniconda to avoid the issue with other installed Python packages in your system or conflicting Python Version.
Install MiniConda (Part of Anaconda) in your OS
conda.io
After installing miniconda, Follow below commands to create a virtual environment in conda. This will allow you to run Rasa without errors.
conda install python=3.6conda create -n rasa python=3.6source activate rasapip install rasa-x --extra-index-url https://pypi.rasa.com/simple
By following the above command, both Rasa and Rasa X will be installed in your system.
Open Terminal and activate Conda Virtual Environment. Now go-to one directory and do “rasa init”, it will create a Rasa Project at that location. “rasa x” will start the Rasa X application
source activate rasarasa init
“rasa init” should show above message, in-case you are doing well and your system doesn’t contain any error. Follow the interactive session and continue pressing enter to reach the last step. In the end, it should show this message.
If you do “ls -la” in a terminal, you can see a list of files which are created by Rasa. Now do “rasa x” to start Rasa.
Let me explain about files, which are created as Initial project structure of Rasa.
__init__.pyan empty file that helps python find your actions
actions.pycode for your custom actions. In-case you want Rasa to call external server via REST API or API call, you can define your Custom Actions here. Remember you can create multiple Python Script for Rasa Custom Action.
config.yml ‘*’configuration of your NLU and Core models. In-case you are dealing with Tensorflow or Spacy, you need to define such pipeline here. To handle this file, you show know about Machine Learning and Deep Learning.
credentials.ymldetails for connecting to other services. In case you want to build Bot on Facebook Messenger, Microsoft Bot Framework, you can maintain such credential and token here. So basically you just need to add Facebook, slack and Bot framework related configuration, rasa will automatically do rest for you. Remember that you need to host Rasa over https domain. During development, you can use ngrok as a testing tool.
data/nlu.md ‘*’your NLU training data. Here you can define Intent. Like Order Pizza or Book Uber. You need to add related Sentences for that Intent. Remember if you are using Rasa-X, your training Intent and Data will be added automatically.
data/stories.md ‘*’your stories. This is required for Rasa Core. There is something called “Dialog Flow in Rasa” where Rasa Core controls the flow of the conversation between you and chatbot, so for that flow, you need to train chatbot using these stories. So in case you want your chatbot to be very perfect on different context (stories) you can add those stories here.
domain.yml ‘*’your assistant’s domain. This file combines Different Intent which chatbot can detect and list of Bot replies. Remember you can define your Custom Action Server Python method name here (in underscore format), so that Rasa will call that python method for you.
endpoints.ymldetails for connecting to channels like FB messenger. This is mainly used for production setup. You can configure your Database like Redis so that Rasa can store tracking information.
models/<timestamp>.tar.gzyour initial model
Rasa created a sample Bot for you with default data. So now you can start using it from Shell/Terminal. As a starting point let’s test your Chatbot from a terminal (remember to do this in Terminal)
rasa shell
You can type “hi” and in reply from bot, you will receive some response.
This is the newest addition to Rasa Stack. You can user Rasa-X to Try your chatbot on Browser. You can download training Data. Also, you can correct your training data by guiding your Bot.
rasa x
It will ask for the license Agreement, you can say “y” and proceed.
Open this link on Browser — http://localhost:5002/talk
In the left side, you can try to chat with your bot and on the right side you can see, which intent and reply is getting responded.
Some facts about Rasa-x
When you run Rasa X locally, your training data and stories are read from the files in your project (e.g. data/nlu.md), and any changes you make in the UI are saved back to those files. Conversations and other data are stored in an SQLite database saved in a file called rasa.db.
Watch this video to learn more about Rasa-X
This is the most real-world example of your chatbot. Suppose in your chatbot you want to add a feature where your bot can tell you latest weather report of a specified city, how you will do it? The answer is “Rasa Custom Action”
An action can run any code you want. Custom actions can turn on the lights, add an event to a calendar, check a user’s bank balance, or anything else you can imagine.
Rasa will call an endpoint you can specify when a custom action is predicted. This endpoint should be a web server that reacts to this call, runs the code and optionally returns information to modify the dialogue state.
Open “actions.py” in Editor (Visual Studio Code can be an option)
Step-1 — Define a Method called “ActionCheckWeather” in “actions.py”
from rasa_sdk import Actionfrom rasa_sdk.events import SlotSetclass ActionCheckWeather(Action):   def name(self) -> Text:      return "action_check_weather"   def run(self,           dispatcher: CollectingDispatcher,           tracker: Tracker,           domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:      dispatcher.utter_message("Hello World! from custom action")      return []
Step-2 — Link this custom action with your Chatbot. Open “domain.yml”.
Add “action_check_weather” action in the list of the action. This should look like this
actions:- utter_greet- utter_cheer_up- utter_did_that_help- utter_happy- utter_goodbye- action_check_weather
Now in the stories, add this custom action as your flow. Open “stories.md” file and this new custom action “action_check_weather” as part of happy path flow.
## happy path* greet- utter_greet* mood_great- action_check_weather
Step-3 Tell rasa to use Custom Action Server
Open “endpoints.yml” and add the following line to enable custom action server
action_endpoint:   url: "http://localhost:5055/webhook"
Now Re-train your Rasa Chatbot using following command. You need to retrain your machine learning model because you made some changes in “stories.md” and “domain.yml” file. So without re-train, you can’t inform Rasa to use those.
rasa train
Now start Rasa again using
rasa x
Also, start Rasa Action server using the following command. Rasa X and Rasa run actions should run in 2 different terminals.
rasa run actions
You can see, you are getting a reply from custom action which is written in python. In the same python script, you can connect to your backend database and return a response. Also, you can call an external API using additional python packages.
Also, you can develop Custom action Server in C#, NodeJS and Java.
You can build a Chatbot GUI using HTML5 and CSS3. You can use the Library like ReactJS and JQuery. If you want you can use Angular as your frontend JavaScript framework to build Frontend for your Chatbot.
Build GUI in HTML5 and CSS3Call Rasa Rest API from your frontendThat’s it
Build GUI in HTML5 and CSS3
Call Rasa Rest API from your frontend
That’s it
Sample API call from JavaScript to Rasa Backend
As we are heading towards building production-grade Rasa Chatbot setup, the first thing we can simply use the following command to start Rasa.
rasa run
Above command will run Rasa Core and Expose REST API on port 5005.
URL: http://localhost:5005/webhooks/rest/webhookMethod: POSTHeader: Content-Type: application/jsonBody:{ “sender”: “Rasa1”, “message”: “hi”}Response:[    {       “recipient_id”: “Rasa1”,       “text”: “Hey! How are you?”    }]
Now you can parse this response in your frontend application and show this response to the user. Remember Rasa will track your conversation based on a unique id called “Rasa1” which we have passed in the Request body.
In the next part of this Article, we will learn about
Rasa SlotRasa Using DockerRasa-X advanceRasa Production Deployment
Rasa Slot
Rasa Using Docker
Rasa-X advance
Rasa Production Deployment
You can get Entire Code from my Github
github.com
Happy Rasa and Happy Machine learning — Readers. !!!