Black Mirror with your own hands - we train the bot on the basis of its chat history

Black Mirror with your own hands - we train the bot on the basis of its chat history

In the Black Mirror there was a series (S2E1) in which they created robots that looked like dead people, using the history of correspondence in social networks for learning. I want to tell you how I tried to do something like that and what came of it. There will be no theory, only practice.


The idea was simple - to take the history of your chats from Telegram and, on their basis, train the seq2seq network, which can predict its completion at the beginning of the dialogue. This network can work in three modes:

  • Predict the completion of a user's phrase based on the conversation history
  • Chat chat
  • Synthesize the entire conversation logs

That's what happened with me

The bot offers the completion of the phrase


The bot offers the completion of the dialogue


The bot communicates with a living person

  User: Hello
 Bot: hello
 User: how are you?
 Bot: going
 User: bug fixed?
 Bot: no
 User: why?
 Bot: it does not work
 User: will you be at home today?
 Bot: I do not know yet
 User: are you busy?
 Bot: to the food shop  

Next, I'll tell you how to prepare the data and train such a bot yourself.

How to train yourself

Data preparation

First of all, you need to get a lot of chats somewhere. I took all my correspondence in Telegram, since the client for the desktop allows you to download the full archive in JSON format. Then I threw away all the messages that contain quotes, links, and files, and translated the remaining texts in the lower case and threw out all the rare characters, leaving only a simple set of letters, numbers and punctuation marks - it is easier for the network to learn.

Then I brought chat rooms to this form:

 & gt;  Hello
 & lt;  hello to you
 & gt;  until
 & lt;  see you!
 & gt;  how are you?
 & lt;  good  

Here are posts that start with the "& gt;" this is a question for me, the symbol "& lt;" accordingly marks my answer, and the line "===" serves to separate the dialogues among themselves. I determined the time that one dialogue ended and another one started (if more than 15 minutes passed between the messages, then we believe that this is a new conversation. Script to convert the story you can look at github .

Since I have been actively using telegrams for a long time, there were a lot of messages in the end - the final file turned out to be 443 thousand lines.

Model selection

I promised that there would be no theory today, so I will try to explain as briefly as possible and on my fingers.

I chose the classic seq2seq model based on the GRU. Such a model receives the text in letters by input and also gives out one letter to the output. The learning process is based on the fact that we teach the network to predict the last letter of the text, for example, we input "lead" to the input and wait for the output to be "answer" .

To generate the same long texts, a simple trick is used - the result of the previous prediction is sent back to the network and so on until a text of the required length is generated.

GRU modules are very, very simplistic, you can think of them as “a cunning perceptron with memory and attention,” you can read more about them, for example, here .

The model was based on the well-known example of the Shakespeare text generation task.


Anyone who has ever encountered neural networks probably knows that it is very boring to learn them on the CPU. Fortunately, google comes to the rescue with their service Colab - you can run your code in jupyter notebook for free using the CPU, GPU and even TPU . In my case, training on a video card is within 30 minutes, although the sane results are already available after 10. The main thing is to remember to switch the iron type (in the Runtime menu - & gt; Change runtime type).


After training, you can proceed to test the model - I wrote a few examples that allow you to access the model in different modes - from text generation to live chat. All of them are available on github .

In the text generation method there is a temperature parameter - the higher it is, the more diverse the text (and meaningless) the bot will issue. This option makes sense to customize hands for a specific task.

Further use

What can such a network be used for? The most obvious is to develop a bot (or smart keyboard) that can offer the user ready-made answers even before he writes them. Such a function has long existed in Gmail and most keyboards, but it does not take into account the context of the conversation and the manner of a particular user to correspond. Say, the G-Keyboard keyboard stably offers me completely meaningless options, for example, "I travel with ... respect" in the place where I would like to get the option "I travel from the country house", which I definitely used many times.

Does the bot have a future? In its pure form, it is not exactly there, it has too much personal data, no one knows at what point it will give the interlocutor the number of your credit card that you once threw to a friend. Moreover, such a bot is not completely tuned, it is very difficult to get it to perform some specific tasks or correctly answer a specific question. Rather, such a chat bot could work in conjunction with other types of bots, providing a more connected dialogue "about anything" - with this he copes well. (And nevertheless, the external expert in the wife’s face said that the bot’s manner of communication is very similar to me. And the topics he’s concerned about are clearly the same - bugs, fixes, commits and other joys and sadness of the developer constantly pop up in the texts).

What else I advise you to try if you are interested in this topic?

  • Transfer learning (to teach foreign dialogues on a large body, and then to complete the education on their own)
  • Change model — increase, change type (for example, to LSTM).
  • Try working with TPU. In its pure form, this model will not work, but you can adapt it. The theoretical acceleration of learning should be tenfold.
  • Port to a mobile platform, for example using Tensorflow mobile.

P.S. Link to github

Source text: Black Mirror with your own hands - we train the bot on the basis of its chat history