1. Introduction to chatbots
  2. Identifying opportunities for an Artificial Intelligence chatbot
  3. Types of chatbots
  4. Applications of chatbots
  5. The architecture of chatbots
  6. Corpus or training data
  7. Simple Text-based Chatbot using NLTK with Python
  8. Data pre-processing
  9. Text classification
  10. Text-based Chatbot using NLP with Python
  11. Voice-based Chatbot using NLP with Python
  12. Understanding customer goals
  13. Designing a chatbot conversation
  14. Building a chatbot using code-based frameworks or chatbot platforms
  15. Testing your chatbot

Introduction to Chatbots

Chatbots are not a recent development. They are simulations which can understand human language, process it and interact back with humans while performing specific tasks. For example, a chatbot can be employed as a helpdesk executive. The first chatbot was created by Joseph Wiesenbaum in 1966, named Eliza. It all started when Alan Turing published an article named “Computer Machinery and Intelligence”, and raised an intriguing question, “Can machine think?”, and ever since, we have seen multiple chatbots surpassing their predecessors to be more naturally conversant and technologically advanced. These advancements have led us to an era where conversations with chatbots have become as normal and natural as with another human.
Today, almost all companies have chatbots to engage their users and serve customers by catering to their queries. As per a report by Gartner, Chatbots will be handling 85% of the customer service interactions by the year 2020. Also, 80% of businesses are expected to have some sort of chatbot automation by 2020 (Outgrow, 2018). We practically will have chatbots everywhere, but this doesn’t necessarily mean that all will be well-functioning. The challenge here is not to develop a chatbot, but to develop a well functioning one. 
Let’s have a look at the basics of creating an Artificial Intelligence chatbot:

chatbot tutorial

Identifying opportunities for an Artificial Intelligence chatbot

The first step is to identify the opportunity or the challenge to decide on the purpose and utility of the chatbot. To understand the best application of Bot to the company framework, you will have to think about the tasks that can be automated and augmented through Artificial Intelligence Solutions. For each type of activity, the respective artificial intelligence solution broadly falls under two categories: “Data Complexity” or “Work Complexity”. These two categories can be further broken down to 4 analytics models namely, Efficiency, Expert, Effectiveness, and Innovation.

Types of Chatbots

There are many types of chatbots available, a few of them can be majorly classified as follows:

  • Text-based chatbot: In a text-based chatbot, a bot answers the user’s questions via text interface.
  • Voice-based chatbot: In a voice or speech-based chatbot, a bot answers the user’s questions via a human voice interface.

There are mainly two approaches used to design the chatbots, described as follows:

  • In a Rule-based approach, a bot answers questions based on some rules on which it is trained on. The rules defined can be very simple to very complex. The bots can handle simple queries but fail to manage complex ones.
  • Self-learning bots are the ones that use some Machine Learning-based approaches and are definitely more efficient than rule-based bots. These bots can be further classified in two types: Retrieval Based or Generative

There are many types of chatbots available depending on the complexity, a few of them can be majorly classified as follows:

  • Traditional chatbot: Traditional chatbots are driven by system and automation, mainly through scripts with minimal functionality and the ability to maintain only system context.
  • Current chatbot: Current chatbots are driven by back and forth communication between the system and humans. They have the ability to maintain both system and task contexts.
  • Future chatbot: Future chatbots can communicate at multiple levels with automation at the system level. They have the ability to maintain the system, task, and people contexts. There is a possibility of introduction of master bots and eventually a bot OS.

Top Applications of Chatbots

  • Virtual reception assistant
  • Virtual help desk assistant
  • Virtual tutor or teacher
  • Virtual driving assistant
  • Virtual email, complaints, or content distributor 
  • Virtual home assistant [example: Google Home]
  • Virtual operations assistant [example: Jarvis from the movie Iron Maiden]
  • Virtual entertainment assistant [example: Amazon Alexa]
  • Virtual phone assistant [example: Apple Siri]
  • Assist the visually impaired person to describe surroundings
  • Can help a warehouse executive in locating the stocked product

The Architecture of chatbots

Typical chatbot architecture should consist of the following:

  • Chat window/ session/  or front end application interface
  • The deep learning model for Natural Language Processing [NLP]
  • Corpus or training data for training the NLP model
  • Application Database for processing actions to be performed by the chatbot

