Construct a Celebrity Twitter Chatbot with GPT-4 Make your personal SnoopStein 1. Create a GPT-4 model! 2. Create an ML model with personality 3. Connect your GPT-4 model to Twitter! 4. Writing tweets using SQL 5. Eventually: let’s create the JOB Conclusion


That is what you might be constructing: A custom chatbot using MindsDB’s connectors to Twitter, OpenAI’s GPT-4 and custom prompts.
A straightforward example is that this Twitter bot – @Snoop_Stein – who will reply with the suitable context and personality to any tweets which mention him. In case you haven’t tried tweeting to SnoopStein yet, test it out and tweet at your recent friend and rapping physicist! See what it comes up with.

To start:

MindsDB is a preferred open-source low-code machine learning platform that helps developers easily construct #AI-powered solutions. It automates and integrates top machine learning frameworks into the information stack to streamline the mixing of AI into applications, making it accessible to developers of all skill levels.

Now allow us to show you ways we built the Snoop_Stein GPT-4 bot and the way you’ll be able to construct your personal.

Let’s first see how easy it’s to create a machine learning model represented as a virtual ‘AI table’ in MindsDB. In this instance, we are going to call our GPT4 model gptbot_model.

(Keep in mind that GPT-4 API is in HIGH demand and is rate limited, so might be slow. The next steps might each take just a few seconds)

CREATE MODEL mindsdb.gpt_model
PREDICT response
engine = 'openai',
-- api_key = 'your openai key', in MindsDB cloud accounts we offer a default key
model_name = 'gpt-4', -- you can too use 'text-davinci-003', 'gpt-3.5-turbo'
prompt_template = 'reply to {{text}} by {{author_username}}';

Optionally, In case you are using MindsDB on docker or if you would like to use your personal OpenAI API key, simply pass the api_key argument within the USING.

One essential attribute here is prompt_template. That is where we tell GPT learn how to write answers; it’s a template because you’ll be able to pass values from columns, on this case the template accommodates {{author_username}} and {{text}}, which might be replaced from the WHERE variables within the query. Let’s see in motion:

SELECT response from mindsdb.gpt_model WHERE author_username = "mindsdb" AND text="why is gravity so different on the sun?";

As you’ll be able to see, the previous model gave responses that aren’t that exciting. But we will use some prompt template magic to shape how we would like the model to reply. Essentially we use prompt_template to clarify how we would like GPT to formulate its responses.
Let’s create a model mindsdb.snoopstein_model with a prompt template that provides GPT a hybrid personality: he’s half-Einstein, half-Snoop Dogg. A superb physicist who owns the rap game. His name is :

CREATE MODEL mindsdb.snoopstein_model
PREDICT response
engine = 'openai',
max_tokens = 300,
-- api_key = 'your openai key, in cloud accounts we offer one',
model_name = 'gpt-4', -- you can too use 'text-davinci-003' or 'gpt-3.5-turbo'
prompt_template = 'From input message: {{text}}
by from_user: {{author_username}}
In lower than 550 characters, write a Twitter response to {{author_username}} in the next format:
Dear @,

Now let’s test this model:

SELECT response from mindsdb.snoopstein_model
author_username = "someuser"
AND text="@snoop_stein, why is gravity so different on the sun?.";

Let’s try one other one:

SELECT response from mindsdb.snoopstein_model
author_username = "someuser"
AND text="@snoop_stein, Aside from yourself, which rappers would make one of the best physicists and why?!";

We’re going to arrange a MindsDB SQL statement to hook up with twitter with read-only access:

WITH ENGINE = 'twitter';

This creates a database called my_twitter. This database ships with a table called tweets that we will use to look for tweets in addition to to write down tweets.

You should use the Twitter API to get an inventory of tweets with a selected text or hashtag, within the case below mindsdb or #mindsdb.

id, created_at, author_username, text
FROM my_twitter.tweets
query = '(@snoopstein OR @snoop_stein OR #snoopstein OR #snoop_stein) -is:retweet'
AND created_at > '2023-03-20'

