What you’ll build
You’ll build a system that scans for targeted content to respond to, and uses ChatGPT to reply from a specific bot account live on Twitter like you see here: https://www.twitter.com/snoop_stein | ![]() |
Tutorial Architecture
You’ll use MindsDB to construct the bot and interact with LLMs like OpenAI’s ChatGPT, while indexing and storing accumulated tweet data in a MariaDB relational database. In the diagram below, the teal colored database represents data in MariaDB. The gray/light blue colored database represents data stored in MindsDB. What you build can then be embedded into your application with one of MindsDB SDKs, and/or called from the MindsDB REST API.
But wait, isn’t MindsDB a database, you might rightly ask? MindsDB manages AI models and automates workflows connecting AI models with enterprise databases. MindsDB is an AI Database rather than a traditional database. Therefore, if your model necessitates considerable input or yields a substantial output volume, or if you’re seeking the data reliability assurances provided by a relational database, it’s advisable to allocate that data storage to a different platform. Ultimately, MindsDB focuses on streamlining the processes of linking your data, training models and deploying AI models rather than being a comprehensive storage solution.
Tutorial Prerequisites
You’ll need three things to get started:- A MindsDB account.
- A MariaDB SkySQL account.
- A Twitter developer account. You’ll need your own API keys /access tokens to input parameters like these below into your MindsDB console to build the bot job later in the tutorial.
Data Setup
This example uses live data sourced directly from the Twitter API via the MindsDB Twitter integration, as opposed to an existing data set, CSV file, or other export of some pre-existing database. Accumulated Twitter data will be stored in a Maria DB Enterprise Server Columnstore database deployed on SkySQL, which is designed for operational analytic workloads.Creating the MariaDB Enterprise Server database
MariaDB ColumnStore extends MariaDB Enterprise Server database with distributed, columnar storage and a massively parallel processing (MPP) shared nothing architecture, transforming it into a standalone or distributed data warehouse for ad hoc SQL queries and advanced analytics – without the need to create indexes. It’s a great place to store live Twitter data. Login to SkySQL, and choose “launch a cloud database in SkySQL”. Then assign these choices, accepting other defaults:
> Technology, topology : Enterprise Server > ColumnStore Data Warehouse
> Cloud Provider: Choose as you wish from the available options. Google Cloud works great. We recommend choosing a nearby region to you, for performance reasons.
> Instance, Storage, Nodes: Choose the Sky 4x16 (4 vCPU, 16GB) instance, which is more than sufficient for learning purposes. SkySQL gives you a free credit to work with but make sure to turn off the instance if you aren’t using it so you don’t waste it.
> Service Attributes: Give it a name of your choosing. The tutorial code will reflect the name: analyticsdb.
> Security: Click the radio box to ‘Allow access to the service from specific IPs’, and then check the box for adding your current IP. Later, we’ll also add the MindsDB cloud IP addresses.
> Launch Service: Lastly, click “Launch Service” to create, and wait a few minutes.
Creating the MariaDB Enterprise Server schema
Now let’s create the schema to hold data from Twitter’s API calls from MindsDB, that will help us identify, track and reply to the right tweets. In the SkySQL Console left-hand navigation pane, click Workspace> Query Editor. Copy and paste the SQL code (below the screenshot) into the editor and click the run button as shown below, which will produce the two tables in the schema browser,chatbot_input and chatbot_output.
SQL to execute:
chatbot_input and chatbot_output tables.
Understanding the Data
Tweets have a long list of fields in the Twitter API and object model; if you look at their API documentation you’ll see this tutorial is using a subset. Of course, the datatypes and field titles are most clearly visible in the SQL script above. You’ll also see them and their values in the MindsDB console (pictured below) as you do testing, develop and execute queries. This will help you target the content you want ChatGPT (another supported LLM) to respond to from Twitter.
MindsDB Connections Setup
MindsDB Cloud and MariaDB Enterprise Server
Ok, a little more infrastructure setup to do before we roll up our sleeves and get into the use-case related code. We’ll handle network security first, and then connect MindsDB and MariaDB in the MindsDB SQL Editor.Security
Let’s add the MindsDB Cloud IP addresses to the network allowlist on SkySQL. To get the IPs, click on the add > new datasource button in the MindsDB console, and search/choose MariaDB. You’ll see the IP addresses in question on the resulting screen.
Copy/paste these IPs Into a temporary text document and then add them with the /32 CIDR notation to the SkySQL > settings> secure access panel as shown below:
If you’re developing on a laptop, MariaDB SkySQL will automatically prompt you to update the allowlist for new IP addresses as you travel, ensuring that your laptop IP can communicate to the query editor, regardless of your location.
Declaring a MariaDB Enterprise Server Data Source in MindsDB
The easiest way to work with the MariaDB Enterprise Server is its DBaaS cloud service, SkySQL. In the MindsDB SQL editor, click add > new datasource button, and search/choose MariaDB SkySQL. This will open a new query window, and generate a MindsDB SQL template for making the MindsDB <-> MariaDB connection:chatbotdb - this is the name of the MariaDB ColumnStore database you created with the SQL above.
(analyticsdb is the SkySQL service name).
Then, execute the completed SQL template in the MindsDB SQL editor by highlighting the SQL to execute, and clicking Run as shown below:
(Note that values have been left unmodified in the screenshot).
You’ll notice a DB connection icon now on the left hand nav for MindsDB Demo cloud (mariadb_db in the above screenshot).
MindsDB Cloud and Twitter
Click the add > new datasource button in the MindsDB console, and search/choose Twitter.
Fill in the template with your Twitter API keys/tokens. Then, in the MindsDB SQL editor, select the SQL, and execute it by clicking Run.
Deploying a Model
Step 1: Creating and Deploying a Model
Here you use theCREATE MODEL command to create a predictor - for example’s sake, the ‘Snoopstein’ model bot. Replace the snoopstein_model with your TwitterBot name, and come up with your own prompt template that makes sense for your scenario.
Before creating an OpenAI model, please create an engine, providing your OpenAI API key:
complete, you can proceed to the next step.
Step 2: Testing the Model
Write this code into the SQL editor, select the SQL, and execute it by clicking Run. Replace the author_username with your own personal Twitter handle, for tweet copy that would include an ‘@’ mention.Automating the Workflow with MindsDB Jobs
Constructing Job Components
You’ll need to complete four job components to monitor Twitter:- First, let’s input tweets that need a reply: tweets that mention your bot handle, but are not from the bot, and are not retweets, using standard Twitter search syntax.
- Then, we input all tweets posted by snoop_stein into the chatbot_output table.
- Then we create a view that compares the input tweet list to the output tweet list, filtering ones that haven’t been replied to yet into a view.
- Lastly, you join the model and prepare the replies (using a view).
Bringing it all together into a job
Jobs in MindsDB are similar to cron jobs in UNIX and Linux, running at regularly scheduled intervals. Write and then select the SQL below, and then execute it by clicking Run.What’s Next?
Use the MindsDB JavaScript SDK or the Python SDK to embed what you’ve built in this tutorial into your application code. While this simple example uses Twitter, MindsDB will soon support Slack and perhaps one day, Discord. Combined with the ability toFINETUNE existing, trained LLM models with your own data inside a relational database, a future where you can train chatbots to reason about data and/or content you provide doesn’t seem far off. Imagine a support bot that understands your digital assets and knowledge!
Explore more about how MariaDB Enterprise Server, MariaDB Columnstore, MaxScale Database proxy, and Xpand Distributed SQL can help your projects at The MariaDB developer hub, or get in touch with MariaDB experts on our slack.
Check out another Twitter Chatbot tutorial that offers an alternative way to simplify the process of fetching tweets with the
LAST keyword.Want to talk to SnoopStein? Post a tweet with @snoop_stein and wait for the reply.