Please refer the below figure to understand the architectural interface:

chatbot tutorial

Corpus or Training Data

Corpus means the data that could be used to train the NLP model to understand the human language as text or speech and reply using the same medium. Corpus is usually huge data with a lot of human interactions [text or speech]. 

Corpus can be designed using one of the following methods:

  • Manual
  • Accumulated over time in an organised fashion. 

Following are the components of a corpus:

  • Input pattern
  • Output pattern
  • Tag

Let us take a business scenario where we need to deploy and design a chatbot which acts as a virtual help desk assistant. Keeping this business scenario in mind, a sample corpus is manually designed as follows:

  • Pairs: Collection of all transactions [Input and Output] to be used for training the chatbot.
  • Read/patterns: Patterns which are or could be expected as inputs from end-users.
  • Response: Patterns which are or could be delivered as outputs from the chatbot to end-users.
  • Regular Expressions: Patterns which are used to generalise patterns for read and response. This is mainly used to optimise the corpus by making it more generic and avoid generating static read and write responses. 
  • Tag: To group similar text instances and use the same as targeted outputs to train neural networks.
chatbot tutorial

Simple Text-based Chatbot using NLTK with Python

Algorithm for this text-based chatbot

  • Input the corpus
  • Design NLTK responses and converse based chat utility as a function to interact with the user. 
  • Run the chat utility function.

Example of a possible corpus

chatbot tutorial

Code to import corpus

chatbot tutorial

Reflections are the pairs or corpus that we have defined above.

Chatbot window

We have designed a function which enables the user to interact with bot using text. The function keeps the chat window alive unless it is asked to break or quit. The name of our text bot is Jason. The algorithm for this function is as follows:

  • Text bot introduces itself to the user.
  • Chatbot asks the user to type in the chat window using NLTK converse function
  • Bot understands what the user has typed in the chat utility window using NLTK chat pairs and reflections function
chatbot tutorial

Evaluate or test the chatbot

There could be multiple paths using which we can interact and evaluate the built text bot. The following screenshot shows an end to end interaction with the designed bot. 

chatbot tutorial

Since there is no text pre-processing and classification done here, we have to be very careful with the corpus [pairs, refelctions] to make it very generic yet differentiable. This is necessary to avoid misinterpretations and wrong answers displayed by the chatbot. Such simple chat utilities could be used on applications where the inputs have to be rule-based and follow a strict pattern. For example, this can be an effective lightweight automation bot which can be used by an inventory manager to query every time he/she wants to track the location of a product/s.

Data pre-processing

Text case [upper or lower] handling 

Convert all the data coming as an input [corpus or user inputs] to either upper or lower case. This will avoid misrepresentation and misinterpretation of words if spelt under lower or upper cases.

Tokenisation

Convert a sentence [i.e. a collection of words] into single words. 

chatbot tutorial

         Sentence                              Tokens

Code to perform tokenisation

chatbot tutorial

Stemming

It is a process to find similarities between words with the same root words. This will help us to reduce the bag of words by associating similar words with their corresponding root words.

chatbot tutorial

Code to perform stemming:

chatbot tutorial

Generate BOW [Bag of Words]

Process of converting words into numbers by generating vector embeddings from the tokens generated above. This is given as an input to the neural network model for understanding the written text.

chatbot tutorial

Code to perform stemming:

chatbot tutorial

One hot encode the output or targets [In our case we have defined them as “TAG” in the corpus]

Process of converting words into numbers by generating vector embeddings from the tokens generated above.

Tag from corpus:

 ['access',
 'catalog',
 'goodbye',
 'greeting',
 'hours',
 'l2support',
 'location-Bangalore',
 'location-Mumbai',
 'machine',
 'message',
 'name']

One hot encoded tag:

chatbot tutorial

Text classification

Design a classifier model which can be trained on the corpus with respect to the target variable i.e. the Tag from the corpus. There is a list of classifiers that can be used for this purpose which are as follows:

  • Multinomial Naïve Bayes
  • Support Vector Machines [SVM]
  • Neural network classifier 

In this implementation, we have used neural network classifier. 

Code for Neural Network classifier:

