Build a conversational AI assistant by integrating OpenAI’s ChatGPT with Plivo’s programmable Voice API
AI-based virtual assistants can serve a range of business purposes, from accepting orders to providing support to helping customers make appointments. Plivo’s Voice API can help your business create a chatbot that automates key elements of your operations in just a few clicks. By integrating Plivo’s Voice API with any Large Language Model (LLM) service, such as OpenAI’s ChatGPT, you can create a conversational AI bot to interact with your customers efficiently.
Get started with Plivo
Before diving into the development of your AI bot, sign up for Plivo or sign in if you already have an account, and purchase a number through the Voice API or Plivo console.
The number you purchase through Plivo needs to be configured to receive customer calls, transcribe the customer request to text, send the text to ChatGPT, and relay a response generated by the AI back to the customer. Here’s how to set up your number to enable that entire interaction.
Connect your number to a Application
Once you have your number, link it to an application equipped to accept voice calls, transcribe speech, and forward this text to your WebHook. The code below can be used to perform this task.
Integrate Voice API with ChatGPT
Next, follow these steps to integrate the Voice API call flow with ChatGPT.
- Initiate a call to the designated number linked to an application that accepts voice calls (similar to the above example).
- Configure the “speak” element with an initial greeting (text-to-speech).
- When a customer speaks, speech from the caller is transcribed using Plivo’s GetInput element (speech-to-text).
- The transcribed text is then sent to your WebHook URL.
- You then need to send this transcribed text to ChatGPT for further processing.
- OpenAI’s Chat Completion API, is designed to simplify multi-turn conversations. It takes a list of messages as input and generates a response, which is then sent back to your WebHook server.
- Upon receiving the response from ChatGPT, the speak element (text-to-speech) converts it into speech.
That’s it! In just minutes, we’ve successfully constructed a conversational AI bot. The infographic below shows more about how this flow works.
Development guide
Let’s now look at how you can use Plivo’s Voice API and OpenAI’s ChatGPT to create a simple doctor’s appointment booking application.
Generate a response
Import the necessary libraries and install a ChatGPT client using your OpenAI API key.
Create the context for ChatGPT
Define Python functions to get messages and assistance based on the above messages.
Develop a local application capable of receiving callbacks from Plivo. Establish a WebHook URL linked to this local application. While we utilized localtunnel, any cross-platform application enabling the exposure of local web servers to the internet can be employed.
This application retrieves transcribed text from callbacks and sends it to ‘generate_response’ to obtain a response from ChatGPT. Additionally, it includes another method called ‘create_xml,’ utilizing the Plivo Python SDK to generate GetInput response XML for further user responses.