NLP project
Let’s get started with NLP APIs like GPT-3 & GPT-Neo.
GPT-3
Getting started with GPT-3 is as easy as signing up to an email waiting list by clicking here. The Beta-program access is approved after few weeks. Then, you will be able to create an account for the OpenAI API with 3 months to spend the free $18.00 credit. There is a getting started tutorial.
There are important facts to know prior using GPT-3. Microsoft’s has an exclusive license of GPT-3. It is not open source. The pricing policy is based on consumption and the engine used.
The OpenAI API includes engines called Ada, Babbage, Curie and Davinci. The engine-documentation allows to pick the right engine. For example Davinci-engine offers highest language capability. Curie-engine is faster and with 1/10th of price.
The OpenAI API’s Playground lets to test GPT-3’s capabilities under various NLP tasks. We made few Q&A-pairs here to showcase the Davinci-engine. GPT-3 is able to provide facts.
Q: Which year Elvis Presley died?
A: Elvis Presley died in 1977
Yet, as seen below, it is capable of making very simple errors.
Q:What day is today?
A: Today is Monday, January 1, 2018.
This flaw can be programmatically fixed. “State-of-the-Art”-models have still deep limitations. We need to understand this point, if we use them in production environments.
GPT-3 is available programmatically with the below code from Jupyter notebook. Libraries must be installed first. Second step is to connect to the API using secret key. Final step is to print the output.
!{sys.executable} -m pip install openaiimport os
import sys
import openaiopenai.api_key = "Insert here your Open AI API secret key"
response = openai.Completion.create(engine="davinci", prompt="I expect", max_tokens=5)print(response["choices"][0]["text"])
The “engine” parameter lets us choose the engine. The “max_tokens” defines the number of words to be generated. The “prompt”-parameter is the beginning of the sentence input. The secret key is available in the Open AI API. The output is JSON response. We can select from the JSON the desired data to be printed.
GPT-3 is capable of making various types of queries. The one used here is “Completion” by stating “openai.Completion”. This will provide one or many predicted completions. The other query types are: “Searches”, “Classifications” and “Answers”. For example “Answers” would provide response to a question.
GPT-Neo
GPT-Neo is open-source alternative to GPT-3. Three lines of code are required to get started:
#Install Transformers library:import sys
!{sys.executable} -m pip install git+https://github.com/huggingface/transformers# Import the pipeline:
from transformers import pipeline#Generate text, which starts with "My day..."
generator("My day", do_sample=True, min_length=100)[{'generated_text': 'My day off, as we live in the city, was nice. I went in the city for dinner with my family. My dad and uncle got me a new kitchen appliance-- a food processor! What I love about the city is-- I can'}]
The above implementation relies on the Transformer-open source library by HuggingFace. Second method is to use the HuggingFace API with either its free or paid pricing plan. The third option is to use Colab & Google Cloud-bucket implementation.
GPT-Neo is useful open source alternative for GPT-3. Yet, the GPT-Neo model size still smaller compared to the largest engines of the GPT-3. Everybody will need to carefully review costs prior selecting between these two models. The usage of GPT-Neo via HuggingFace API has a monthly charge in commercial use. There are costs associated with using bucket in Google Cloud too. The third option is to pay based on actual usage via OpenAI API.