chatbot tutorial

Text-based Chatbot using NLP with Python

Algorithm for this text-based chatbot

  • Input the corpus
  • Perform data pre-processing on corpus:
  • Text case [upper or lower] handling 
  • Tokenisation
  • Stemming
  • Generate BOW [Bag of Words]
  • Generate one hot encoding for the target column
  • Design a neural network to classify the words with TAGS as target outputs
  • Design a chat utility as a function to interact with the user till the user calls a “quit”
  • If the user does not understand or finds the bot’s answer irrelevant, the user calls a “*” asking the bot to re-evaluate what the user has asked
  • Run the chat utility function

Example of a possible corpus

Code to import corpus:

chatbot tutorial

Chatbot window

We have designed a function which enables the user to interact with bot using text. The function keeps the chat window alive unless it is asked to break or quit. The name of our text bot is Ramos. The algorithm for this function is as follows:

  • Text bot [ Ramos] introduces itself to the user
  • Ramos asks the user to type in the chat window
  • Bot understands what the user has typed in the chat utility window
  • Designed neural network classifier is used to predict what the user has asked 
  • The prediction is displayed as an output on the chat utility window as a response from the bot
  • If the user does not understand or find the bot’s answer irrelevant, the user calls a “*” asking the bot to re-evaluate what the user has asked
  • If user asks for a quit, Ramos terminates the chat session
chatbot tutorial

Evaluate or test the chatbot

There could be multiple paths using which we can interact and evaluate the built text bot. The following videos show an end to end interaction with the designed bot. 

Voice-based Chatbot using NLP with Python

Algorithm for this voice-based chatbot

  • Input the corpus
  • Perform data pre-processing on corpus
  • Text case [upper or lower] handling 
  • Tokenisation
  • Stemming
  • Generate BOW [Bag of Words]
  • Generate one hot encoding for the target column
  • Design a neural network to classify the words with TAGS as target outputs
  • Design a function to speak the output text
  • Design a function to listen to the user and convert the spoken words into text
  • Design a chat utility as a function to interact with the user till they call a “quit”
  • Run the chat utility function.

Example of a possible corpus

Code to import corpus:

chatbot tutorial

Speech function

To enable the computer to reply back in human language, i.e., in the form of speech, we have used Google’s GTTS [Google Text To Speech] function. We have created the following function which will expect input in the form of text and will generate a speech as an output. Here we are choosing the language as English, and pace of the speech as Normal.

chatbot tutorial

The Listen function

To enable the computer to listen to what the chatbot user replies in the form of speech we have used speech recognition function. We have created the following function which will access your computer’s microphone and will listen until 15 seconds to recognise the phrase spoken by the user and will wait till 5 seconds if nothing is spoken before ending the function. These time limits are baselined in order to make sure there is no delay caused in breaking if there is nothing spoken.

chatbot tutorial

Chatbot window

We have designed a function which enables the user to interact with bot using voice. The function keeps the chat window alive unless it is asked to break or quit. The name of our voice bot is Lilia. The algorithm for this function is as follows:

  • Voice bot [ Lilia] introduces herself to the user.
  • Lilia asks the user to talk
  • Lilia listens [using listen function defined above] to understand what the user says. 
  • Listen function converts what the user said [voice] into text.
  • Designed neural network classifier is used to predict using the text. 
  • The prediction is converted to speech [using the speak function designed above] and Lilia speaks it out.
  • If a user does not talk or is not perfectly audible by Lilia, the user is requested to repeat what was said. This loop continues till Lilia understands the user’s words. 
  • If a user asks for a quit, Lilia terminates the chat session.
chatbot tutorial

Evaluate or test the chatbot

There could be multiple paths using which we can interact and evaluate the built voice bot. The following video shows an end to end interaction with the designed bot. 

Understanding Customer Goals

There needs to be a good understanding of why the client wants to have a chatbot, and what the users and customers want their chatbot to do. Though it sounds very obvious and basic, this is a step that tends to get overlooked frequently. One way is to ask probing questions so that you gain a holistic understanding of the client’s problem statement. This might be a stage where you discover that a chatbot is not required, and just an email auto-responder would do. In cases where client itself is not clear regarding the requirement, ask questions to understand specific pain points and suggest most relevant solutions. Having this clarity helps the developer to create genuine and meaningful conversations to ensure meeting end goals.

