GPT-3 is a language model developed by OpenAI that is capable of responding to text prompts with human-like text. The model can perform natural language processing tasks such as summarizing content, extracting structured information from free text, and converting English into code.
OpenAI provides an API for their models that lets you harness the power of GPT-3 in your applications. In this article, you'll learn how to integrate GPT-3 into a Python application and then use the application to experiment with the model's capabilities.
What Is GPT-3?
GPT-3 is the third iteration of the GPT series. It has been trained on over 175 billion parameters, making it one of the most powerful language models available. The model uses this vast amount of training data to accept input, process it, and appropriately respond to the user. Because of the model's ability to understand the content it receives, it excels at natural language processing tasks that often require an understanding of human-like text (free text) input.
OpenAI provides an API that lets you use GPT-3 in your applications. For example, your app could use the model to generate a summary of input data, such as a news article or a short book. GPT-3 can also extract structured information from free text, which you can then process in your app. For example, your Python app could use GPT-3 to retrieve details from a rental lease agreement, such as the start and end dates, monthly rent payment information, and termination conditions.
To access the OpenAI API, you can use any of OpenAI's libraries, which are available for several popular programming languages. In this article, you'll use the Python library to process data using GPT-3 in your application.
Using GPT-3 with Python
The rest of this tutorial guides you through the process of creating a Python application using the GPT-3 model and testing it with several text-heavy tasks, such as summarizing text, fleshing out outlines, and writing content.
Since you'll be using the OpenAI API, you must sign up for an account. Once you've registered, you should receive some free credits, which you'll use for the demonstration in this article. If you've already signed up and have depleted your free credits or they've expired, you'll have to pay for the tokens used in this demonstration.
Creating the Python Project
Open a new terminal window and execute the following commands to create a new folder for your Python project and navigate into it:
Setting Up a Virtual Environment
To avoid polluting your global Python environment when installing Python packages, you can install additional packages like the OpenAI package in a Python virtual environment. The virtual environment will ensure that the packages you install for this project don't interfere with other packages and package versions used by other Python projects.
To set up a new virtual environment, execute the following command in your terminal in your project directory:
Once the command has created the virtual environment, run the appropriate command below in your project directory to activate it:
Installing the OpenAI Package
You'll use the OpenAI Python package to access the OpenAI API. The package offers methods and classes that simplify accessing different OpenAI models from Python.
Once pip downloads and installs the package, you can integrate your Python application with OpenAI, as shown in the next section.
Retrieving and Setting the OpenAI API Key
Before you can access the OpenAI API from your application, you'll need to retrieve and set the API key your application should use to access OpenAI.
You can retrieve this key by logging in to the OpenAI platform, selecting your account in the top-right menu, and clicking View API keys:
On the page that appears, click the Create new secret key button. A dialog window should appear containing your API key. Copy the key before closing the dialog box, as you cannot view the key again for security reasons.
This environment variable will be read by your Python application and used to access the OpenAI API in the following sections.
Developing the Python Application
You're now ready to begin developing your Python application. To start, simply open the project folder in your preferred code editor.
If you're using Visual Studio Code, you can use the following terminal command to open the project folder:
Loading the Prompt File
GPT-3 can perform text-heavy tasks that involve lengthy and multiline text. To make it easier to input these tasks in Python, you can store the prompts in text files and prompt the user to enter the name of the text file they want to use as input.
Sending the Prompt to OpenAI
Outputting the Response
Testing the Script
Now that you've written a Python script to send prompts to OpenAI's GPT-3 model and print the response, you can test the model with different prompts to see how well it performs various text-heavy processing tasks.
The prompt above contains a few paragraphs from Apple's Wikipedia page. At the end of the file, there are instructions that tell GPT-3 to summarize the article into four sentences.
Run your Python script using the command below:
The script will take a few seconds to send the prompt to OpenAI and receive a response. Once the API returns a reply, you should see it outputted in the terminal window. Below is an example of a response you might receive; you'll see a different output since GPT-3 is a non-deterministic model, meaning the same inputs can produce different results:
Apple is an American technology company that is the largest technology company in terms of revenue and the world's biggest company by market capitalization. It was founded in 1976 by Steve Wozniak, Steve Jobs, and Ronald Wayne. Over the years, the company faced challenges and lost market share to competitors but regained profitability under the leadership of Jobs, who introduced innovative products and strategies. Today, Apple is valued at over $2 trillion but receives criticism for its labor and environmental practices, and business ethics.
Notice how the example response above contains a summary of the Wikipedia article paragraphs in exactly four sentences, as instructed.
Generating a Twitter Thread
Since OpenAI trained GPT-3 on a vast amount of input data from different disciplines, you can use the learned data to create brief content on a particular topic in its training base. The following example explains how to prompt GPT-3 to generate a Twitter thread with five tips on how to pay down your debt faster.
The prompt instructs GPT-3 to write a Twitter thread with five tips on paying down your debt faster. It also provides additional instructions on formatting the Twitter thread and its tweets, including an opening tweet with a captivating statement and a conclusion tweet listing all the tips in order. The prompt also instructs GPT-3 to include suitable hashtags in each tweet.
Rerun your Python script using the command below:
Notice how the response contains a total of seven tweets. The first tweet introduces the thread, the following five tweets list and elaborate on each tip, and the last tweet summarizes the tips.
Extracting Structured Data
The prompt above contains a text representation of the bank statement, including instructions for GPT-3 on how to handle the bank statement at the end of the text. The prompt requests that GPT-3 produce a JSON object that matches the JSON schema specified. By using a JSON schema, you can extract information from different data sources into a consistent JSON object that can be processed by your system. In this case, you extract the bank details, statement dates, currency, opening and closing balance, and the transactions.
Rerun the script:
Notice how the output contains a valid JSON object with all the requested information from the bank statement. You could ingest this JSON object in your application for several use cases, such as importing a bank statement into an accounting system or calculating the total money received and spent by the account holder.
As you've seen, GPT-3 from OpenAI is an impressive language model that provides developers with a powerful tool for performing text-heavy natural language processing tasks. In this article, you learned about some use cases for GPT-3 and then developed a Python application that harnesses the OpenAI API to leverage GPT-3's capabilities across several use cases.
Using GPT-3, your application can understand free text by translating it into structured data. Sensible is a document extraction platform for developers that integrates with GPT-3 and GPT-4. Using Sensible, you can convert PDF documents and images into structured data in seconds using an LLM. The platform provides multiple natural language processing methods depending on the kind of data you want to extract. These methods include the List method, which extracts lists of data from a document where you’re not sure of the data structure; the NLP Table method, which extracts data out of a document that’s inside a table; the Query method, which can find a single fact or data point in your document; and, finally, the Summarizer computed field method and Topic method, which are similar to the List method but provide more configuration options. Using these methods, you can easily extract all kinds of information from your unstructured data.
Another benefit of Sensible is that, unlike the examples in this article, you don't need to create an OpenAI account, manage OpenAI API keys to access GPT-3, or worry about context windows because it's already baked into Sensible. Get your free Sensible API key today and see LLMs can be used for document extraction!