paint-brush
How to Call ChatGPT with OpenAI's APIsby@plivo
1,587 reads
1,587 reads

How to Call ChatGPT with OpenAI's APIs

by PlivoApril 11th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Learn how Plivo is exploring the potential of ChatGPT, an AI-adjacent NLP tool from OpenAI, to help automate text messaging and voice calls using APIs. Find out how to trigger ChatGPT from Plivo and see how it works in a proof-of-concept program. Discover how you can get started with this technology and explore the possibilities for your own organization. While plugins for triggering actions in third-party applications are still in a limited alpha phase, calling ChatGPT from your service's APIs can already accomplish some useful tasks. Join the waitlist to join the program and get started with ChatGPT today.
featured image - How to Call ChatGPT with OpenAI's APIs
Plivo HackerNoon profile picture


At Plivo, we’ve been brainstorming about how ChatGPT, OpenAI’s AI-adjacent natural language processing tool, might help our customers. Our cloud communications platform provides APIs that let businesses automate sending and receiving text messages, as well as voice calls. The first step toward figuring out how ChatGPT could elevate those tasks has been to try it ourselves. We wrote this post to help other organizations get started more quickly.


Your application and ChatGPT can interact in two ways — think of them as “to” and “from.” An application action (in Plivo’s case an incoming call or text message, for instance) could be a trigger to invoke ChatGPT. For instance, someone might text a question to a given phone number designated to communicate with ChatGPT, or tagged with an #askChatGPT hashtag. We could then use OpenAI APIs to pass that query to ChatGPT and return the answer to the person who initiated the query.


Another use case, for data going in the other direction, would have someone invoke something from ChatGPT that triggers an action in a third-party application. For instance, you could go to the OpenAI’s Playground page and ask ChatGPT to text everyone in a list a weather report for their area.


Unfortunately, that second use case isn’t ready yet. Any application that wants to allow ChatGPT to use its APIs has to write an OpenAI plugin — and “plugins are a limited alpha,” according to the company. If your company wants to join the program, get on the waitlist now.


Still, calling ChatGPT from your service’s APIs can accomplish some pretty useful tasks.

From Plivo to ChatGPT

To see how a Plivo action could trigger ChatGPT, Plivo CTO Mike Ricordeau wrote a short proof-of-concept program in Python, using the Flask framework. His sms_bot.py code is publicly available on GitHub, so you can clone it and play with it.


To test the application, you’ll need a few bits of information, starting with an OpenAI API key. If you don’t have one already, sign up for an OpenAI account, then click View API Keys, and create a new secret key. Make sure you save the key somewhere secure because OpenAI will never display it for you again. Crisis? Not really — if you lose your key you can create a new one.


If you want to use Plivo as your test application, you’ll also need Plivo credentials. Sign up for a free account, and note the Auth Key and Auth Token on the front page of the Plivo console. You’ll also need to rent a Plivo phone number that’s capable of sending SMS messages; we give you plenty of credits so you don’t have to spend anything to get one.


The last variable you’ll need is the name of an OpenAI model or engine. We used GPT-3.5-turbo.


Now, turn to the instructions in the GitHub project’s README file. Follow the instructions to install, configure, and run the application on a local machine.


Before you deploy the application, you must expose it as a public URL. To do that, we recommend using a secure tunneling application such as ngrok. Make sure to specify to ngrok the same port on which the sms_bot.py application is running (8080 by default). Ngrok will return a forwarding IP address you can use.


Copy the forwarding address from ngrok, visit the Plivo console, and add a Plivo application. Specify the address, postpended with “/sms,” in the Message URL.






Next, click the phone number you rented and associate it with the application you just created. In the Application Type drop-down, choose XML Application, and under Plivo Application choose the name of the Plivo application you added.







Now the application should be live and available. You can try it by texting a question in the form of a message to the phone number you used.


What happens when you do that? The Plivo platform receives the message and POSTs it to the Message URL. Ngrok forwards it to the Python application running locally, which connects to OpenAI by using the APIs in the program and asks the question. OpenAI gives the answer. The Python application then returns a Message XML element that contains the response, which Plivo sends to the user’s phone number.





In our test setup the entire “transaction” took about four seconds from when we tapped Send to when we received a response.

Try It Yourself

This simple use case helped us understand how to use OpenAI APIs with Plivo. You can adapt what we did to your own use case. Let us know what you learn — we’re on Twitter @Plivo.