This guide will help you set up and deploy the very basic Shasta (or whatever name you choose) textbot using OpenAI, Flask, Heroku, and Twilio.
This will go much more smoothly if you have some experience with basic python, cl commands, and integrated services experience. With that said, this bot is easily deployable by non-coders.
Shasta is the name of the hero from CS Lewis's Narnia Chronicles', "The Horse and His Boy". A book I read as a kid.
This text bot is repurposed from OpenAI's "Marv" bot that's designed to be nothing but a snarky assistant with no power to do anything substantial (like access the internet). If you tool around in the Marv bot OpenAI playground you can tweak the models to get desired responses, then replicate those model settings in your main.py script.
The instructions below will walk you through the process of creating an OpenAI API key, setting up a Heroku app, configuring Twilio, and modifying the textbot responses.
- Go to the OpenAI website and create an account if you don't have one already.
- After signing up or logging in, navigate to the API Keys page.
- Click on the "Create API key" button and take note of the generated key. This key will be used to authenticate requests to the OpenAI API.
- Sign up for a Twilio account if you don't have one already.
- After signing up or logging in, go to the Phone Numbers page and click on "Get a Trial Number" or "Buy a Number" if you want a specific number.
- Once you have a Twilio phone number, navigate to the phone number's configuration page.
- Scroll down to the "Messaging" section and set the "A MESSAGE COMES IN" webhook to your Heroku app's
/sms
endpoint (e.g.,https://your-heroku-app.herokuapp.com/sms
). Make sure the request method is set to "HTTP POST". - Save your changes on the Twilio phone number configuration page.
- Sign up for a Heroku account if you haven't already.
- Install the Heroku CLI and log in by running
heroku login
in your command line. - In the root directory of your project, run
heroku create
to create a new Heroku app. - Push your code to Heroku by running
git push heroku main
. - Set the environment variables on Heroku:
heroku config:set OPENAI_API_KEY=your_openai_api_key
heroku config:set TWILIO_PHONE_NUMBER=your_twilio_phone_number
- (Optional) Scale your app by running
heroku ps:scale web=1
. - Visit your app's URL (displayed in the Heroku dashboard) to check if it's running.
To keep your API keys and other sensitive information secure, use a .env
file to store them. This file should be added to your project's .gitignore
so that it is not tracked by version control and accidentally shared.
Create a .env
file in the root of your project and add the following variables:
OPENAI_API_KEY=your_openai_api_key
TWILIO_PHONE_NUMBER=your_twilio_phone_number
Replace your_openai_api_key
and your_twilio_phone_number
with the actual values you obtained from OpenAI and Twilio.
To modify the chatbot responses, you can adjust the parameters passed to the openai.Completion.create() function in
file. Some of the key parameters to tweak are:
temperature
: Controls the randomness of the generated responses. Higher values (e.g., 1.0) will make the output more random and creative, while lower values (e.g., 0.2) will make it more focused and deterministic.max_tokens
: The maximum number of tokens (words or word pieces) in the generated response. Adjusting this value controls the length of the response.top_p
: This parameter controls the nucleus sampling. Adjusting this value may influence the diversity of the generated responses. A higher value (e.g., 0.8) may result in more diverse responses, while a lower value (e.g., 0.3) will likely result in more conservative ones.frequency_penalty
: This parameter can be used to encourage or discourage the use of common words or phrases. A higher frequency_penalty value (e.g., 0.8 or even 1.0) will encourage the model to generate less frequent or more unusual words and phrases, which can lead to more creative outputs.presence_penalty
: This parameter influences the repetition of words or phrases within the generated response. A higher presence_penalty value (e.g., 0.5 or even 1.0) will discourage the model from repeating the same words or phrases within the response, which can lead to more creative and diverse outputs.
Keep an eye on the costs associated with using OpenAI, Heroku, and Twilio services, as some of them may have fees depending on your usage.
-
Twilio
: Bills at$.0079 per in/outbound text. That's $ .0158 per conversation. This can add up quickly. Carrier fees usually average 50% of your conversation fees, so around $.008 in carrier fees per post and response. The initial fee for phone number setup is a one-time $4. There's also a$1.55/month local phone number fee and $ .75/month 911 fee. -
Heroku
: This will work consistently provided you use the Hobby level dyno Gunicorn (Green Unicorn). This costs $7/month. If you turn off the Hobby dyno and opt for free, you may notice delayed responses or configuring error notices sent from twilio.
Make sure to monitor your usage and adjust your plan accordingly to avoid unexpected charges. This goes for Heroku.
Most importantly, be mindful when sharing your Twilio phone number. If people share your bot it could spread exponentially, leading to fees you never intended!