1. Tutorials
  2. Building a Chatbot for Twitter using MindsDB and GPT-4

In this section, we present how to build a chatbot for Twitter using MindsDB and GPT-4.

Prerequisites

  • Make sure you have access to a working MindsDB installation, either locally or at MindsDB Cloud.
  • Follow this instruction to connect your Twitter developer account to MindsDB.

Tutorial

Searching and Writing Tweets

Here is how to search tweets containing mindsdb keyword:

SELECT id, created_at, author_username, text 
FROM my_twitter.tweets 
WHERE query = '(mindsdb OR #mindsdb) -is:retweet -is:reply' 
AND created_at > '2023-02-16' 
LIMIT 20;

Alternatively, you can use a Twitter native query, as below:

SELECT * FROM my_twitter (
  search_recent_tweets(
    query = '(mindsdb OR #mindsdb) -is:retweet -is:reply',
    start_time = '2023-03-16T00:00:00.000Z',
    max_results = 2
  )
);

To learn more about native queries in MindsDB, visit our docs here.

Here is how to write tweets:

INSERT INTO my_twitter.tweets (reply_to_tweet_id, text)
VALUES 
    (1626198053446369280, 'MindsDB is great! now its super simple to build ML powered apps'),
    (1626198053446369280, 'Holy!! MindsDB is the best thing they have invented for developers doing ML');

We write tweets by inserting values into the tweets table.

Responding to Tweets

Let’s create an OpenAI model to respond to tweets.

CREATE MODEL mindsdb.gpt_bot
PREDICT response 
USING 
 engine = 'openai', 
 api_key = 'YOUR_OPENAI_API_KEY',     -- This parameter is optional if you use MindsDB Cloud.
 model_name = 'gpt-4',                -- You can also use the 'text-davinci-003' or 'gpt-3' models.
 max_tokens = 300, 
 prompt_template = "
From input message: {{text}} 

by author username: {{author_username}} 

Write a Twitter response in the following format: 
Dear @<username>, <respond as if you were either Sigmund Freud, Albert Einstein, whichever is most relevant to the input message. If possible include references to publications and sections for further reading. If the reference or quoting yourself add papa, for example if you are referencing Alan Turing, say papa Alan Turing. If the question wuld have made no sense to any of those three individuals, explain that you are a bit lost and make up something hilarious but relevant up. sign with -- bot at the end of the name as in reference to a robot version of those individuals, for example Albertobot for Albert Einstein.> ";

This model responds to tweets as instructed in the prompt_template parameter.

To learn more about OpenAI integration with MindsDB, visit our docs here.

Here is how you can get a response by manually querying the model:

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

On execution, we get:

+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| response                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Dear @mindsdb, Gravity on the sun is different due to its immense mass and size compared to Earth. As Papa Albert Einstein explained in his theory of general relativity, gravity is the curvature of spacetime caused by mass. The sun's mass is about 333,000 times that of Earth, resulting in a stronger gravitational pull. For more insights, check out Einstein's publication "The Foundation of the General Theory of Relativity" (1916). Stay curious! -- Albertobot |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

Or you can query for batch responses, as below:

SELECT 
    t.id AS in_reply_to_tweet_id, 
    t.text AS input_text, 
    t.author_username, 
    t.created_at, 
    r.response AS text 
FROM my_twitter.tweets t 
JOIN mindsdb.gpt_bot r 
WHERE t.query = '(@mindsdb) -is:retweet'
AND t.created_at >  '2023-03-12' limit 4;

Follow along to see how to automate the responses.

Automating Responses to Tweets

Finally, we create a chatbot by automating responses to tweets using jobs.

To learn more about jobs, visit our docs here.

Let’s create a job that runs every hour, checks for new tweets, and responds using the OpenAI model.

CREATE JOB auto_respond AS (

    INSERT INTO my_twitter.tweets (in_reply_to_tweet_id, text)
        SELECT 
            t.id AS in_reply_to_tweet_id, 
            r.response AS text
        FROM my_twitter.tweets t
        JOIN mindsdb.gpt_bot r 
        WHERE t.query = '(mindsdb OR #mindsdb) -is:retweet -is:reply' 
        AND t.created_at > "{{PREVIOUS_START_DATETIME}}"
        LIMIT 2
)
EVERY hour;

The EVERY hour clause ensures the job runs once every hour. The job uses the SELECT statement to join the data table with the model table and get responses for tweets posted after the {{PREVIOUS_START_DATETIME}} timestamp. Then, the INSERT INTO statement writes these responses to the tweets table of the my_twitter integration.