Creating a Rick Sanchez chat bot with Transformers and Chai

Making a Rick & Morty Chatbot for Chai

Rick Bot in action

We’re going to be making a chatbot, based on Microsoft’s DialoGPT. I’ve seen lots of other guides on training chatbots, but I haven’t come across one which actually deploys the bot. This tutorial will cover deploying our new bot to Chai, a platform for creating and interacting with conversational AI’s. It will allow us to chat with the bot through a mobile app, from anywhere, at any time. We will also be able to see performance stats and watch our bot climb the Chai bot leaderboard.

By the end of this tutorial you will have your very own chatbot, like the one pictured above 😎

If you would rather start off small, head over to the chai docs for a much simpler and shorter tutorial on creating your first bot!

I made a Google Colab notebook which allows you to run all of the code featured in this blog really easily. You can find it here.

Almost all of the code for training this bot was made by Mohamed Hassan. Their code has been adapted to suit the tutorial better.
The training data has been fetched from this article by Andrada Olteanu on Kaggle

Let’s get started!

Install the Huggingface transformers module

pip -q install transformers

Import DialoGPT

DialoGPT is a chatbot model made by Microsoft. This will be the base for Rick Bot.

Chat with the untrained model

Chatting with the untrained bot

It’s capable of holding a conversation (sort of), but it doesn’t resemble Rick Sanchez at all yet.

Configuring the model

Here we define some config. These can be tweaked to generate a slightly different bot.

Trending Bot Articles:

1. How Conversational AI can Automate Customer Service

2. Automated vs Live Chats: What will the Future of Customer Service Look Like?

3. Chatbots As Medical Assistants In COVID-19 Pandemic

4. Chatbot Vs. Intelligent Virtual Assistant — What’s the difference & Why Care?

Gather the training data

We’re using some Rick and Morty scripts from this article by Andrada Olteanu (the data can be found here)

We want the model to be aware of previous messages from the dialogue to help it decide what to say next. We call this context. The dataset has context from 7 previous messages.

Training

Now, this is quite a hefty chunk of code but don’t worry you don’t need to understand it yet, we can cover this in later tutorials.

Main Runner

Let’s start training!

This should take around 10 minutes so you might as well go grab a cup of coffee ☕️

Chatting with the trained bot

Conversation with Rick bot

That’s more like it!

Uploading to HuggingFace 🤗

HuggingFace is a platform for hosting machine learning models. Think Github for ML.

apt-get install git-lfs

Git needs an email address:

git config --global user.email <YOUR_EMAIL>

Login with your Huggingface account (if you don’t have one you can sign up here) and then push our new model:

huggingface-cli login

Following the link the code above gives us will take you to your bot’s Huggingface page, it will look something like this:

Rick Bot on Huggingface

Great! Now our bot is being hosted on HuggingFace we can deploy to Chai

Deploying to Chai

Chai is a platform for creating, sharing and interacting with conversational AI’s. It allows us to chat with our new bot through a mobile app. This means you can show it off really easily, no need to whip out your laptop and fire up a colab instance, simply open the mobile app and get chatting!

There is also a bot leaderboard to climb. We can see how our new bot compares to others on the platform:

Lets deploy RickBot to Chai!

Install the chaipy package:

pip install --upgrade chaipy

Head over to the Chai Dev Platform to set up your developer account. This allows us to deploy the bot under our own account.
Your developer ID and keys can be found on the dev page.

Setup the notebook:

Chai bot code

Now it’s time to write the code to interact with our Huggingface model.

You can add to the past_user_inputs to change the bot’s starting context. This changes how the bot will act when the conversation starts.

Deploy to Chai

Time to deploy the bot!

You can change the image_url and description to personalise how the bot will appear on the platform.

Success 🎉

deploy.py will generate a QR code, scan it with your phone and you will be taken to a chat screen with your brand new bot. How cool is that?!
(Make sure you have the Chai app installed on your phone)

Conclusion

If you would like to make your own Rick bot, I highly recommend using the Google Colab linked at the start of the blog. I’ve trimmed some of the code snippets on the blog to make it easier to understand, so the code probably wont run properly when copied to your own machine.

Any dialogue can be used to train this bot, it just needs to be in the right format. Try making a bot of your favourite film character, or maybe from a TV show.
You can also try changing the training config. For example, the context length (n) or any of the arguments in the Args class.

Don’t forget to give us your 👏 !


Creating a Rick Sanchez chat bot with Transformers and Chai was originally published in Chatbots Life on Medium, where people are continuing the conversation by highlighting and responding to this story.