Conversational AI Chatbot with Pretrained Transformers Using Pytorch | by Raymond Cheng | Towards Data Science

Conversational systems, or dialogue systems, have garnered huge interest in the modern Natural Language Processing (NLP) community. It is simply exciting to see how closely bots can mimic our thoughts, logic, and emotions as shown from their language. Today, we know that there are digital assistants right at the palm of our hands in our smartphones, such as Apple Siri, Google Assistant, and Microsoft Cortana. They are all able to listen and respond to the user’s language although not perfect.
In this post, we teach you how you can leverage pretrained transformers such as DialoGPT to implement your own conversational chatbot. We will be using the Transformers library provided by Huggingface to build this system.
So without further ado, let’s begin!
Step 1: Install Library
Step 2: Import Library
Step 3: Build Conversational Pipeline
Step 4: Add starting conversations
Step 5: Add continuing conversations
The library we are using is Huggingface Transformers. To install it, you can simply do:
pip install transformers
Make sure to have a working version of Pytorch or Tensorflow, so that Transformers can use one of them as the backend.
After installing Transformers, now it’s time to import it in a Python script. Instead of using the entire Transformers library, we introduce a pipeline module that basically provides a very simple API to do a variety of NLP tasks without needing to understand complex code. We also import the Conversation object, which we will use later on for the conversational pipeline.
To import them, you can do:
from transformers import pipeline, Conversation
After importing the relevant components, now we can start building the pipeline for the conversation system by:
conversational_pipeline = pipeline(“conversational”)
This line of code will setup the conversation pipeline using DialoGPT as the model, a GPT2 model trained on a dialogue dataset.
Now, we can start talking to the bot! First, let’s open up two conversations with the bot and ask it for movie recommendations and what it’s favorite book is:
Conversation 1: Let’s watch a movie tonight — any recommendations?
Conversation 2: What’s your favorite book?
conv1_start = “Let’s watch a movie tonight — any recommendations?”conv2_start = “What’s your favorite book?”
Then, we construct two conversations using the defined variables:
conv1 = Conversation(conv1_start)conv2 = Conversation(conv2_start)
After that, we put both of the conversations into the list and then feed it to the conversational pipeline:
conversational_pipeline([conv1, conv2])
Finally, we can see the results of each conversation returned by the bot:
[Conversation id: a9bae572-cc11–48c8-b36f-d88bcd49b8c0  user >> Let’s watch a movie tonight — any recommendations?  bot >> The Big Lebowski , Conversation id: e6ee2d97–5ee7–404b-b2e5–4dd527f9e9dd  user >> What’s your favorite book?  bot >> The Hunger Games ]
We can continue the conversation with the bot by adding new user inputs to the previous conversations and then process the conversational pipeline again.
Imagine we want to follow up the bot by asking what the movie is about in Conversation 1 and what the genre of the book is in Conversation 2.
Conversation 1: What is it about?
Conversation 2: Cool, what is the genre of the book?
conv1_next = “What is it about?”conv2_next = “Cool, what is the genre of the book?”
To add new user inputs to previous conversations, we can first do:
conv1.add_user_input(conv1_next)conv2.add_user_input(conv2_next)
After that, we can simply reuse the prior code to update the conversational pipeline with the new set of conversations:
conversational_pipeline([conv1, conv2])
Finally, you should see that the bot is able to return new responses which are quite on topic:
[Conversation id: a9bae572-cc11–48c8-b36f-d88bcd49b8c0  user >> Let’s watch a movie tonight — any recommendations?  bot >> The Big Lebowski  user >> What is it about?  bot >> It’s a comedy about a guy who gets a job at a movie theater and gets a bunch of jobs. , Conversation id: e6ee2d97–5ee7–404b-b2e5–4dd527f9e9dd  user >> What’s your favorite book?  bot >> The Hunger Games  user >> Cool, what is the genre of the book?  bot >> I’m not sure, but I think it’s fantasy. ]
And that’s all for this article! Hope you learned something useful. In this post, we went through how you can implement your own conversational bot using a pretrained model provided by Huggingface. In case you are looking for it, I have attached a Jupyter version of the entire code down below, for your convenience:
from transformers import pipeline, Conversation

conversational_pipeline = pipeline("conversational")

conv1_start = "Let's watch a movie tonight - any recommendations?"
conv2_start = "What's your favorite book?"

conv1 = Conversation(conv1_start)
conv2 = Conversation(conv2_start)

conversational_pipeline([conv1, conv2])

Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.

[Conversation id: a9bae572-cc11-48c8-b36f-d88bcd49b8c0 
 user >> Let's watch a movie tonight - any recommendations? 
 bot >> The Big Lebowski ,
 Conversation id: e6ee2d97-5ee7-404b-b2e5-4dd527f9e9dd 
 user >> What's your favorite book? 
 bot >> The Hunger Games ]
conv1_next = "What is it about?"
conv2_next = "Cool, what is the genre of the book?"

conv1.add_user_input(conv1_next)
conv2.add_user_input(conv2_next)

conversational_pipeline([conv1, conv2])

Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.

[Conversation id: a9bae572-cc11-48c8-b36f-d88bcd49b8c0 
 user >> Let's watch a movie tonight - any recommendations? 
 bot >> The Big Lebowski 
 user >> What is it about? 
 bot >> It's a comedy about a guy who gets a job at a movie theater and gets a bunch of jobs. ,
 Conversation id: e6ee2d97-5ee7-404b-b2e5-4dd527f9e9dd 
 user >> What's your favorite book? 
 bot >> The Hunger Games 
 user >> Cool, what is the genre of the book? 
 bot >> I'm not sure, but I think it's fantasy. ]
Hope you enjoyed the article! If you like my work, please subscribe to the email list so you can receive updates whenever I create new content! Feel free to also browse my other posts if interested:
towardsdatascience.com
towardsdatascience.com
towardsdatascience.com
towardsdatascience.com
towardsdatascience.com
[1] Apple Siri Official Website, Apple
[2] Google Assistant Official Website, Google
[3] Microsoft Cortana Official Website, Microsoft
[4] Transformers Github, Huggingface
[5] Transformers Official Documentation, Huggingface
[6] Pytorch Official Website, Facebook AI Research
[7] Tensorflow Official Website, Google Brain
[8] Zhang, Yizhe, et al. “Dialogpt: Large-scale generative pre-training for conversational response generation.” arXiv preprint arXiv:1911.00536 (2019).
[9] Radford, Alec, et al. “Language models are unsupervised multitask learners.” OpenAI blog 1.8 (2019): 9.