This content originally appeared on Twilio Blog and was authored by Felistas Ngumi
Learning a new language can be hard and sometimes enrolling to a language school is not enough. The only way around it, is constant practice. At times, those around you might not share a similar interest or you might not be making enough progress from the learning options you have. As this can get challenging in the long run and demotivate you from learning the language, why not build an AI powered chatbot to lessen the burden? Check out the chat I had below with the GPT-3 language model:
In this tutorial, I will walk you through on how to build an AI chatbot to help and encourage you to practice a language that you are learning, in a creative and fun way using the most popular language model of 2020, GPT-3! As I’m in the quest of being fluent in German, I will use this language as an example for this tutorial.
Prerequisites
To complete this tutorial you will need the following:
- Twilio account. Sign up for free if you don’t have one. Use the link provided to obtain $10 credit once you upgrade to a paid account.
- Node version 12 or newer.
- OpenAI account to obtain an API Key for the GPT-3 model and access to the Playground.
- Ngrok to expose our application to the internet.
- A phone with WhatsApp installed and an active mobile number.
What is GPT-3?
GPT-3, Generative Pretrained Transformer 3, is the largest language model ever created (at the time of this writing) with 175 billion parameters! It was developed by researchers at OpenAI and it is so powerful that it produces indistinguishable human-like text. The language model was announced in May 2020 and has not been released to the public yet since then. You can, however, request private beta access here. GPT-3 has a range of capabilities from content generation, code generation, semantic search, chat and many more.
OpenAI researchers provided an API and a Playground, which is fairly simple to use and interact with. Here’s an example of some text I fed into the model via the Playground for the model engine to autocomplete greetings in different languages:
Text: Hi, How are you doing?
Language: English
Text: Hallo, Wie gehts es dir?
Language: German
Text: Salut, Comment ca va?
Language:
Here's the output i got:
Aside from providing a nice playground interface, folks at OpenAI have also provided a way in which you can export the playground configuration and integrate it with your Python code to use in your application by clicking on the code icon (<>) as shown below:
In this tutorial, however, I will show you how to do this in Node and Javascript.
Activate Twilio Sandbox for WhatsApp
After obtaining your Twilio account, navigate to the Twilio console and head over to the `All Products & Services` tab on the left. Select `Programmable Messaging`and click on `Try it Out` then `Try WhatsApp` to activate the sandbox as shown below:
Send the code displayed on your console (join ****), as a WhatsApp message from your personal mobile number to the WhatsApp sandbox number indicated in the page. After a successful activation, you should receive a confirmation message stating you are connected to the sandbox and are all set to receive and send messages.
Set up your development environmentIn your preferred terminal, run the following commands:
mkdir gpt3-whatsapp-chatbot
cd gpt3-whatsapp-chatbot
npm init -y
npm install twilio@3.54.2 axios@0.21.1 express@4.17
The above commands create our working directory, initializes an empty Node.js application and installs the dependencies needed to build the application which are:
- Twilio Javascript library to work with Twilio APIs.
- Axios , a helper library for making HTTP requests.
- Express Js for building our small API.
Send GPT-3 Requests with Node.js
Inside our project folder gpt3-whatsapp-chatbot
, create a file called bot.js
and in your favorite text editor, add the following lines of code.
const axios = require('axios');
async function complete(prompt) {
let response;
const url = 'https://api.openai.com/v1/engines/davinci/completions';
const params = {
prompt,
max_tokens: 90,
temperature: 0.9,
stop: 'Human:',
};
const headers = {
Authorization: `Bearer ${process.env.OPENAI_SECRET_KEY}`,
};
await axios({
method: 'post',
url,
data: params,
headers,
}).then((msg) => {
response = msg.data.choices[0].text;
});
return response;
}
module.exports = complete;
(async () =>