Build an SMS AI Agent with GPT, Twilio, and MindsDB
Call it the Costume Curator. A souped-up Halloween Outfit Optimizer.
Your friends over at MindsDB and Twilio have dreamed up a way for an imaginary celebrity—a mashup between Bill Murray and Taylor Swift—to help you pick the perfect Halloween costume, harnessing the power of artificial intelligence.
First, meet Billor Swift. She’s got all the wit of Bill Murray but is as magnetic as the musician-phenom Taylor Swift.
It’s Halloween, and Billor Swift can help you answer one of the holiday’s most perplexing dilemmas: What should I wear?
To solve the problem, we’re going to show you how to create Billor Swift, a character-based chatbot that interacts with you via text message and image.
Meet 'Billor Swift', a chatbot mashup of Bill Murray and Taylor Swift who can help you pick the perfect Halloween costume. Try it yourself by texting a costume question to +16122550527.
To make it simple, we’ll use three tools.
First, Twilio. You’ve probably heard of it, but if not, it’s the platform that lets developers (or anyone, really) make and receive voice and video calls, and send and receive text messages through its APIs.
For quick tutorials on how to use Twilio, click here.
We’ll bring everything together in MindsDB. Our platform connects real-time data to AI. The platform lets companies prototype and deploy AI-powered enterprise solutions quickly and at scale.
In this case, MindsDB will act as an orchestration layer between Twilio and OpenAI, maker of popular chatbots like ChatGPT. We’ll make use of a handy integration between MindsDB and Twilio, then incorporate a few custom prompts.
MindsDB is easy to use for developers at all skill levels. The platform brings AI to the data layer, and the programming language SQL is the most common way developers manage AI workflows. In this tutorial, we’ll use SQL. Knowing a few SQL commands will come in handy, but no worries if not. You can also use an API or the MongoDB query language. (If you get stumped, ask us a question through Community Slack.)
1. Create OpenAI models with a bit of personality.
In order to create an AI model, you’ll need an OpenAI account and an API key. You’ll also need a MindsDB account. (There’s an open-source version of MindsDB here.)
Then go to your MindsDB SQL Editor and enter the following commands to create AI models:
I. Model to generate a text response:
CREATE MODEL halloween_model
PREDICT answer
USING
engine = 'openai',
api_key = 'input-your-openai-api-key',
prompt_template = 'Pretend you are a mashup of Bill Murray and Taylor Swift. You are giving advice about the perfect Halloween costume. The costumes should be groundhog-like and have a lot of fur, but also glamorous and have plenty of sequins. Answer users questions: {{body}}';
The CREATE MODEL command creates and deploys the model within MindsDB. As we want to create an OpenAI model, we must define the engine as OpenAI and provide the OpenAI API key. The prompt_template message sets the personality of the bot, which is a mashup of Bill Murray and Taylor Swift.
II. Model to generate an image response:
CREATE MODEL halloween_image_model
PREDICT img_url
USING
engine = 'openai',
mode = 'image',
api_key = 'input-your-openai-api-key',
prompt_template = '{{body}}, 8K | highly detailed oil painting style groundhog-like and a lot of fur and glamorous | cinematic lighting | happy colors';
In this CREATE MODEL command, we add the mode as image to ensure the model's output is a link to an image.
Feel free to use the suggested prompt text above, or get creative with your own prompt language.
Then you can ask questions and get a reply from the models:
I. Model to generate a text response:
II. Model to generate an image response:
The img_url link leads to this image:
2. Set up your Twilio account and connect it to MindsDB.
You can set up a Twilio account here, and then you get a virtual phone number in the console. This virtual number will be the one that sends a text to your personal number.
Save the account string identifier (SID), auth token, and virtual phone number.
Use this command to connect the Twilio account to MindsDB:
CREATE DATABASE twilio
WITH
ENGINE = 'twilio',
PARAMETERS = {
"account_sid":"input-your-account-sid",
"auth_token":"input-your-auth-token"
};
The Twilio connection comes with the default messages table.
To learn more about how to generate tokens required to connect Twilio accounts to MindsDB, follow this link.
3. Write and read messages from your Twilio virtual number using MindsDB.
Here is how you can write messages:
INSERT INTO twilio.messages (to_number, from_number, body)
VALUES("+10001112222", "+13334445555", "This is a message sent from Twilio.");
Please replace +13334445555 with the virtual number from Twilio and +10001112222 with the receiver’s phone number.
Here is how you can read messages:
SELECT *
FROM twilio.messages
WHERE sent_at > LAST AND msg_status = 'received';
The INSERT command is used to send messages from your Twilio account to any phone number provided as the to_number argument.
4. Make sure the model replies to texts from Twilio.
Use this command to write messages generated by the models:
INSERT INTO twilio.messages (to_number, from_number, body, media_url)
SELECT outputtext.to_number AS to_number,
outputtext.from_number AS from_number,
outputtext.body AS body,
outputimage.img_url AS media_url
FROM (
SELECT input.from_number AS to_number,
input.to_number AS from_number,
output.answer AS body
FROM twilio.messages AS input
JOIN halloween_model AS output
) AS outputtext
JOIN halloween_image_model AS outputimage;
Here we chain the models in order to provide a text response (generated by the halloween_model) and an image response (generated by the halloween_image_model). The image is generated based on the text response created by halloween_model.
5. Pull it all together and automate model-generated replies.
Use this command to create a job that replies to incoming messages every minute:
CREATE JOB twilio_bot_halloween (
INSERT INTO twilio.messages (to_number, from_number, body, media_url)
SELECT outputtext.to_number AS to_number,
outputtext.from_number AS from_number,
outputtext.body AS body,
outputimage.img_url AS media_url
FROM (
SELECT input.from_number AS to_number,
input.to_number AS from_number,
output.answer AS body
FROM twilio.messages AS input
JOIN halloween_model AS output
WHERE input.sent_at > LAST AND input.msg_status = 'received'
) AS outputtext
JOIN halloween_image_model AS outputimage;
)
EVERY 1 minute;
We use a MindsDB-custom CREATE JOB statement to schedule a job that replies to messages every minute.
In the inner SELECT statement, we use a custom LAST keyword implemented by MindsDB to make it easier to fetch only the recently received messages. The LAST keyword ensures that only the messages that haven't been replied to yet will be selected in the current job run.
Here are the sample answers:
Try it yourself! Send a message to +16122550527 and ask about Halloween costume ideas to get Billor's replies.
The Billor Swift bot will help you get your costume just right.
This may seem like a funny task for AI, or a wacky mashup of personalities. (Truly, it’s about getting in the Halloween spirit with a dash of humor.)
But this exercise is about more than finding a costume. Creating a celebrity bot can be the beginning of your journey in building AI that’s useful to you.
MindsDB has hundreds of integrations, and the possibilities are endless. Want a bot that recommends Halloween events near you? Pull in Eventbrite or Luma as a data source and connect it up through MindsDB. Want to poll your friends about their Halloween costumes on Reddit? MindsDB connects to Reddit automatically.
Whatever you want to build, get started here.
Check us out on GitHub to see how you can contribute. Love what we’re doing? Give us a high five and star our repo.