Build a ChatGPT-like SMS Chatbot with OpenAI and Python
Tags: python, openai, chatgpt, ngrok
Published: 2023-01-12 21:48:48

---
title: Build a ChatGPT-like SMS Chatbot with OpenAI and Python
published: true
description:Web browser who? Learn to build a ChatGPT-like SMS Chatbot using Python, Twilio, and the OpenAI API
tags: python, openai, chatgpt, ngrok
cover_image: https://assets.cdn.prod.twilio.com/images/Q9X36mYPCgds0eh21YnaGIGBzzkKLMMp9o0f03VW6M_nP.width-1616.png
# Use a ratio of 100:42 for best results.
# published_at: 2023-01-12 21:27 +0000
---
This blog post was written for [Twilio](https://www.twilio.com/) and [originally published on the Twilio blog](https://www.twilio.com/blog/sms-chatbot-openai-api-node).
[ChatGPT](https://openai.com/blog/chatgpt/) went viral recently. This conversational machine learning (ML) chatbot developed by [OpenAI](https://openai.com/) can answer questions, admit its mistakes, challenge incorrect premises, generate stories and poetry, and more. Read on to learn how to build a ChatGPT-like SMS chatbot using the OpenAI API and [Twilio Programmable Messaging](https://www.twilio.com/docs/sms) with Python.
![sms example asking for a haiku about SendGrid to be generated by ChatGPT](https://assets.cdn.prod.twilio.com/original_images/irgxCVXa7OgVEXV-BU5YUaLCTMRRIfxAbPmj-Dq0YcMdWneVMlLpOVMx82NKZkc69BhATjjHZ2_Kao)
Read [this blog post if you'd like to learn how to build the same application but using Node.js](https://www.twilio.com/blog/sms-chatbot-openai-api-node).
### ChatGPT and Python
GPT-3 (short for “Generative Pre-training Transformer 3”) is a natural language processing (NLP) model trained on human-generated text. Given some text input, it can generate its own human-like text based in a variety of languages and styles. Here, I ask it to "give me some bars about SendGrid."
![chat example of ChatGPT being asked for some bars about Twilio Segment](https://assets.cdn.prod.twilio.com/original_images/e3cwQT1JIJTU_gKl6Y-xYY_th5pO_XJlpYkYQHoaJLurPvS4pKUMjCu9OF2CLOJuBtZBBqMqCU6QTX)
You can [test it out in the browser here](https://chat.openai.com/chat).

The GPT-3 model uses a [transformer architecture](https://machinelearningmastery.com/the-transformer-model/). This multi-layer neural network is good for processing sequential data, like text. Language-related tasks it can perform include translation, summarization, and question answering, as well as text generation comparable to human text generation.

To make a ChatGPT-like application via SMS with Python, you must use the [OpenAI API](https://beta.openai.com/dashboard).
### Prerequisites
1. A Twilio account - [sign up for a free one here](https://www.twilio.com/try-twilio)
2. A Twilio phone number with SMS capabilities - [learn how to buy a Twilio Phone Number here](https://support.twilio.com/hc/en-us/articles/223135247-How-to-Search-for-and-Buy-a-Twilio-Phone-Number-from-Console)
3. OpenAI Account – [make an OpenAI Account here](https://openai.com/api/)
4. Python installed - [download Python here](https://www.python.org/downloads/)
5. [ngrok](https://ngrok.com/download), a handy utility to connect the development version of our Python application running on your machine to a public URL that Twilio can access. This is needed for the development version of the application because your computer is likely behind a router or firewall, so it isn’t directly reachable on the Internet. You can also choose to [automate ngrok as shown in this article](https://www.twilio.com/blog/automating-ngrok-python-twilio-applications-pyngrok).
### Configuration
Since you will be installing some Python packages for this project, you will need to make a new project directory and a [virtual environment](https://docs.python.org/3/tutorial/venv.html).

If you're using a Unix or macOS system, open a terminal and enter the following commands:
```bash
mkdir chatgpt-sms-python
cd chatgpt-sms-python
python3 -m venv venv
source venv/bin/activate
pip install openai twilio flask python-dotenv
```
If you're following this tutorial on Windows, enter the following commands in a command prompt window:
```bash
mkdir chatgpt-sms-python
cd chatgpt-sms-python
python -m venv venv
venv\Scripts\activate
pip install openai twilio flask python-dotenv
```
The last command uses `pip`, the Python package installer, to install the four packages that you are going to use in this project, which are:
1. The [OpenAI Python client library](https://pypi.org/project/openai/), to send requests to OpenAI's [GPT-3](https://www.fullstackpython.com/gpt-3.html) engine.
2. The [Twilio Python Helper library](https://www.twilio.com/docs/libraries/python), to work with SMS messages.
3. The [Flask](https://www.palletsprojects.com/p/flask/) framework, to create the web application in Python.
4. The [python-dotenv](https://pypi.org/project/python-dotenv/) package, to read a configuration file.
As mentioned above, this project needs an OpenAI API Key. After making an OpenAI account, you can [get an OpenAI API Key here](https://beta.openai.com/account/api-keys) by clicking on <strong>+ Create new secret key</strong>.
![openAI API keys](https://assets.cdn.prod.twilio.com/images/cPaoAYgpTi5cTblt2G5s-C2Vvv4xHh1V9rzb-wBX8t0EY.width-1000.png)
The Python application will need to have access to this key, so we are going to make a .env file where the API key can safely be stored. The application we create will be able to import this key as an environment variable soon.

Create a .env file in your project’s root directory and enter the following line of text, making sure to replace <OPENAI_API_KEY> with your actual key:
```bash
OPENAI_API_KEY= <YOUR-OPENAI-KEY>
```
Make sure that the `OPENAI_API_KEY` is safe and that you don't expose your <em>.env</em> file in a public location such as GitHub.

Now, your Flask app will need to be visible from the web so Twilio can send requests to it. ngrok lets you do this. With ngrok installed, run `ngrok http 5000` in a new terminal tab in the directory your code is in.
![ngrok in terminal showing URLs generated to copy and paste to configure twilio phone #](https://assets.cdn.prod.twilio.com/images/YDUTATKQVr85Uj_cWael1iQhVymew9E3XsSfMTTiZqt1k.width-1000.png)
You should see the screen above. Grab that ngrok Forwarding URL to configure your Twilio number: select your Twilio number under <strong>Active Numbers</strong> in your Twilio [console](https://www.twilio.com/console/phone-numbers/incoming), scroll to the Messaging section, and then modify the phone number’s routing by pasting the ngrok URL in the textbox corresponding to when <em>A Message Comes In</em> as shown below:
![phone number configuration in the console under where a message comes in](https://assets.cdn.prod.twilio.com/images/CqOXGybKIPpoovGDaIb-dnmxlOqYT6OVeBtj356saMA6R.width-1000.png)
Click <strong>Save</strong> and now your Twilio Phone Number is configured so that it maps to your web application server running locally on your computer. Let's build that application now.
### Build your ChatGPT-like SMS Python App
Inside your <em>chatgpt-sms-python</em> directory, make a new file called <em>app.py</em>.

Copy and paste the following code into <em>app.py</em> to start off the ChatGPT-like SMS app. It imports the required modules, sets the OpenAI API key from the <em>.env</em> file, and creates a Flask app.
```python
from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
app = Flask(__name__)
```
Directly beneath, add the following code:
```python
@app.route("/sms", methods=['POST'])
def chatgpt():
    """get incoming message"""
    inb_msg = request.form['Body'].lower()
    print(inb_msg)
    response = openai.Completion.create(
        model="text-davinci-003",
        prompt=inb_msg,
        max_tokens=3000,
        temperature=0.7
    )
    """Respond to incoming calls with a simple text message."""
    # Start our TwiML response
    resp = MessagingResponse()
    # Add a message
    resp.message(response["choices"][0]["text"])
    print(response["choices"][0]["text"])

    return str(resp)

if __name__ == "__main__":
    app.run(debug=True)
```
Inside of the `/sms` webhook, this code creates a variable `inbMsg` from the inbound text message users will text in and prints it out. It then calls the `openai.Completion.create` method to use one of their language models to generate text based on `inbMsg`.

In order to specify our completion, you pass in some properties: `model` identifies the OpenAI language model used to generate an answer for the text which we assign to the `prompt` property. In this tutorial, you use the `text-davinci-003` language model. It's the same language model used in the background by ChatGPT. The OpenAI docs list the other language models they offer for use.

Optional properties `max_tokens` and `frequency_penalty` specify the maximum completion length and the effort the model will make to not repeat itself. You can see more [optional properties for completion here in the OpenAI documentation](https://beta.openai.com/docs/api-reference/completions/create).

You can [view the complete app on GitHub here](https://github.com/elizabethsiegle/chatgpt-sms-python).

Run the Python Flask app by running the following command
```bash
python app.py
```
Now take out your phone and text your Twilio Phone Number a question or prompt so that OpenAI can answer it or generate text!
![sms example asking chatGPT for a haiku about SendGrid as well as the questions did humans invent or discover mathematics](https://assets.cdn.prod.twilio.com/images/Screenshot_2023-01-12_at_1.44.48_PM.width-1600.png)
### What's Next for Twilio and ChatGPT?
The development possibilities offered by ChatGPT and Twilio are endless! You can build an [SMS chatbot with Python](https://www.twilio.com/blog/openai-gpt-3-chatbot-python-twilio-sms), [call an AI friend](https://www.twilio.com/blog/call-ai-friend-gpt-3-twilio-voice-functions), [chat with an AI chef over WhatsApp](https://www.twilio.com/blog/python-whatsapp-chef-bot-openai-gpt3), use the [OpenAI API with Node.js and Twilio Serverless](https://www.twilio.com/blog/sms-chatbot-openai-api-node), and more. Let me know what you're working on with OpenAI or Python–I can't wait to see what you build.