Technological progress and automation are starting to influence numerous spheres of human economy and everyday life. The rapid development of artificial intelligence imposes training computers to do the human work and implement their usage in business. One of the main applications of artificial intelligence in business is the chatbot.
NLP together with chatbots have great potential in the area of customer service, and can easily accept the customer’s order and give them a consultation regarding the company’s services.
It is possible to automate the work of support centers with the help of bots on the official website of the company or in popular messengers like Telegram, Slack or Facebook Messenger.
In this article, we shall give you a brief tutorial about chatbot development, and share our experience in building a Telegram chatbot in Python.
Nowadays, NLP has become a topic of high importance since it makes sense of unstructured text data. It can be used in commerce, for example, and creating intelligent bots who can classify customers according to their requests, interests and characteristics, and handle millions of requests simultaneously.
Our project allows creating the chatbots who are able to analyze a real-time dialogue between the client and the consultant. It is possible to teach a bot in order to improve the quality of the answers and to train him to process more case-by-case situations.
Online consulting implies face-to-face consultation with the client and influence him as a potential buyer. In order to do this, the consultant should know the customer’s profile (interests, vocabulary, and psychological type). The majority of online dialogues is handled via phone calls or messages. The application has several libraries for understanding the human voice and transforming it into text data.
According to this, the idea of the project is building an interactive system capable of self-learning while communicating with the customers by classifying and processing their lexical forms. The main aim of our AI bot is to identify entity-clusters — groups of related entities. Entity-clusters can be very useful for many activities. For example, we can analyze customer requests and identify or even sort them by interests groups.
Frontend:
The project’s architecture is based on Telegram and its backend part written in Python programming language. Since the web-client is the main component of the project, one of the most important decisions was to choose the suitable framework for the realization of the web-application. There are several “full-stack” frameworks for Python language: Django, Grok, web2py, Giotto.
The application is written in Python programming language in order to unite the process of development and machine learning. Django and Web2py frameworks were chosen for the development. In order make a Telegram bot and integrate it with Telegram services, Telegram Bot API was used.
In order to realize the AI part of the project, the following Python libraries were used:
The key point in choosing the tools is selecting the library for machine learning. In terms of morphological analysis, we decided to choose a Pyromorphy2 one since it supports the Russian language. TeleBot library was chosen for using of Telegram Bot API. The client-side realization of web sockets is handled via SocketIO library. Flask-SocketIO library is used for establishing a real-time client-server communication. It is a tool for using a SocketIO library in connection with Flask — a convenient framework for working with web-sockets.
In order to create an AI-based bot, it is necessary to use the database of questions and answers. The time gap between the client’s inquiry and the bot’s reply should be as small as possible. In order to achieve this goal, we decided to choose Redis — a highly productive non-relational database. The information about questions and answers is stored as blocks of messages from the conversation. The advantages of such approach are:
Since the main goal of the development was creating a real-time application, web-sockets technology was used. They will imply the interactive connection allowing real-time client-server interaction. In contrast to HTTP/HTTPS, web-sockets are able to work with bidirectional flow that will hasten the work of the application. They will be useful for creating any type of real-time software: chatbots, IoT-applications or multiplayer online game.
Choosing a suitable algorithm of machine learning is an important point in the development of any intellectual system. All the algorithms of machine learning can be subdivided into three types:
One of the tasks of the project is the classification of the answers according to the questions from databases. The answers were classified in terms of their relation to the corresponding category.
Machine learning was handled with the help of the decision trees. One of them was substituted for the random forest. It is a set of several trained trees that handle the voting. Based on its results, the random forest prepares the reply according to the specified selection. For the realization of choosing the suitable one, the enforcement graph memo was used. It is a model designed by Microsoft that is used for selecting the machine learning algorithm for particular tasks (see the picture below).
Based on the scheme, we come to the conclusion that the random forest algorithm is the most suitable one for solving the tasks of multiclass classification when precision and speed of learning are of the highest priority.
The process of bot learning consists of several stages:
The application consists of a web-client and a Telegram chatbot on Python. The main functions of the application are:
The web-client consists of the interface for the work of the dialogue graph and of the keyboard for sending the messages. The client-side interaction with the bot is the classical option of working with the Telegram bot. Upon launching the application, two flows are launched as well. One of them is to maintain the work of Telegram bot, and the second one — the work of application. Processing of the client’s messages is handled through the tools “message handler” of TeleBot library.
At the head of the graph, the user’s question and the possible options for the answer are located. Upon clicking on one of the answers, its text is automatically displayed in the space of the answer.
Then, the system is waiting for the client’s reply to the sent message. When it receives it, the graph is reorganized by adding the user’s answer to the branch with the selected messages. New replies are prepared by the application.
While working on the project, we faced the following challenges:
In order to solve these challenges, we implemented the following features:
Although intellectual systems are growing rapidly, it is only the beginning of their implementation in the sphere of consulting. This is because of the necessity of the real-time handling of the online consultation, and because the algorithms of machine learning demand a lot of time for preprocessing and the learning itself.
Originally published at sloboda-studio.com on May 18, 2018.