Building a Chatbot for Twitter using MindsDB and GPT-4
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.
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 MODEL gpt_modelPREDICT responseUSING 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:
Copy
Ask AI
SELECT responseFROM gpt_modelWHERE 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:
Copy
Ask AI
CREATE MODEL snoopstein_modelPREDICT responseUSING 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:
Copy
Ask AI
SELECT responseFROM snoopstein_modelWHERE author_username = "someuser"AND text='@snoop_stein, why is gravity so different on the sun?.';
Now we put together all job components and automate the process.
Copy
Ask AI
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:
Copy
Ask AI
SELECT * FROM jobsWHERE name = 'twitter_chatbot';SELECT * FROM jobs_historyWHERE name = 'twitter_chatbot';