How to Build Amazing AI Use Cases under 10 Mins using GPT-3? | by Sharan Kumar Ravindran | Towards Data Science

Do you think that building an NLP based AI application such as a chatbot or a Translator requires a lot of data science skills and would take a lot of time? It is not always true, this article will help you better understand. I am going to show how to use GPT-3 to build some amazing NLP based AI applications with minimal development effort. Before getting into the build let’s first understand what GPT-3 is and what are all the features of GPT-3 that make it so special.
GPT-3 is Generative Pre-trained Transformer 3, it is a language model trained on a vast number of datasets from the internet and developed by a company called OpenAI. GPT-3 is being offered via API, currently, the API is in a controlled beta and there is a waitlist for one to gain access to this amazing API.
Below are some reasons why the GPT-3 model has been most talked about for
GPT-3 model consists of 175 billion parameters, the previous version, GPT-2 model had only 1.5 billion parameters. Parameters are weights in a neural network model that transforms the inputs to output
It is a generative model which means that it has the ability to generate a long sequence of words that is coherent as an output
This state-of-the-art language model can respond to almost any question passed on to it and that too in a more humane way
Billions of words, text, and code snippets used in model training hence make it capable of auto code in a wide range of programming languages
Its Multilingual text processing help to work on languages other than English as well
The best part is GPT-3 model can perform a specific task, such as being a translator or a chatbot or code builder without any customization or any special tuning, all it needs is a few training examples
To learn more about the technical details of this amazing model check out here. Here in this article, I am going to show you how this amazing API can be used to solve different NLP based AI use-cases.
To build the use-cases covered in this article, you need to have access to the GPT-3 beta API. It is currently available only on an invite, you can apply for access using the below link. It will take you to a form where a couple of questions will be asked about your organization and the project you are planning to implement.
To interface with the GPT-3 API, I am going to use a script from the following gpt3-sandbox repository. The script I am using from this repository is gpt.py available at the API folder that enables me to access the GPT-3 API hence making it possible to show different use-cases that can be solved. The scripts used in this article can be found here
Here we are not going to pass any training examples but just directly access the API and use it as a chatbot.
In the below example, I am importing the required packages as well as the script we have downloaded from the “gpt3-sandbox” repository. We will be passing three arguments to the model they are,
Engine — There are four options for us to choose from, they are Davinci, ADA, Babbage, and Curie. We will be using Davinci as it is the most powerful engine trained using 175 billion parameters
Temperature — This is usually between the range 0 and 1, it is used to control the randomness of the generated output. A value of 0 makes the model deterministic that is, the output will be the same every time we execute whereas on the other hand with 1 there will be high randomness in the output generated.
max_tokens — maximum completion length
In the below script the question that needs to be asked is passed on to the variable “prompt1” and then passed to the model using the submit_request function. The result is stored in the ‘output1’ variable as shown below,
import json
import openai

from gpt import GPT
from gpt import Example

import config 

# Reading the API key from a file
service_key = config.api_key
openai.api_key = service_key

# Application 1 - Directly use as chatbot Use-case
gpt1 = GPT(engine="davinci", temperature=0.2, max_tokens=100)
# Input to the Model
prompt1 = "How to learn data science?"
output1 = gpt1.submit_request(prompt1)
# Model Output
output1.choices[0].text

'\nI think the best way to learn data science is to do data science.\n\nI’ve been doing data science for a few years now, and I’ve learned a lot from doing it. I’ve learned a lot from reading books and blogs, but I’ve learned the most from doing it.\n\nI’ve learned a lot from doing data science, and I’ve learned a lot from reading books and blogs.\n\nI'
In the above example, as you see the model can come up with a really good response for the question asked with no tweaking or tuning. Since here we are not providing any training examples the model output need not always be a response it could come up with a similar set of questions or with content related to the input passed. The performance can be improved by supplying some training examples
In the below example we are going to see the implementation of text to equation conversion with very minimal training which is not possible with other pre-trained models.
In the below example the temperature value has been increased to bring some randomness to the response and we are also passing some predefined examples to the model as a training dataset. With just 5 examples we could train the model to convert the text to an equation. After training the model with the known examples we pass the following as input “x squared plus 2 times ” and the model converts it into an equation.
# Application 2 - LaTex Use-case - Text to Equation
gpt2 = GPT(engine="davinci", temperature=0.5, max_tokens=100)
# Examples to train as LaTex Application
gpt2.add_example(Example('Two plus two equals four', '2 + 2 = 4'))
gpt2.add_example(Example('The integral from zero to infinity', '\\int_0^{\\infty}'))
gpt2.add_example(Example('The gradient of x squared plus two times x with respect to x', '\\nabla_x x^2 + 2x'))
gpt2.add_example(Example('The log of two times x', '\\log{2x}'))
gpt2.add_example(Example('x squared plus y squared plus equals z squared', 'x^2 + y^2 = z^2'))

# Input to the model
prompt2 = "x squared plus 2 times x"
output2 = gpt2.submit_request(prompt2)
# Model Output
output2.choices[0].text

'output: x^2 + 2x\n\n'
 

Like above use-case with a few examples, we could train the model to behave like a translator. Below is the code for the translator module where we train the model to translate the text in English to French with just three examples.
# Application 3 - Translator Use-case
gpt3 = GPT(engine="davinci", temperature=0.5, max_tokens=100)
# Examples to train as a English to French translator
gpt3.add_example(Example('What is your name?', 'quel est votre nom?'))
gpt3.add_example(Example('What are you doing?', 'Que faites-vous?'))
gpt3.add_example(Example('How are you?', 'Comment allez-vous?'))

# Input to the model
prompt3 = "where are you?"
output3 = gpt3.submit_request(prompt3)
# Model output
output3.choices[0].text

'output: Où êtes-vous?\n\n'
 

This ability of the model to perform a specific task like “foreign language translator” or “text to equation converter” with very minimal development effort makes it very special.
Below is a tutorial video where I provide a walk-through of the use-case implementation.
To learn more about interesting applications built using GPT3 check this page here.
I am a Data Science professional with over 10 years of experience and I have authored 2 books in data science. I write data science-related content intending to make it simple and accessible. Follow me up at Medium. I have a YouTube channel as well where I teach and talk about various data science concepts. If interested, subscribe to my channel below.