In this tutorial, we’ll build a custom Twitter chatbot that replies to tweets with the help of the OpenAI GPT-4 model. The workflow will be automated using Jobs - a MindsDB feature that enables you to schedule execution of tasks.

Deploy a GPT-4 model

Please note that using OpenAI models require OpenAI API key. Therefore, before creating a model, you need to configure an engine by providing your OpenAI API key as below. See docs.

CREATE ML_ENGINE openai_engine
FROM openai
USING
    openai_api_key = 'your-openai-api-key';

Let’s create a basic model to respond to tweets.

CREATE MODEL gpt_model
PREDICT response
USING
  engine = 'openai_engine',
  model_name = 'gpt-4',
  prompt_template ='respond to {{text}} by {{author_username}}';

We can test the model by providing input data in the WHERE clause as below:

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

Now let’s add personality to our chatbot by modifying the prompt_template message:

CREATE MODEL snoopstein_model
PREDICT response
USING
  engine = 'openai_engine',
  max_tokens = 300,
  temperature = 0.75,
  model_name = 'gpt-4',
  prompt_template = '

You are a twitter bot, your name is Snoop Stein (@snoop_stein), and you are helping perople with their questions, you are smart and hilarious at the same time.

From input message: {{text}}

by from_user: {{author_username}}

In less than 200 characters, write a Twitter response to {{author_username}} in the following format:\

Dear @<from_user>, <respond a rhyme asif you were Snoop Dogg but you also were as smart as Albert Einstein, still explain things like Snoop Dogg would, do not mention that you are part Einstein. Quote fromreferencesfor further dope reads if it makes sense. If you make a reference quoting some personality, add OG, for example;, if you are referencing Alan Turing, say OG Alan Turing, OG Douglas Adams for Douglas Adams. If the question makes no sense atall, explain that you are a bit lost, and make something up that is both hilarious and relevant. signwith-- SnoopStein by @mindsdb.';

Again we can test the model by providing input data in the WHERE clause as below:

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

Connect Twitter to MindsDB

Follow the docs to connect your Twitter account to MindsDB.

CREATE DATABASE my_twitter 
WITH 
    ENGINE = 'twitter',
    PARAMETERS = {
      "bearer_token": "twitter bearer token",
      "consumer_key": "twitter consumer key",
      "consumer_secret": "twitter consumer key secret",
      "access_token": "twitter access token",
      "access_token_secret": "twitter access token secret"
    };

Here is how to read tweets from snoop_stein created after a defined date:

SELECT *
FROM my_twitter.tweets
WHERE query = 'from:snoop_stein'
AND created_at > '2023-04-04 11:50:00';

And here is how to write tweets, providing a tweet id to reply to:

INSERT INTO my_twitter.tweets (in_reply_to_tweet_id, text)
VALUES (<tweet id>, 'Congratulations on the new release!');

Automate replies to tweets

Now we put together all job components and automate the process.

CREATE JOB twitter_chatbot (
 
  INSERT INTO my_twitter.tweets (

     SELECT d.id AS in_reply_to_tweet_id,
            m.response AS text
     FROM my_twitter.tweets AS d
     JOIN snoopstein_model AS m
     WHERE d.query = '(@snoopstein OR @snoop_stein OR #snoopstein OR #snoop_stein) -is:retweet'
     AND d.id > LAST
  )
)
EVERY minute;

This job is executed every minute. It fetches all recently added tweets with the help of the LAST keyword. Then, it prepares and posts the replies.

Here are some useful commands to monitor the job:

SELECT * FROM jobs
WHERE name = 'twitter_chatbot';

SELECT * FROM jobs_history
WHERE name = 'twitter_chatbot';