Basics of building an Artificial Intelligence Chatbot – 2023

David Heredia October 23, 2023
Basics of building an Artificial Intelligence Chatbot – 2023

Chatbots have been gaining popularity over the years and can be seen on almost every website we visit. They are being increasingly used by businesses for customer support and are predicted to improve customer service for many industries in the coming years. And, of course, with AI in the picture, it only makes sense to introduce well-functioning chatbots. So, in this chatbot tutorial, we will talk about how you can also build an AI chatbot. Let us look at what we will be learning today!

Chatbots are not a recent development. They are simulations that 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. Joseph Weizenbaum created the first chatbot in 1966, named Eliza. It all started when Alan Turing published an article named “Computer Machinery and Intelligence” and raised an intriguing question, “Can machines think?” 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. Before looking into the AI chatbot, learn the foundations of artificial intelligence.

Today, almost all companies have chatbots to engage their users and serve customers by catering to their queries. 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 how to make a chatbot in PythonThe 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. The respective artificial intelligence solution broadly falls under two categories for each type of activity: “Data Complexity” or “Work Complexity”. These two categories can be further broken down into 4 analytics models: Efficiency, Expert, Effectiveness, and Innovation.

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

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

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

Typical chatbot architecture should consist of the following:

Please refer to the below figure to understand the architectural interface:

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. The corpus is usually huge data with many human interactions . 

Corpus can be designed using one of the following methods:

Following are the components of a corpus:

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

Algorithm for this text-based chatbot

Chatbot window

We have designed a function that enables the user to interact with a 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:

Evaluate or test the chatbot

There could be multiple paths using which we can interact and evaluate the built text bot.

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 that an inventory manager can use to query every time he/she wants to track the location of a product/s.

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 spelled under lower or upper cases.

Tokenization

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

It is a process of finding 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.

Generate BOW [Bag of Words]

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

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. 

Algorithm for this voice-based chatbot

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: expect input in the form of text and generate a speech as an output. Here we are choosing the English language and the speech’s pace as Normal.

The Listen function

We have used the speech recognition function to enable the computer to listen to what the chatbot user replies in the form of speech. We have created the following function, which will access your computer’s microphone and will listen until 15 seconds to recognize 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 to ensure no delay caused in breaking if nothing is spoken.

Chatbot window

We have designed a function that enables the user to interact with a 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:

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. 

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 the client itself is not clear regarding the requirement, ask questions to understand specific pain points and suggest the most relevant solutions. Having this clarity helps the developer to create genuine and meaningful conversations to ensure meeting end goals.

There is no common way forward for all the different types of purposes that chatbots solve. Designing a bot conversation should depend on the bot’s purpose. Chatbot interactions are categorized 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. 

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:

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 the first time, it must still 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. 

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 the

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. The bot will get better each time by leveraging the AI features in the framework.

This was an entry point for all who wished to use deep learning and python to build autonomous text and voice-based applications and automation. The complete success and failure of 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, and 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.

David Heredia

SHARE THIS POST