March 28, 2021 No Comments

Since OpenAI launched GPT-3, we have been seeing numerous applications with various functionalities developed using GPT3.

Recently GPT-3 added new feature of Question Answering system which we took for a spin to check how it works.

In our experimentation with small data, the system looks pretty promising. It is fetching answers quite accurately from small data. In future, we’ll be trying on large data to check how it works.

For those, who have got GPT-3 access and would like to try the new Question Answering system by GPT-3, we have put together this small tutorial. 

How to use GPT-3 Question Answering

QnA is not available in the playground so we have to use it via API. For that, we will be using Python.

It is assumed that Python, pip and virtualenv are already installed in your system. If not, then kindly do that first and follow steps below.

Create & Activate virtualenv by following steps

				
					virtualenv env_gpt --python=python3
source env_gpt/bin/activate
				
			

Install OpenAI Python bindings

				
					pip install openai
				
			

To perform Question Answering using GPT3 endpoint, we need to provide the question, set of documents(paragraphs), and some sample examples to the Open AI API, and it will generate an answer.

We can provide the document as a document list or a file.

1. Let’s explore how we can perform question answering using a document list :

Create a python file and copy the code below, you need to provide your OpenAI API key value to openai.api_key, and then run the file.

				
					import openai
openai.api_key = "YOUR-API-KEY"
 
document_list = ["Google was founded in 1998 by Larry Page and Sergey Brin while they were Ph.D. students at Stanford University in California. Together they own about 14 percent of its shares and control 56 percent of the stockholder voting power through supervoting stock. They incorporated Google as a privately held company on September 4, 1998. An initial public offering (IPO) took place on August 19, 2004, and Google moved to its headquarters in Mountain View, California, nicknamed the Googleplex. In August 2015, Google announced plans to reorganize its various interests as a conglomerate called Alphabet Inc. Google is Alphabet's leading subsidiary and will continue to be the umbrella company for Alphabet's Internet interests. Sundar Pichai was appointed CEO of Google, replacing Larry Page who became the CEO of Alphabet.",
"Amazon is an American multinational technology company based in Seattle, Washington, which focuses on e-commerce, cloud computing, digital streaming, and artificial intelligence. It is one of the Big Five companies in the U.S. information technology industry, along with Google, Apple, Microsoft, and Facebook. The company has been referred to as 'one of the most influential economic and cultural forces in the world', as well as the world's most valuable brand. Jeff Bezos founded Amazon from his garage in Bellevue, Washington on July 5, 1994. It started as an online marketplace for books but expanded to sell electronics, software, video games, apparel, furniture, food, toys, and jewelry. In 2015, Amazon surpassed Walmart as the most valuable retailer in the United States by market capitalization."]
 
response = openai.Answer.create(
 search_model="ada",
 model="curie",
 question="when was google founded?",
 documents=document_list,
 examples_context="In 2017, U.S. life expectancy was 78.6 years.",
 examples=[["What is human life expectancy in the United States?","78 years."]],
 max_tokens=10,
 stop=["\n", "<|endoftext|>"],
)
 
print(response)
				
			

In document_list you need to provide your paragraphs.

openai.Answer.create() function parameters:
question: provide your input question here
document: the list of the document
examples_context: A text example containing the paragraph or context which is used to generate the answers for the examples you provide.
examples: provide sample pair of question & answer
stop: API will stop generating further tokens
max_tokens: the maximum number of tokens allowed in the final response

After executing the file you will see the response as below:

2. Let’s see how we can perform Question Answering using data stored in File :

You can create a .jsonl(JSON Lines) files where each JSON line contains the “text” field and “metadata” field(optional). The following is a simple .jsonl file format

				
					{"text": "Hello OpenAI", "metadata": "sample data"}
				
			

Let’s start by creating a .jsonl file for QNA, create a sample_qna.jsonl and copy the following code in it:

				
					{"text": Google was founded in 1998 by Larry Page and Sergey Brin while they were Ph.D. students at Stanford University in California. Together they own about 14 percent of its shares and control 56 percent of the stockholder voting power through supervoting stock. They incorporated Google as a privately held company on September 4, 1998. An initial public offering (IPO) took place on August 19, 2004, and Google moved to its headquarters in Mountain View, California, nicknamed the Googleplex. In August 2015, Google announced plans to reorganize its various interests as a conglomerate called Alphabet Inc. Google is Alphabet's leading subsidiary and will continue to be the umbrella company for Alphabet's Internet interests. Sundar Pichai was appointed CEO of Google, replacing Larry Page who became the CEO of Alphabet.", "metadata": "Google"}
{"text": "Amazon is an American multinational technology company based in Seattle, Washington, which focuses on e-commerce, cloud computing, digital streaming, and artificial intelligence. It is one of the Big Five companies in the U.S. information technology industry, along with Google, Apple, Microsoft, and Facebook. The company has been referred to as 'one of the most influential economic and cultural forces in the world', as well as the world's most valuable brand.Jeff Bezos founded Amazon from his garage in Bellevue, Washington on July 5, 1994. It started as an online marketplace for books but expanded to sell electronics, software, video games, apparel, furniture, food, toys, and jewelry. In 2015, Amazon surpassed Walmart as the most valuable retailer in the United States by market capitalization.", "metadata": "Amazon"}
				
			

Once you have created the jsonl file, now you need to upload that file. To upload the file create a python file name ‘upload_file.py’, copy the below code and add your API Key :

				
					import openai
openai.api_key = "YOUR-API-KEY"
 
response = openai.File.create(
 file=open("sample_qna.jsonl"),
 purpose='answers'
)
 
print(response)
				
			

Now run the upload_file.py file, you will get the below like response :

				
					{
  “bytes”: 1691,
  “created_at”: 1616762542,
  “filename”: “sample_doc.jsonl”,
  “id”: “file-h5zzNVGdUigntPQWeVmJqAJf”,
  “object”: “file”,
  “purpose”: “answers”
}
				
			

From the above response, we will need the file ID

Below is the code to perform question answering using file

				
					import openai
openai.api_key = "YOUR-API-KEY"
 
response = openai.Answer.create(
   search_model="ada",
   model="curie",
   question="when was google founded?",
   file="file-h5zzNVGdUigntPQWeVmJqAJf",
   examples_context="In 2017, U.S. life expectancy was 78.6 years.",
   examples=[["What is human life expectancy in the United States?","78 years."]],
   max_rerank=10,
   max_tokens=10,
   stop=["\n", "<|endoftext|>"]
 
)
 
print(response)
				
			

The parameters of open.Answer.create() are almost the same as we have seen above, instead of document we need to provide the file parameter

file: specify the file ID that we got after uploading the file

NOTE : you can specify either file or document not both

You will get the below response:

As we can see, it’s not difficult to setup the QnA system using GPT-3. And results on our test of small text data also seems good.

Though, we found that it takes some time to generate results and if the text length will increase then it may take even longer.

We’ll publish our further experimentations and learning in upcoming days.

We have created Text paraphrasingIntent classificationads generation and many other apps using GPT-3. For GPT-3 related work, do get in touch with us at letstalk@pragnakalp.com

Write a comment

Your email address will not be published. Required fields are marked *

Want to talk to an Expert Developer?

Our experts in Generative AI, Python Programming, and Chatbot Development can help you build innovative solutions and scale your business faster.