Help

Welcome to the Airtable Community! If you're new here, check out our Getting Started area to get the most out of your community experience.

Summarizing text with OpenAI API

2856 7
cancel
Showing results for 
Search instead for 
Did you mean: 

Hey all!

I made a quick test with OpenAI GPT-3 to summarize text and add the TL;DR to another field. I find it kinda cool, and it was super quick to make, so sharing the example if someone finds it useful.

You’ll need to sign up to OpenAI and generate your own API key there.

Add a scripting app, and here’s the code (remember to edit your own table and field names, and add your own API key):

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

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

// Prompt the user to pick a record 
// If this script is run from a button field, this will use the button's record instead.
let record = await input.recordAsync('Select a record to use', table);

if (record) {

    // Construct the body
    // Change the field name to match yours.
    let body = {
        "prompt": record.getCellValue("Text") + "\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 XXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
        },
    });
    let data = await response.json();

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

    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(record, 
        {
            'TLDR': str
        });
}

Sweet! And there is a ton of other things you can use OpenAI API for. And you could automate this to happen every time a record is created.

Check out a video from my Tweet how it looks in real life:

7 Replies 7

Very nice! I just tested it out this morning.

It wasn’t reading the Tl;dr at first, so added a space so it read " \n\nTl;dr" and that seemed to make it work better. Not sure if the space really did anything or GPT-3 was being weird. The first few times it was just spitting out a few lines from within the text.

The da-vinci engine’s 4000 token limit (~3000 words) makes it a bit less useful for many of the things I read or want summarized though.

I know, the token limit sucks right. But then again. This stuff will develop fast, and soon we have forgotten such limits…

Have you had any problems with the summarizer not finishing the summary? Sometimes it just ends on an incomplete sentence. I know the tl;dr has a 60 token limit for output.

2daeaa8b5f19f0bc209d976c02bd6acb51b00b0a.gif

I have seen the same happening. I’ve meant to take a look at the parameters in the api call, but have not unfortunately had any time this week for hobbies. :triumph:

Hey @Tommi_Holmgren! I've used your summarizing text with OpenAI API and it's been great.

I've been using it with multi-line responses from the API, but I think it's only picking up the first line from the API response. Any change to the regex to fix this?

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

 Here are screenshots of the API response. And what is getting extracted in the str.

kavir_4-1670531527431.png

 

kavir_2-1670531469765.pngkavir_3-1670531496892.png

let str = data.choices[0].text;

Gamechanger! Modified it a bit to write blog posts and coupled it with Zapier to push them to Wordpress. Thanks so much!