Note that the parameter query supports anything that the twitter API supports as query, for more reference, read (

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:

SELECT AS in_reply_to_tweet_id,
t.text AS input_text,
r.response AS text
FROM my_twitter.tweets t
JOIN mindsdb.snoopstein_model r
WHERE t.query = '(@snoopstein OR @snoop_stein OR #snoopstein OR #snoop_stein) -is:retweet -from:snoop_stein'
AND t.created_at > '2023-03-20'

Now we’re on the part where we would like MindsDB to write down responses back into Twitter. For this, you will want to enroll for a Twitter dev account in an effort to actually find a way to write down tweets back into Twitter (write, versus read-only), when you don’t have a Twitter dev account already.

Twitter may take a day or so to approve your recent dev account. Once you might be approved, listed here are the steps to link your Twitter account to MindsDB:

  1. Open developer portal,
  2. Select the [+ Add app] button to create a recent app
  3. Select [Create new]
  4. Select “Production” and provides it a reputation
  5. Copy and populate within the query:

6. Click Setup on

  • On Permissions Select: Read and Write
  • On Form of app Select: Web App, Automated App or Bot
  • On App info: provide any url for the callback url and website url
  • Click Save

7. Once you might be back within the app settings, click Keys and Tokens

8. Generate Access Token and Secret and populate on the query

  • Access Token
  • Access Token Secret

You may create a recent updated database so it could possibly read and write as follows:

CREATE DATABASE mindsdb.my_twitter_v2
"consumer_key": "your twitter App API key",
"consumer_secret": "your twitter App API key secret",
"bearer_token": "your twitter App bearer TOKEN",
"access_token": "your twitter App Access Token",
"access_token_secret": "your twitter App Access Token Secret"

Let’s test by tweeting just a few things into the MindsDB Twitter account

INSERT INTO my_twitter_v2.tweets (in_reply_to_tweet_id, text) 
(1633439839491092482, 'MindsDB is great! now its super easy to construct ML powered apps using JOBS'),
(1634126825377996800, 'Holy!! MindsDB is such a useful gizmo for developers doing ML');

Like magic right? Those tweets ought to be live now on twitter, You may check your tweet responses here:
And here:

Note: you’ll be able to insert any of the values of the tweepy function create_tweet:

The CREATE JOB statement is great because you should utilize it to automate work. The concept is straightforward, you give it a question you would like to execute and the way often. Let’s arrange a job for Snoop Stein!

Let’s write a JOB called gpt4_twitter_job:

  1. Checks for brand spanking new tweets
  2. Generates a response using the OpenAI model
  3. Writes the responses back into Twitter

All of this might be written in a single SQL command

CREATE JOB mindsdb.gpt4_twitter_job AS (
-- insert into tweets the output of joining model and recent tweets
INSERT INTO my_twitter_v2.tweets (in_reply_to_tweet_id, text)
SELECT AS in_reply_to_tweet_id,
r.response AS text
FROM my_twitter.tweets t
JOIN mindsdb.snoopstein_model r
t.query = '(@snoopstein OR @snoop_stein OR #snoopstein OR #snoop_stein) -is:retweet -from:snoop_stein'
AND t.created_at > "{{PREVIOUS_START_DATETIME}}"
limit 10
EVERY hour

And there it’s! Every hour, we might be checking for brand spanking new tweets that mention MindsDB, and replying with responses generated by OpenAI GPT-4 using the template from step 2 that may respond in a method that mixes Albert Einstein and Snoop Dogg.

You may check in case your JOB is running effectively

SELECT * FROM jobs WHERE name="gpt4_twitter_job";
SELECT * FROM jobs_history WHERE name="gpt4_twitter_job";

You may stop the job as follows

DROP JOB gpt4_twitter_job

MindsDB is a robust software platform that allows developers to simply construct 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, on this case, Twitter. This instance of constructing a Twitter chatbot with GPT-4 integration will not be the one quick solution that developers can implement in only just a few minutes: MindsDB has many examples, including integration with many other models, including Hugging Face, to construct applications that may summarize text, translate, analyze customer sentiment (product reviews) and perform every kind of business forecasting. You will discover many examples here

In pt. 2 of this series on Twitter and GPT integration, out next week, we are going to walk you thru some recent features that may allow quick creation of a conversational chatbot, that is in a position to keep up the state of historical messages and supply appropriate responses in context. Be certain that you don’t miss it by going here and signing up for our blog updates!

Completely satisfied coding, and please join our community Slack for feedback, support, and questions. We sit up for meeting you there!


What are your thoughts on this topic?
Let us know in the comments below.


0 0 votes
Article Rating
1 Comment
Newest Most Voted
Inline Feedbacks
View all comments

Share this article

Recent posts

Would love your thoughts, please comment.x