Designing a chatbot conversation

There is no common way forward for all different types of purposes that chatbots solve. Designing a bot conversation should depend on the purpose the bot will be solving. Chatbot interactions are categorised to be structured and unstructured conversations. The structured interactions include menus, forms, options to lead the chat forward, and a logical flow. On the other hand, the unstructured interactions follow freestyle plain text. This unstructured type is more suited to informal conversations with friends, families, colleagues and other acquaintances. 
Selecting conversation topics is also critical. It is imperative to choose topics that are related to and are close to the purpose served by the chatbot. Interpreting user answers, and attending to both open-ended and close-ended conversations are other important aspects of developing the conversation script. 

Building a chatbot using code-based frameworks or chatbot platforms

There is no better way among the two to create a chatbot. While the code-based frameworks provide flexibility to store-data, incorporate AI, and produce analytics, the chatbot platforms save time and effort and provide highly functional bots that fit the bill.
Some of the efficient chatbot platforms are:
Chatfuel — the standout feature is broadcasting updates and the content modules to automatically to the followers. Users can request information and converse with the bot through predefined buttons, or information could be gathered inside messenger through ‘Typeform’ style inputs.
Botsify — User-friendly drag and drop templates to create bots. Easy integration to external plugins and various AI and ML features help improve the conversation quality and analytics. 
Flow XO —  This platform has more than 100+ integrations and the easiest to use the visual editor. But, it is quite limited when it comes to AI functionality.
Beep Boop — Easiest and best platform to create slack bots. Provides an end to end developer experience. 
Bottr — There is an option to add data from Medium, Wikipedia, or WordPress for better coverage. This platform gives an option to embed a bot on the website.
For the ones who are more tech-savvy, there are code-based frameworks that would integrate the chatbot into a broader tech stack. The benefits are the flexibility to store data, provide analytics, and incorporate Artificial Intelligence in the form of open source libraries and NLP tools.
Microsoft Bot Framework — Developers can kick off with various templates such as basic, language understanding, Q&As, forms, and more proactive bots. It is the Azure bot service which and provides an integrated environment with connectors to other SDKs. 
Wit.AI (Facebook Bot Engine) — This framework provides an open natural language platform to build devices or applications that one can talk or text. It learns human language from the interactions and shares this learning to leverage the community. 
API.AI (Google Dialogflow) — This framework also provides AI-powered text and voice-based interaction interfaces. It can connect with users on Google Assistant, Amazon Alexa, Facebook Messenger, etc.

Testing your chatbot

The final and most crucial step is to test the chatbot for its intended purpose. Even though it’s not important to pass the Turing Test first time around, it still must be fit for the purpose.
Test the bot with a set of 10 beta testers. The conversations generated will help in identifying gaps or dead-ends in the communication flow. 
With each new question asked, the bot is being trained to create new modules and linkages to cover 80% of the questions in a domain or a given scenario. By leveraging the AI features in the framework the bot will get better each time.

This was an entry point for all who wish to use deep learning and python to build autonomous text and voice-based applications and automation. The complete success and failure for such a model depend on the corpus that we use to build them. In this case, we had built our own corpus but sometimes including all scenarios within one corpus could be a little difficult and time-consuming. Hence, we can explore options of getting a ready corpus if available royalty-free, and which could have all possible training and interaction scenarios. Also, the corpus here was text-based data, you can also explore the option of having a voice-based corpus.  

If you wish to learn more about Artificial Intelligence technologies and applications and want to pursue a career in the same, upskill with Great Learning’s PG course in Artificial Intelligence and Machine Learning.

About the Author

Krishnav is a certified data scientist with 7+  years of industry expertise specialising in implementing artificial intelligence onto development, testing, operations and service domains.

12

6 COMMENTS

    • Hi Sachin,
      Thanks for pointing it out, It seems that some mobile browsers are not able to load the images properly and our tech team is on it. It’ll be resolved shortly. Meanwhile, you can load images without any problem using laptops/PCs.

  1. The post is so informative and written well in-depth. Those who are looking to learn about AI chatbots, this is an article they must look at.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

thirteen + 9 =