Build a Telephone Chatbot with GPT-3 and Autopilot
GPT-3 is a new language model from OpenAI that is able to generate text that is indistinguishable from human-written text. In this tutorial, we're going to use GPT-3 and Twilio Autopilot to create a chatbot that users can talk with over the telephone.
Technical Requirements
To follow along, you’ll need the following:
- A free Twilio account. If you use this link to register, you will receive $10 credit when you upgrade to a paid account.
- An API key for the OpenAI API
Setting up a phone number for your bot
[to be completed]
Creating an Autopilot Bot
Let's get started by creating a new Autopilot bot. We'll use the Start from scratch
option to create a simple bot that we'll modify as we move through the tutorial.
To create your Autopilot bot, login to the Twilio web console and navigate to the Autopilot console by choosing All Products & Services -> Autopilot
.
NOTE: The 'All Products & Services' option is the ellipse icon on the sidebar menu.
- Click the
Build from Scratch
or theStart from scratch
button. - In the Start from scratch modal window, set the unique name to
gpt3-telephone-bot
- Click the
Create bot
button to create your bot - Set the fallback behavior to the
greeting
task
Create a Twilio Functions to Handel Autopilot Requests
By default, new Autopilot bots use static JSON to respond to task requests from users. But since we'll be generating our responses using GPT-3 we'll need to respond to user requests dynamically. To make that happen, we'll use a Twilio function.
- Create a function using the
blank
template - Set the name to
gpt3-telephone-bot
and path to/gpt3-telephone-bot
- Create a
OPENAI_API_KEY
environment variable with your OpenAI API Key - Add the
axios
npm package as a dependancy - Replace the template code with the following
const axios = require('axios');
//autopilot request handler
exports.handler = async (context, event, callback) => {
const response = {};
response.actions = [];
response.actions = await greetingHandler(event);
callback(null, response);
};
//greeting task response
const greetingHandler = async (event) => {
const actions = [];
const instance = axios.create({
baseURL: 'https://api.openai.com/v1/',
headers: { 'Authorization': `Bearer ${process.env.OPENAI_API_KEY}` }
});
const turns = [
'This is a a bot that asks and a human questions. The bot is funny and friendly.\n',
'bot: Hello, how are you today?',
`human: ${event.CurrentInput}`,
'bot:'
]
const completionParmas = {
"prompt": turns.join('\n'),
"max_tokens": 75,
"temperature": .65,
"n": 1,
"stream": false,
"logprobs": null,
"echo" : false,
"stop": "\n"
}
await instance.post('/engines/davinci/completions',completionParmas).then(result => {
const botResponse = result.data.choices[0].text.trim();
const log = {
"id" : result.data.id,
"inputs" : completionParmas,
"result" : result.data,
"turns" : turns
}
turns[3] = `bot: ${botResponse}`
console.log(log);
actions.push({"say" : botResponse});
}).catch(err => {
console.log(err);
actions.push({"say" : botResponse});
});
actions.push({"listen" : true});
return actions;
}
Understanding the OpenAI API
[to be completed]
The OpenAI API is a "text in/text out"
Calling the OpenAI API from a Twilio Function
[to be completed]
Require the
axis
npm moduleconst axios = require('axios');
Create an instance of
axis
for calling the OpenAI APIconst instance = axios.create({
baseURL: 'https://api.openai.com/v1/',
headers: { 'Authorization': `Bearer ${process.env.OPENAI_API_KEY}` }
});
Responding to users using GPT-3
[to be completed]
Wrapping up
[to be completed]