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.
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.
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>
Next, follow these steps to integrate the Voice API call flow with ChatGPT.
That’s it! In just minutes, we’ve successfully constructed a conversational AI bot. The infographic below shows more about how this flow works.
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.
1
2
3
4
from openai import OpenAI
client = OpenAI(
api_key='<OPEN_AI_API_KEY>',
)
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})
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
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