Implementing a Chatbot for Slack using MindsDB and OpenAI's GPT Model
The objective of this tutorial is to create an AI-powered personalized chatbot by utilizing the MindsDB’s Slack connector, and combining it with OpenAI’s GPT-4 Model.
To illustrate practically, we will be having a Slack bot - @Whiz_Fizz - which will reply to the user’s queries with proper context and with a unique persona while responding. It is a weird magician 🪄 and a Space Science Expert! Let’s see how it responds.
Before Jumping more into it. Let’s first see how to create a bot and connect it to our Slack Workspace.
Getting Started
- Create an account on MindsDB cloud (if you don’t have one yet).
- Create a Slack Account and follow this instruction to connect Slack to MindsDB.
- Go to your MindsDB SQL Editor
MindsDB is a famous open-source application used for simplifying the use of Machine Learning in modern-day applications without the need for any external tool by making forecasts on the Data available in your database. It lets you build any Generative AI application with minimal steps required.
Usage
This query will create a database called mindsdb_slack
and channels
table automatically.
Here is how to retrieve the 10 messages after specific timestamp:
Please change the channel-name
in the WHERE
clause to the channel where, you added the bot in your Slack Workspace.
However, you can also retrieve messages in alphabetical order by using:
By default, it retrieves by the order the messages were sent, unless specified ascending/descending.
Here is how to post messages:
Whoops! Sent it by mistake, No worries, use this to delete a specific message
Now, let’s roll up our sleeves and start building the GPT-4 Model together. Here’s how we can do it:
1. Crafting the GPT-4 Model:
Generating a Machine Learning model with MindsDB feels like taking a thrilling elevator ride in Burj Khalifa (You don’t realize, that you made it)!
Here gpt_model
represents our GPT-4 Model.
Before creating an OpenAI model, please create an engine, providing your OpenAI API key:
If you are using MindsDB on Docker or if you want to use your own OpenAI API Key, simply remove --
from api_key
argument.
The critical attribute here is prompt_template
where we tell the GPT model, how to respond to the questions asked by the user, feel free to change any values passed in the columns, from our example, {{messages}}
, will be replaced by the WHERE
clause provided in the query. Let’s see how it works:
2. Feeding Personality into Our Model
Alright, so the old model’s replies were good. But hey, we can use some prompt template tricks to make it respond the way we want. Let’s do some Prompt Engineering.
Now, let’s make a model called whizfizz_model
with a prompt template that gives GPT a wild personality that eludes a playful and magical aura. Imagine scientific knowledge with whimsical storytelling to create a unique and enchanting experience. We’ll call him WhizFizz:
Let’s test this in action:
You see the difference! Now, I’m getting excited, let’s try again.
3. Let’s Connect our GPT Model to Slack!
The database has channels
table. This table can be used to search for channels
, messages
, and timestamps
, as well as to post messages
into slack channels. These functionalities can also be done by using Slack API or Webhooks.
Let’s query the user’s question and see how our GPT model responds to it, by joining the model with the channels
table:
4. Posting Messages using SQL
We want to respond to the user’s questions by posting the output of our newly created WhizFizz Model. Let’s post the message by querying and joining the user’s questions to our model:
Works like a charm!!
5. Let’s automate this
We will CREATE JOB
that means the query which we will give, will execute after some interval. Let’s schedule a job every hour. The job will do the following:
- Check for new messages
- Generate an appropriate response
- Post the response into the channel
Let’s do it in single SQL statement:
That sums up the tutorial! Here it will continually check for new messages posted in the channel and will respond to first 10 messages through responses generated by OpenAI’s GPT model in the style of WhizFizz.
To check the jobs
and jobs_history
, we can use the following:
To stop the sheduled job, we can use the following:
What’s next?
Check out How to Generate Images using OpenAI with MindsDB to see another interesting use case of OpenAI integration.