Using PaLM 2 & MakerSuite to Categorize Halloween Costumes

G. Hussain Chinoy
4 min readOct 31, 2023

--

PaLM 2 Imagen “spooky halloween jack-o-lantern carved with an outline of a black cat, with children faded out in the background, at night, cartoon”

Every year I keep a Sheet of the kids that visit for Halloween, since they’re very proud to tell us who they’re dressed as. The problem is we’re Olds and can’t always tell what a kid’s dressed as or, sometimes, for the littles, what they actually say (cute). I still write it down (along with the time) and then I try to categorize the costume.

This year, I’m prepared — I’m using PaLM 2 and MakerSuite in a Sheets Custom Function with Apps Script to do the categorization for me!

In my Sheet, I created a new custom function=categorize_costume(cell) to get the category and subcategory via a PaLM 2 prompt that categorizes the genre and subgenre of the costume. Here’s the result tested on part of my 2022 list (columns Category and Subcategory are LLM generated!):

2022 Halloween Costumes, with columns Category and Subcategory classified by PaLM 2

Here’s how you can do this, too!

Go to MakerSuite and get an API key. Once you’ve gotten that, write a text prompt like the following. I used a fairly verbose prompt, combining a few best practices:

  • providing a persona
  • providing some instructions
  • providing a few examples
You’re an expert at identifying halloween costumes, genre and subgenre.
Categorize the following halloween costume by a two word broad category of costume, such as: cartoon character, tv show character, movie character, undead, video game, comic book;
followed with a one or two word subcategory such as: vampire, zombie, pixar, star wars.
Output the genre and subgenre separated by a comma.

Examples
Input: elmo
Output: tv character, sesame street
Input: dora
Output: cartoon character, dora the explorer
Input: sith
Output: movie character, star wars

Input: {{}}
Output:

If you don’t want to type all this out, here’s the link to my shared MakerSuite Halloween Costume Categorization prompt. Is it the best prompt in the world? Not by far. Does it get the job done? Definitely, and hilariously sometimes.

Next, I opened up a Sheet via sheets.new and then Extensions > Apps Script

Get to App Script by selecting the Extensions menu, then Apps Script

MakerSuite is quite nice and provides a curl example of the HTTP call to the PaLM 2 API with your prompt, so I used that and translated it to the JavaScript that Apps Script uses.

/**
* Categorize Halloween Costumes with PaLM 2 / MakerSuite
* Author ghchinoy@
*
* Usage
* =categorize_costume(cell)
*
* Provide a cell value and retrieve a comma separated category and subcategory.
*
* Use =split(categorize_costume(cell), “,”) to split between two cells.
*
*/

// Hi! Add your API key here!
const PALM_API_KEY = “ADD-YOUR-KEY-HERE”;
const MODEL_NAME = “models/text-bison-001”;

function categorize_costume(cell_value) {

const PALM_URL = `https://generativelanguage.googleapis.com/v1beta3/${MODEL_NAME}:generateText?key=${PALM_API_KEY}`

// NOTE: medium readers, I've removed the full prompt for brevity
const promptString = `THE PROMPT GOES HERE`;
const stopSequences = [];
let raw = JSON.stringify({“prompt”: {“text”: promptString}, “temperature”: 0.4, “top_k”: 40, “top_p”: 0.95, “candidate_count”: 1, “max_output_tokens”: 1024})

// send request
let requestOptions = {
‘method’: ‘post’,
‘muteHttpExceptions’: true,
‘contentType’: ‘application/json’,
‘payload’: raw,
redirect: ‘follow’
};
let response = UrlFetchApp.fetch(PALM_URL, requestOptions)

// parse & return
let jsonObj = JSON.parse(response.getContentText());
let categories = jsonObj.candidates[0].output;

return categories;
}

A few clever bits:

  • Apps Script uses a function and here I’ve defined categorize_costume as the function that will be used in Sheets as =categorize_costume(cell_value)
  • I’ve removed the prompt in the above code snippet, but you can see the full code at the Sheet I’ve shared here
  • The response is in a JSON format, which you can see in the MakerSuite docs here, so I pulled out the first response: jsonObj.candidates[0].output
  • Since this is Sheets, I can use a built-in function to split the LLM output — using known capabilities with LLM capabilities is a superpower

In Sheets, I’ve used the =split(text,separator) function to split the comma-separated value, so an example output I’d receive would be:

cartoon character, star wars

.. but what I want is that value split in two columns.

So if ahsoka was in B2, I’d do =split(categorize_costume(B2), ",") and I’d then receive:

Here’s my Sheet, which you can copy. Definitely open up the Apps Script (Extensions > Apps Script) and put in your API key!

Happy Halloween!

--

--