Automatic summarization of text in your Airtable database using OpenAI

@Gabrielle and I were chatting this morning, and she mentioned to me that she was experimenting with OpenAI summarizing long text fields in Airtable.

I am on an Airtable automation kick so the next thing I did was write an automation that will automatically summarize a long text field in a record when that record is updated.

Before you can do this, you will need to sign up for Open AI and obtain an API key.

You’ll also need a table that has at least an Article field and a Summary field, both long text. Or you can name them anything else, as long as you update the script accordingly.

Here’s the automation script. It assumes the previous step in the automation is When a record is updated and outputs a field called Article. The scripting step creates two input parameters, Article (the long text field to summarize) and record (the record ID).

let config = input.config();

// Change this to the name of a table in your base
let table = base.getTable('Summarization');

let openaiUrl = "https://api.openai.com/v1/engines/text-davinci-003/completions";

// Construct the body
let body = {
    "prompt": config.Article + "\n\nTl;dr",
    "temperature": 0.7,
    "max_tokens": 60,
    "top_p": 1.0,
    "frequency_penalty": 0.0,
    "presence_penalty": 0.0
    }

// Make a request to OpenAI API
// Add your own API key in the place of Xses.
let response = await fetch(openaiUrl, {
    method: 'POST',
    body: JSON.stringify(body),
    headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer xxxxxxx', 
    },
});
let data = await response.json();

// Comment this out if you don't want to see any outputs
console.log(data)

// @ts-ignore
let str = /[^A-Za-z0-9](.+)/.exec(data.choices[0].text)[1];

// Write to a cell, change field name to match your table.
table.updateRecordAsync(config.record, {
  'Summary': str // change Summary to the field in your table where you want to store the summary
});

Try it and let me know what you think!

4 Likes

Get the latest model by using:
https://api.openai.com/v1/engines/text-davinci-003/completions

1 Like

Thanks! Script updated!

By the way, under the topic of “how does the When a record is updated trigger work?” I just learned that this script will only run when the field Article is updated, and it appears it doesn’t run when any other field is updated. I don’t quite understand this magic but it is nice because you don’t want Open AI to be called unnecessarily and use up your quota.