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.

<Response>
	<GetInput inputType="speech" action="
		{your_webhook_url}">
		<Speak>Welcome to Gleaneagles Hospital. I'm Ivy, and I'm here to assist you in scheduling an appointment. Please describe your symptoms, or if you have a specific doctor in mind, please provide their name.</Speak>
	</GetInput>
</Response>

Integrate Voice API with ChatGPT

Next, follow these steps to integrate the Voice API call flow with ChatGPT.

Integrate Voice API with ChatGPT

  1. Initiate a call to the designated number linked to an application that accepts voice calls (similar to the above example).
  2. Configure the “speak” element with an initial greeting (text-to-speech).
  3. When a customer speaks, speech from the caller is transcribed using Plivo’s GetInput element (speech-to-text).
  4. The transcribed text is then sent to your WebHook URL.
  5. You then need to send this transcribed text to ChatGPT for further processing.
  6. 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.
  7. 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

  1. Import the necessary libraries and install a ChatGPT client using your OpenAI API key.
    1
    2
    3
    4
    
    from openai import OpenAI
    client = OpenAI(
    api_key='<OPEN_AI_API_KEY>',
    )
    
  2. Create the context for ChatGPT
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    
    messages = []
    
    system_msg = """Your task is to serve as an interactive and supportive assistant, aiding in scheduling and issuing appointments for Gleaneagles Hospital. The user can either directly tell the doctor's name or tell his symptoms based on which you can suggest a doctor. If user provides symptoms, query him more so that you can correctly identify the appropriate doctor. You wait to collect the entire appointment details, then summarize it and check for a final time if the patient wants to add anything else. Makesure to clarify all options, departments, doctors, timings to uniquely identify the department from the departments. You respond in a short, very conversational friendly style. 
    If user asks any other question, you will let him know politely that you can't help him with this and tell him to ask relevant question.
    The departments includes:
    Cardiologist
    Neurologist
    Gynaecologist
    Urologist
    Physiotherapist
    Dermatologist
    General Physician
    
    Available list of Doctors:
    Dr Leena S Sridhar, Gynaecologist
    Dr Barnali Ghosh, Gynaecologist
    Dr Anand R Shenoy, Cardiologist
    Dr Deepak Padmanabhan, Cardiologist
    Dr Akash J, Neurologist
    Dr Lakshmi Krishna V, Neurologist
    Dr Manohar Bhadrappa, Urologist
    Dr Harsha R, Urologist
    Dr Akshi Bansal, Dermatologist
    Dr Sachith Abraham, Dermatologist
    Dr Ria Emmanuel, General Physician
    Dr Satish Babu K, General Physician
    Abraham S S, Physiotherapist
    Ashwin Bhat H, Physiotherapist
    
    The timings includes: 9:00, 10:00, 11:00, 12:00, 15:00, 16:00, 17:00
    The Doctors will be available from Monday to Saturday. 
    
    If it is an emergency, the patient can come anytime in the Emergency Unit and can meet the available doctor.
    """
    
    messages.append({"role": "system", "content": system_msg})
    
  3. Define Python functions to get messages and assistance based on the above messages.
    1
    2
    3
    4
    5
    6
    7
    8
    
    def generate_response(input_message):
        messages.append({"role": "user", "content": input_message})
        response = client.chat.completions.create(
            model="gpt-3.5-turbo",
            messages=messages)
        reply = response.choices[0].message.content
        messages.append({"role": "assistant", "content": reply})
        return reply
    
  4. 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.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    @app.route('/webhook', methods=['POST'])
    def webhook():
        input_message = request.form.get('Speech')
        response = generate_response(input_message)
        xml_response = create_xml(response)
        return Response(xml_response, mimetype='text/xml')
        
    def create_xml(text):
        element = plivoxml.ResponseElement()
        response = element.add(
            plivoxml.GetInputElement().
            set_action('<action url>').
            set_input_type('speech').
            set_language('en-US').
            set_execution_timeout(10).
            set_speech_end_timeout(10).
            add_speak(content=text, language='en-US')
        ).to_string(True)
        print(response)
        return response
    
    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.