Conversational AI — Interactive Chatbot using Rasa and Python

Ananya Balaji
7 min readFeb 14, 2021

--

By Ananya Balaji

Much of artificial intelligence as we know it today is attributed to Alan Turing. His Turing Test is quite admirable and inspired me to probe into conversational robots or Chatbots.

Rasa, an open-source platform for conversational AI assistant, quickly caught my attention and I started to build my first Hello World Chatbot. As I was exploring, I quickly found the Python integration to be quite appealing. So, I decided to build a conversational assistant that can be even more sophisticated.

In this post, I’ll explain how you could build your own semi-intelligent Interactive Chatbot using Rasa, Python, Wolfram Alpha, and Wikipedia.

Conversational AI
Conversational AI — Interactive Chatbot

Step 1: Install Python

If you have not downloaded Python, use this link and follow the instructions: https://www.python.org/downloads/

For my setup, I‘m using Python 3.7 on Mac OS.

Step 2: Install Rasa

If you have not downloaded Rasa, use this link and follow the instructions: https://rasa.com/docs/rasa/installation/

For my setup, I used the virtual environments to set up Rasa. This is optional. You could use quick installation, but this helps me isolate my python projects. Tools like virtualenv and virtualenvwrapper provide isolated Python environments, which are cleaner than installing packages system-wide (as they prevent dependency conflicts).

Virtual Environment Setup

  1. Create a new virtual environment by choosing a Python interpreter and making a ./venv directory to hold it:
python3 -m venv ./venv

2. Activate the virtual environment:

source ./venv/bin/activate

3. Update pip3

(venv) %chatbot% pip3 install -U pip

4. Install Rasa

(venv) %chatbot% pip3 install rasa

5. You can create a new project by running:

(venv) %chatbot% rasa init

I chose the default options, but if you want to use a separate project name and directory, you could do so.

Rasa Initialization

You can choose the option to train. Once trained, you have an option to interact with the default bot on the command line. I chose to interact with the default bot.

Rasa Initial Interaction

Step 3: Rasa Configuration

Natural Language Understanding (nlu.yml)

Chatbots work by understanding the user’s intent based on the natural language test. We can guide the Rasa bot through the configuration in the nlu.yml file.

(venv) %chatbot% head -30 data/nlu.yml 
version: "2.0"
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: |
- good afternoon
- cu
- good by
- cee you later
- good night
- bye
- goodbye
- have a nice day
- see you around

Domain and Stories

Once we know the intent, the chatbot can respond with a text or perform additional actions. The responses are captured through “utterances” and actions explicitly. A custom action is an action that can run any code you want. This can be used to make an API call or to query a database for example.

The domain defines the universe in which your assistant operates. It specifies the intents, entities, slots, responses, forms, and actions your bot should know about. It also defines a configuration for conversation sessions. Here is a sample of domain.yml

(venv) %chatbot% head -30 domain.yml 
version: "2.0"
intents:
- greet
- goodbye
- affirm
- deny
- mood_great
- mood_unhappy
- bot_challenge
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"
utter_iamabot:
- text: "I am a bot, powered by Rasa."
(venv) %chatbot%

Stories

Stories are a type of training data used to train your assistant’s dialogue management model. Stories can be used to train models that can generalize to unseen conversation paths.

A story is a representation of a conversation between a user and an AI assistant, converted into a specific format where user inputs are expressed as intents (and entities when necessary), while the assistant’s responses and actions are expressed as action names. Here is a sample of the stories.yml file

(venv) %chatbot% head -30  data/stories.yml
version: "2.0"
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

Step 4: Wolfram Alpha and Wikipedia Integration

Wolfram Alpha Client Installation

First, you want to get your app id from https://products.wolframalpha.com/api/

Then we can use the Python Client built against the Wolfram|Alpha v2.0 API. Documentation can be found here: https://pypi.org/project/wolframalpha/

(venv) %chatbot% pip3 install wolframalpha
Collecting wolframalpha
Using cached wolframalpha-4.1.1-py3-none-any.whl (6.9 kB)
Collecting more-itertools
Using cached more_itertools-8.6.0-py3-none-any.whl (45 kB)
Collecting xmltodict
Using cached xmltodict-0.12.0-py2.py3-none-any.whl (9.2 kB)
Installing collected packages: xmltodict, more-itertools, wolframalpha
Successfully installed more-itertools-8.6.0 wolframalpha-4.1.1 xmltodict-0.12.0Step 5: Chatbot Invocation

Wikipedia Client Installation

The Wikipedia Python library makes it easy to access and parse data from Wikipedia. The Python library wraps the MediaWiki APIs. Additional documentation can be found here: https://pypi.org/project/wikipedia/

