How to classify open ended text using AI & Python

Love Spreadsheets
3 min readDec 6, 2022

--

OpenAI’s GPT-3 (Generative Pretrained Transformer 3) is a cutting-edge language processing model that can be used for a wide range of natural language processing tasks, including text classification.

In this blog post, we’ll take a look at how to use GPT-3 to classify open-ended texts into categories, and we’ll include some Python code examples to show you how it’s done.

Shameless plug: if you would like expert help on integrating AI & GPT-3 into your workflows, contact us at www.lovespreadsheets.com

Our itty bitty AI doing text classifications

To get started, you’ll need to have GPT-3 installed and set up on your machine.

You can do this by using pip on your command line (terminal)

$ pip install openai

You will also need an OpenAI account to get your personalized key.

To get the key:

  1. Sign up for an OpenAI account here
  2. Click on View API Keys under your Profile section
Click on View API Keys

3. Copy your key and keep it safe. If you do not have a key, then click on Create New Secret Key and copy it. We will need it in the next section

Copy your Secret Key from this section

Once you have GPT-3 installed and your key, you’ll need to load the model and set up the necessary Python libraries and dependencies.

Here’s a code example that shows you how to do this:

import openai

# Set up the openai API key
openai.api_key = "your_openai_api_key"
# Load the GPT-3 model
model = openai.Model("text-davinci-002")

Next, we need to define the text classification task that we want GPT-3 to perform.

In this example, we’ll define two categories: “positive” and “negative”.

These categories can be whatever you want, depending on the specific classification task that you’re trying to perform.

Here’s how you can define these categories and provide some example texts for each category:

# Define the categories and example texts
categories = ["positive", "negative"]
examples = {
"positive": ["I had a great time at the party last night.", "I love this song.", "This is the best day ever."],
"negative": ["I hate this weather.", "I'm not feeling well.", "This is the worst day of my life."]
}

Now that we have our categories and examples defined, we can use GPT-3 to train a text classifier.

To do this, we’ll need to provide GPT-3 with the examples that we defined earlier, along with a prompt that specifies the classification task that we want GPT-3 to perform.

Here’s an example of how you can do this:

# Train the classifier
prompt = "Classify the following text as either positive or negative: "
model.classify(
prompt=prompt,
examples=examples
)

After training the classifier, we can use it to classify new texts that it hasn’t seen before. Here’s an example of how you can do this:

# Classify a new text
text = "I'm feeling really happy today."
result = model.classify(text)
# Print the result
print("The text is classified as:", result["choices"][0]["text"])

In this example, GPT-3 will classify the text as “positive”, since it contains words and phrases that are typically associated with positive sentiment.

GPT-3 is a powerful tool for text classification, and it can be used to perform a wide range of natural language processing tasks.

With the Python code examples in this blog post, you should be able to get started using GPT-3 for text classification, and you can experiment with different classification tasks to see what it’s capable of.

If you don’t know Python or would like to integrate AI into another data workflow, feel free to contact us and our experts will do this for you at www.lovespreadsheets.com

--

--

Love Spreadsheets
Love Spreadsheets

Written by Love Spreadsheets

An AI powered Data & Analytics company. On a mission to make everyone love spreadsheets!

Responses (1)