Building a Chatbot for Twitter using MindsDB and GPT-4
This is what you will be building: A custom chatbot using MindsDB’s connectors to Twitter, OpenAI’s GPT-4 and custom prompts.
Want to talk to SnoopStein? Post a tweet with @snoop_stein
and wait for the reply.
A simple example is this Twitter bot - @Snoop_Stein - who will reply with the appropriate context and personality to any tweets which mention him. If you haven’t tried tweeting to SnoopStein yet, check it out and tweet at your new friend and rapping physicist! See what it comes up with.
Make your own SnoopStein
To get started:
- Create an account on MindsDB cloud (if you don’t have one yet).
- Go to your MindsDB SQL Editor
MindsDB is a popular open-source low-code machine learning platform that helps developers easily build #AI-powered solutions. It automates and integrates top machine learning frameworks into the data stack to streamline the integration of AI into applications, making it accessible to developers of all skill levels.
Now let us show you how we built the Snoop_Stein GPT-4 bot and how you can build your own.
1. Create a GPT-4 Model!
Let’s first see how simple it is to create a machine learning model represented as a virtual ‘AI table’ in MindsDB. In this example, we will call our GPT-4 model gpt_model
.
(Bear in mind that GPT-4 API is in HIGH demand and is rate limited, so can be slow. The following steps might each take a few seconds.)
Before creating an OpenAI model, please create an engine, providing your OpenAI API key:
Then, create a model using this engine:
If you are using MindsDB on Docker or if you want to use your own OpenAI API key, simply pass the api_key
argument in the USING
clause.
One important attribute here is prompt_template
. This is where we tell GPT how to write answers; it is a template because you can pass values from columns, in this case the template contains {{author_username}}
and {{text}}
, which will be replaced from the WHERE
clause variables in the query. Let’s see it in action:
2. Create an ML Model with Personality
As you can see, the previous model gave responses that aren’t that exciting. But we can use some prompt template magic to shape how we want the model to respond. Essentially we use prompt_template
to explain in plain english how we want GPT to formulate its responses.
Let’s create a model called snoopstein_model
with a prompt template that gives GPT a hybrid personality: he is half-Einstein, half-Snoop Dogg. A brilliant physicist who owns the rap game. His name is Snoop Stein:
Before creating an OpenAI model, please create an engine, providing your OpenAI API key:
Now let’s test this model:
Let’s try another one:
Let’s generate a response that includes an image created with the OpenAI DALL·E 3 model.
First, create and deploy the OpenAI DALL·E 3 model in MindsDB:
Let’s try it out by joining the two models:
The image URL takes you to an image generated by the OpenAI DallE3 model. Here is a sample image generated by Dalle3:
3. Connect your GPT-4 Model to Twitter!
You will need to sign up for a Twitter dev account in order to actually be able to read and write tweets into Twitter, if you don’t have a Twitter dev account already. Twitter may take a day or so to approve your new dev account. Once you are approved, here are the steps to link your Twitter account to MindsDB: https://www.youtube.com/watch?v=qVe7PeC0sUQ
You can find the details on how to connect your Twitter developer account to MindsDB here.
We are going to prepare a MindsDB SQL statement to connect to Twitter:
This creates a database called my_twitter
. This database ships with a table called tweets
that we can use to search for tweets as well as to write tweets.
You can use the Twitter API to get a list of tweets with a particular text or hashtag:
Note that the query
parameter supports anything that the Twitter API supports as query
, for more reference, read here.
Let’s test that this model can generate outputs based on the Snoop Stein personality on many tweets, by joining the model with the tweets
table:
4. Writing Tweets using SQL
Let’s test by tweeting a few things into the MindsDB Twitter account:
Like magic right? Those tweets should be live now on twitter. You can check your tweet responses here: https://twitter.com/MindsDB/status/1633439839491092482 And here: https://twitter.com/MindsDB/status/1634126825377996800
You can insert any of the values of the tweepy function create_tweet
: https://docs.tweepy.org/en/stable/client.html#tweepy.Client.create_tweet
5. Setting up a Database
MindsDB processes your data but doesn’t store it. Therefore, you need a database where MindsDB can insert query results to ensure the JOBs run smoothly. It also helps keeping the logs of conversations. Let’s create a MariaDB SkySQL cloud database, it takes just minutes to launch, and it comes with a free trial as well. Follow the docs to set up SkySQL and connect it to MindsDB.
Alternatively, you can opt for using the MindsDB’s custom LAST
keyword that, when used in a query, ensures only the newly added data is selected. Jump to the end of point 7 if you prefer to not use any external database for storing tweets.
Now, create two tables in your SkySQL database using the code below:
These tables will be used to store input and output of our chatbot.
6. Creating Job Components
First, we input all tweets that need a reply into the chatbot_input table:
Then, we input all tweets posted by snoop_stein into the chatbot_output table:
Now we create a view that stores all tweets that hasn’t been replied to yet.
Let’s join this view with the model and prepare the replies.
7. At last: let’s create the JOB
Now we put together all job components and automate the process.
This job is executed every minute. It fetches all new tweets. Then, it prepares and posts the replies.
Here are some useful commands to monitor the job and review the created tables:
Alternatively, you can skip setting up a database to store input and output tweets. Instead, you can use the LAST
keyword that ensures only the newly added tweets are selected.
Here is how to set up a job using the LAST
keyword:
Conclusion
MindsDB is a powerful software platform that enables developers to easily build machine learning features into their applications. With MindsDB, developers can train machine learning models from different data sources and integration platforms, and output the generated ML results or predictions directly into the DB, queryable as tables, or output via the connected application, in this case, Twitter. This example of building a Twitter chatbot with GPT-4 integration is not the only quick solution that developers can implement in just a few minutes: MindsDB has many examples, including integration with many other models, including Hugging Face, to build applications that can summarize text, translate, analyze customer sentiment (product reviews) and perform all kinds of business forecasting. You can find many examples here.
In pt. 2 of this series on Twitter and GPT integration, out next week, we will walk you through some new features that will allow quick creation of a conversational chatbot, that is able to maintain the state of historical messages and provide appropriate responses in context. Make sure you don’t miss it by going here and signing up for our blog updates!
Happy coding, and please join our community Slack for feedback, support, and questions. We look forward to meeting you there!