How to make your first chat bot in 50 lines of code — theory and practice


Most of fashionable chat bot platforms consist of Three primary issues — intent recognition , slot filling and dialog graph.

1.1 Intent recognition

Intent recognition is a textual content classification job which objective is to seize particular intent behind a consumer question. That is motivated by the truth that customers have a tendency to formulate their request in lots of alternative ways so we’d like to have a system that’s ready to inform if these messages relate to the identical factor or not. Let’s illustrate this with an instance of a financial institution chat bot, the place customers can ask the bot to withdraw cash:

Visualization of intent detection system

You may see that though the requests are formulated in lots of alternative ways and in totally different kinds all of them imply mainly the identical factor and chat bot ought to react in the identical manner. Due to this fact we’d like textual content classification mannequin that captures the semantics behind consumer sentences and assigns them to the particular predefined class.

1.2 Slot filling

As soon as we all know what motion the consumer needs to take we’d like to seize particular parameters of these actions. For instance, if you’d like Alexa to play your favourite track, you need her to play this particular track not simply any track, so apart from detecting intent chat bots additionally want to carry out a job that known as slot-filling.

1.Three Dialog graph

Visualization of dialog graph

One other requirement for chat bot performance is dialog graph. It’s objective is to steer dialog in the best route. For instance if you say “Test the climate” the chat bot might then ask “What day ought to I examine the climate for?” and subsequent will probably be searching for intents like ‘tomorrow’ or ‘right now’. The essential half right here is, there can be no level in asking the second query with out the first one, so there’s a want for a system that shops the knowledge of the purpose in the dialog the place we’re and what are the doable subsequent states.

1.Four Our chat bot

On this tutorial our objective is to create a easy chat bot, so we’re going to focus solely on intent detection job and easy dialog graph mannequin. That is sufficient to make a chat bot that’s ready to reply FAQ and conduct as easy dialog.

Our objective in designing an intent detection is to create a system that, given a number of examples for intent, can detect {that a} sentence given by the consumer is analogous to these examples and due to this fact ought to have the identical intent.

The issue behind this technique is that we’ve to design a system for checking if 2 sentences are comparable. This could possibly be achieved by eg. counting what number of overlapping phrases are in the brand new sentence and the sentences in coaching information set. That is nevertheless a naive method as a result of a consumer can use a phrase that has comparable that means, however is totally different from those in the practice examples.

2.1 Phrase embedding

An answer right here is to use phrase embedding.

Phrase vectors ref :

Phrase embeddings are mathematical representations of phrases encoded as vectors in n-dimentional house. Related (used in the identical context) phrases are shut to one another in this house. Which means that we will examine 2 or extra phrases to one another not by e.g. the quantity of overlapping characters however by how shut they’re to one another in they embedded type.

2.2 Sentence embeddings

From phrase embeddings we will assemble embeddings for the entire sentence. This may be completed in a spread of methods, we will merely take the typical of the phrase vectors, use weighted common to examine how essential the phrases are by e.g tf-idf coefficient and even use extra superior strategies like transformer neural networks.

2.Three Similarity

As soon as we’ve ready embeddings for the sentences we’ve to design a manner for evaluating them. A easy extensively used methodology right here is cosine similarity that measures similarity between two vectors because the angle between them.

Cosine similarity ref:

To create the sentence embedding we’re going to use aptitude library. This library is predicated not solely on static phrase embeddings but additionally analyses the phrases character by character which helps in coping with out-of-vocabulary phrases.

In our mannequin we’re going to embed the examples for every intent and then, whereas processing the customers message, discover probably the most comparable one. This method is especially taken as quick and easy one, illustrating how embedding work. Most of fashionable techniques use neural networks (hyperlink to associated articles will be discovered on the finish), nevertheless this method can nonetheless be used if you’d like to design a system that’s quick and and doesn’t use lots of sources.

We start our program with creating the define of the mannequin.

define of this system


1–9 : importing vital libraries
11 : initialization of the aptitude mannequin for creating embeddings of sentences. We’re utilizing English phrase embeddings and imply polling methodology for creating sentence embeddings from phrase embeddings.
13–20 : chatbot class, this class has two static strategies one for creating embeddings and one for processing consumer message and answering it.

3.1 Making ready embeddings

Firstly we’d like to put together a file containing our intents and their examples. It is a json dictionary that makes use of intents as keys and tables of examples as values.

intents.json file

Subsequent we’d like to to create a operate that constructs embeddings for the examples.

Perform for crating embeddings


4 : Creating new python dictionary for the embeddings
5–6 : Opening the enter file and loading it to python dictionary
7–8 : For every intent we create a desk in the embeddings dictionary
9–12 For every instance in the intent, we create a Aptitude sentence object that we will later embed utilizing the mannequin specified earlier. Lastly we add the embedded sentence to the desk
13–14: If the file doesn’t exist, we create it
15: We save the embedded dict. We use pickle as an alternative of json to retailer the numpy arrays

3.2 Answering the message

solutions.json file
operate for for answering messages


3 : We use the embeddings mannequin
4 -5 : We load load the embeddings file created earlier
6–8 : Embedding of consumer message
9–10 : Initializing finest intent and finest sore variables
11–16 For every intent we loop by means of it’s embedded examples and examine the cosine similarity between customers message and these examples.
We selected the intent, which instance has the best similarity with the brand new message
17–18 : Loading the solutions dict
19 : Checking if intent chosen by the system is in the solutions dict
20 : Return random reply from those assigned to the chosen intent

On this format the chat bot has to select one of the intents supplied. This implies we’ve no manner of detecting if consumer mentioned one thing that does not belong to any of the intents. A doable answer is to examine the numerical values of the cosine similarity and based mostly on these commentary assign a threshold worth under which the bot will classify the message because the one it doesn’t understand how to reply.


Please enter your comment!
Please enter your name here