Gen AI Classification

Using ChatGPT in Google Sheets for Classification (custom function included)

Classification is the generative AI-based pattern I use most frequently. Why? Three reasons:

  1. It’s the easiest to deploy.
  2. The leverage is amazing.
  3. Use cases are everywhere.

It’s perfect for tackling big tasks where 80% accuracy on 100% of cases is better than 100% accuracy on just 20%. This week alone, I’ve used classification to:

  • Identify target job titles and ideal customer profile (ICP) companies for go-to-market (GTM) strategies.
  • Classify negative keywords to reduce ad costs.
  • Measure the search volume of commercial organic search queries.

Since Google Sheets is one of my go-to tools for small/quick data problems, I wanted an easy and extensible method for doing classification that was “built-in.” App Script makes it dead simple to create custom Google Sheets functions, so I made a CLASSIFY function to do the trick.

Why Classification?

The prompts are deceptively simple. They boil down to this:
“Does X meet {{some criteria}}?”

By adding 2–3 examples, you can tune the model’s accuracy significantly. Even better, if you have historical, human-validated classifications, you can provide them dynamically to make predictions even more precise.

The beauty of this approach is its adaptability. You can apply it to any process where human interpretation would normally be the bottleneck, saving both time and effort.

AI Classification Custom Function in Google Sheets

The easiest way to implement this is in Google Sheets. I’ve built a custom function using the ChatGPT API that you can use to classify anything you want. There are two parameters you can change:

  1. Update the API_KEY variable value with your own API key. You can find your API key here.
  2. Update the SYSTEM_PROMP variable value to be more specific to your task.

Here’s the full script :

// Replace "YOUR_OPENAI_API_KEY" with your actual OpenAI API key
const API_KEY = "YOUR_OPENAI_API_KEY";
// System prompt to guide the model. Modify this to be more specific to your task.
const SYSTEM_PROMPT = "You are a classification model. Based on the provided prompt, classify the input as 'true' or 'false'.";


function CLASSIFY(inputValue, classificationPrompt) {

  // Check if the API key has been updated
  if (API_KEY === "YOUR_OPENAI_API_KEY") {
    throw new Error("API key is missing. Please replace 'YOUR_OPENAI_API_KEY' with your actual OpenAI API key.");
  }

  // OpenAI API URL
  const url = "https://api.openai.com/v1/chat/completions";

  // Construct the message array for the API request
  const messages = [
    { role: "system", content: SYSTEM_PROMPT },
    { role: "user", content: `Classification prompt: ${classificationPrompt}` },
    { role: "user", content: `Input: ${inputValue}` }
  ];

  // API request options
  const options = {
    method: "post",
    headers: {
      "Authorization": `Bearer ${API_KEY}`,
      "Content-Type": "application/json"
    },
    payload: JSON.stringify({
      model: "gpt-4",
      messages: messages,
      temperature: 0.0
    })
  };

  try {
    const response = UrlFetchApp.fetch(url, options);
    const json = JSON.parse(response.getContentText());

    // Extract and validate the response
    const answer = json.choices[0].message.content.trim().toLowerCase();

    if (answer === "true") return true;
    if (answer === "false") return false;

    // Handle unexpected responses
    console.error("Unexpected API response:", answer);
    throw new Error("Unexpected response from the model.");
  } catch (error) {
    console.error("Error calling OpenAI API:", error.message);
    throw new Error("Failed to classify input. Please try again.");
  }
}

Example Use Cases for AI Classification

This script can supercharge workflows across a variety of domains. Here are some ideas to get you started:

  1. Marketing and Sales:
    • Classify job titles to determine if they match your ideal customer profile (ICP).
    • Identify keywords for negative match lists in Google Ads to cut ad costs.
  2. Content Strategy:
    • Determine whether search queries are commercial, informational, or navigational.
    • Sort blog topics by relevance to your audience.
  3. Data Analytics:
    • Categorize responses in survey data (e.g., sentiment analysis).
    • Classify customer feedback into actionable categories.
  4. Customer Engagement:
    • Assess whether inbound messages are urgent, important, spam, etc.
    • Sort applications into qualified, unqualified, and needs-review categories.


Go try it out!

This script integrates directly into Google Sheets, making it easy for anyone familiar with spreadsheets. Using the ChatGPT API offers the flexibility to classify inputs based on any criteria you define, with little setup and a huge potential for impact.

If you’re ready to try it out, copy the script into your Google Apps Script editor, replace the placeholder API key with your own, and start classifying!

Have questions? Let me know in the comments!

Leave a Comment

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