Customer Support Chatbots: Easier & More Effective Than You Think
Tags: python, machinelearning, tensorflow, tutorial
Published: 2019-07-29 21:25:30


At the end of this post, you'll be able to use Python to create your own chatbot with just a few commands. But first, you might want to understand why customer service chatbots are so hot right now.

Opinions are divided over the corporate use of chatbots: surprisingly, most people seem to love them (see below), while others find them annoying. What’s not up for debate is the cost savings and 24×7 customer service chatbots enable. 

Some key points to consider:

- The [top benefits of chatbots](http://www.drift.com/wp-content/uploads/2018/01/2018-state-of-chatbots-report.pdf) are 24/7 service, instant responses, and answers to simple questions.
- [63% of people prefer online bots](https://www.mindshareworld.com/sites/default/files/MINDSHARE_HUDDLE_HUMANITY_MACHINE_2016_0.pdf) to interacting with a company’s human resources.
- Chatbots will [power 85% of customer service](https://channels.theinnovationenterprise.com/articles/why-including-chatbots-planning-in-big-data-planning-is-becoming-the-need-of-the-hour) by 2020.
- Chatbots are expected to [cut business costs by $8 billion](https://www.impactbnd.com/blog/chatbots-for-2019-infographic) by 2022.

While the Machine Learning (ML) technology on which chatbots are based has come a long way, it can’t yet fully replicate natural language comprehension. But there are still many potential applications for chatbots where complete comprehension is not required. It’s becoming commonplace to pay a bill, check an account balance or, as shown in figure 1, track a package with a chatbot. More and more companies turn over simple, routine consumer interactions to bots. 

![](https://thepracticaldev.s3.amazonaws.com/i/xnfr792e5wmmtsnhehsn.png)

Figure 1: chatbot interaction for tracking package delivery

It’s now possible to complete most routine transactions without having to speak to a real person. Food companies like [Subway](http://www.prnewswire.com/news-releases/subway-launches-ordering-of-sandwiches-and-salads-on-facebook-with-a-bot-for-messenger-300441476.html), [Dominos](https://www.dominos.com.au/inside-dominos/technology) and [Starbucks](https://news.starbucks.com/press-releases/starbucks-debuts-voice-ordering) are all experimenting with letting people place orders using chatbots in Facebook Messenger (like MobileMonkey), or by verbally telling Amazon’s Alexa to order them a frappuccino. 

And advances in natural language processing are being used to great effect today in relatively simplistic virtual assistants like Amazon’s Alexa, Apple Siri and Microsoft’s Cortana. [Erica, a Bank of America chatbot](https://promo.bankofamerica.com/erica/) goes a step further, leveraging “predictive analytics and cognitive messaging” to provide financial guidance to their customers.

Of course, [chatbots aren’t infallible](https://www.theverge.com/2016/3/24/11297050/tay-microsoft-chatbot-racist), especially when it comes to things like problem-solving, intuition, and empathy, which is where human interaction can make a tremendous difference to the outcome for a customer. In these cases, chatbots can be used for routine interactions, and then escalate to a real person for more complex interactions. 

![](https://thepracticaldev.s3.amazonaws.com/i/8lev1dlshmwh3863v7wl.png)

Figure 2: chatbot escalation/de-escalation to a real person
 
###How Do Chatbots Work?
A chatbot is simply an application that is governed by Machine Learning (ML) models in conjunction with a set of specified rules. Like most applications, chatbots are comprised of an application layer, a database and a set of APIs. 

Chatbots simulate a real-life, two-way conversation when humans interact with it using either a chat or voice interface. Responses are based on the “knowledge” available to it at that point in time. If an interaction introduces a new concept, chatbots are typically trained to either deflect the conversation, or else escalate to a human operator. Logs of discussions between chatbots and customers are saved and dissected by researchers in order to retrain the chatbot. Thus, a chatbot’s interactions will gradually grow in scope and relevance.

There are generally three types of chatbots based on different classification methods and uses:

- Pattern Matching Bots – these bots parse an input phrase and try to match keywords or key phrases to an entry in its database. If found, the bot can then provide the corresponding response.
   - Best suited for FAQ responses to common customer inquiries
- Natural Language Understanding Bots – these bots link entities (such as “ordering” or “purchasing”) with context (such as pattern matched keywords like “pizza types”) with expectations, which are what the customer is expecting in response to their interaction, such as “a list of toppings.”
   - Best suited for simple transactional inquiries, such as ordering food
- Natural Language Processing (NLP) Bots – these bots convert the user’s speech or text into structured data through a series of processes that parse the language into grammatical entities (verbs, subjects, objects, etc), extract recognized words or phrases (such as product names), identify misspellings, and may even attempt sentiment analysis to understand the customer’s attitude.
   - Best suited to complex customer service interactions

While it sounds complex, you may be surprised to learn that implementing a fully functioning chatbot is significantly cheaper than building a cross-platform app, and substantially less expensive than the human alternative (i.e., customer service reps).

 
###Make Your Own Chatbot
Now that you understand the importance and utility of chatbots, you may be inspired to see if you can create one for yourself. Daniel Kukiela has a Python-based implementation of a [chatbot on Github](https://github.com/daniel-kukiela/nmt-chatbot) that you can try “out of the box” with just a few commands to setup your environment and clone the repo. 

The Requirements.txt is pretty basic, and includes:

- tensorflow-gpu>=1.4.0
   - Utilizes TensorFlow’s Neural Machine Translation (seq2seq) capabilities to perform natural language processing
- tqdm
   - Implements a progress bar
- colorama
   - For producing colored terminal text and cursor positioning
- regex
   - For parsing text
- python-Levenshtein
   - For manipulating strings, including determining string similarity, median and average
- requests
   - The de facto standard for making HTTP requests in Python

All of these packages are supported on the ActiveState Platform (except for the GPU-based  version of TensorFlow which is replaced with the standard CPU version). If you’d like to try making Kukiela’s chatbot, the easiest way to set up your development environment is to:

1. [Fork my “Chatbot” runtime environment] (https://platform.activestate.com/Pizza-Team/Chatbot/auto-fork) on the ActiveState Platform into your account (you’ll be prompted to create an account if you don’t already have one) 
2. [Install the State Tool] (sh <(curl -q https://platform.activestate.com/dl/cli/install.sh))
3. Install the runtime on your system with ActiveState’s State Tool, which will automatically:
   - Create a virtual environment, and then
   - Install the chatbot’s Python runtime into it (i.e., all 6 packages in Kukiela’s Requirements.txt plus their 50 dependencies)

To install the runtime, follow the instructions listed under the “Use via Command Line” heading. For example:

![](https://thepracticaldev.s3.amazonaws.com/i/lfwqusthu9n1gj4vmwh8.png)

Now that your chatbot project’s environment is ready to go, you can grab Kukiela’s code and use the included training data to give it a try. Just issue the following commands from your new virtual environment:

    $ git clone --branch v0.1 --recursive https://github.com/daniel-kukiela/nmt-chatbot.git
    $ cd nmt-chatbot/
    $ cd setup
    $ python3 prepare_data.py
    $ cd ..
    $ python3 train.py

And now the hard part: waiting. Eventually the chatbot will chew through the training data and you’ll have a trained demo that’s ready to deploy and test out. 

Happy chatting!

Got a Github project you’d like to simplify the setup for by offering a pre-built environment? Sign up for a [free ActiveState Platform account](https://platform.activestate.com/create-account) and see how easy it can be.