(venv) %chatbot% pip3 install wikipedia
Collecting wikipedia
Using cached wikipedia-1.4.0-py3-none-any.whl
Collecting beautifulsoup4
Using cached beautifulsoup4-4.9.3-py3-none-any.whl (115 kB)
Requirement already satisfied: requests<3.0.0,>=2.0.0 in ./venv/lib/python3.7/site-packages (from wikipedia) (2.25.1)
Requirement already satisfied: chardet<5,>=3.0.2 in ./venv/lib/python3.7/site-packages (from requests<3.0.0,>=2.0.0->wikipedia) (3.0.4)
Requirement already satisfied: certifi>=2017.4.17 in ./venv/lib/python3.7/site-packages (from requests<3.0.0,>=2.0.0->wikipedia) (2020.12.5)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in ./venv/lib/python3.7/site-packages (from requests<3.0.0,>=2.0.0->wikipedia) (1.26.3)
Requirement already satisfied: idna<3,>=2.5 in ./venv/lib/python3.7/site-packages (from requests<3.0.0,>=2.0.0->wikipedia) (2.10)
Collecting soupsieve>1.2
Using cached soupsieve-2.1-py3-none-any.whl (32 kB)
Installing collected packages: soupsieve, beautifulsoup4, wikipedia
Successfully installed beautifulsoup4-4.9.3 soupsieve-2.1 wikipedia-1.4.0

Step 6: Rasa changes to support smart lookup

  1. Extend Natural Language Understanding to handle searches (change data/nlu.yml) to include search intent
(venv) %chatbot% tail -8 data/nlu.yml
- intent: search
examples: |
- What
- Why
- How
- When
- Which
- Who is the

2. Change the story to include action to handle smart search capabilities

(venv) %chatbot% head -15 data/stories.yml
version: "2.0"
stories:- story: ask for info
steps:
- intent: greet
- action: utter_greet
- intent: search
- action: utter_looking_it_up
- action: action_search

3. Implement action_search function in Python

from typing import Any, Text, Dict, Listimport ssl
import wolframalpha
import wikipedia
# Wikipedia and Wolfram Alpha lookup
ssl._create_default_https_context = ssl._create_unverified_context
app_id = "VY9P3H-W4HA5HU8YU"
client = wolframalpha.Client(app_id)
class ActionSearch(Action):def name(self) -> Text:
return "action_search"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
qry = (tracker.latest_message)['text']
print(qry)
output = ""
res = client.query(qry)
if res['@success'] == 'true':
try:
output = next(res.results).text
except StopIteration:
print("No results")
# Wolfram was able to resolve question
if output == "":
print("Let's try wikipedia")
try:
output = wikipedia.summary(qry, sentences=1);
except wikipedia.exceptions.DisambiguationError as e:
print("Disambugiation error")
output = "Unable to disambiguate. Could be " + " ".join(e.options[0:3])
except wikipedia.exceptions.PageError as e:
print("Page error")
output = "Unable to process request"
print (output)
dispatcher.utter_message(text=output)
return []

4. Start action job to handle incoming requests

(venv) %chatbot%  rasa run actions --debug
2021-02-07 17:22:12 INFO rasa_sdk.endpoint - Starting action endpoint server...
2021-02-07 17:22:12 INFO rasa_sdk.executor - Registered function for 'action_hello_world'.
2021-02-07 17:22:12 INFO rasa_sdk.executor - Registered function for 'action_search'.
2021-02-07 17:22:12 INFO rasa_sdk.endpoint - Action endpoint is up and running on http://localhost:5055

5. Enable endpoints access by changing the endpoints.yml file

...
# Server which runs your custom actions.
# https://rasa.com/docs/rasa/custom-actions
action_endpoint:
url: "http://localhost:5055/webhook"

6. Train the model

(venv) %chatbot% rasa train
The configuration for policies and pipeline was chosen automatically. It was written into the config file at 'config.yml'.
2021-02-07 19:42:36 INFO rasa.model - Data (messages) for NLU model section changed.
Training NLU model...

7. Run the Rasa chatbot shell for the interactive bot

(venv) %chatbot% rasa shell                                             
2021-02-07 20:05:03 INFO rasa.model - Loading model models/20210207-194327.tar.gz...
2021-02-07 20:05:05 INFO root - Connecting to channel 'cmdline' which was specified by the '--connector' argument. Any other channels will be ignored. To connect to all given channels, omit the '--connector' argument.
2021-02-07 20:05:05 INFO root - Starting Rasa server on http://localhost:5005
2021-02-07 20:05:05 INFO rasa.model - Loading model models/20210207-194327.tar.gz...
2021-02-07 20:05:13 INFO root - Rasa server is up and running.
Bot loaded. Type a message and press enter (use '/stop' to exit):
Your input ->

8. Now let’s interact with the bot

Your input ->  Hi                                                                                                                 
Hey! How are you?
Your input -> What is the temperature in NJ?
Ok, let me check that...
31 °F (wind chill: 26 °F)
(1 hour 13 minutes ago)
Your input -> Who is the President of USA?
Ok, let me check that...
Joe Biden (from January 20, 2021 to present)
Your input -> Distance between New York and Tampa
Ok, let me check that...
1001 miles

Step 7: Conclusion

You have now created your own chatbot. Now that you have the outline of how the code should be written, feel free to add whatever requests you want!

Here I used the chatbot shell, but you can easily extend it to Facebook Messenger, Slack, Twillio, Google Hangouts, Webex, and more.

Have fun creating wonderful chatbots!

--

--