Airtable script from twitter

Hi,
I found this Airtable script on twitter. I tried to make it “Seatable- compatible” with the help of chatgpt (I am not a coder) without success. Can someone help?

// 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 XXXXX',
    },
});
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
    });

}

Hi @Ralf.

You very first step should be to format the code with the forum built-in formatting. Otherwise we don’t have a change to properly read it.

Can you tell me the purpose of this script? As far as I read the code, I assume the following:

  1. enter your question into a text column with the name “Text”.
  2. Press a button to execute this python script
  3. the python script sends the question to OpenAI API
  4. you write back your response to another text-column.

Can you confirm that my assumptions are right?
Is that what you want to achieve?

@cdb Correct. into the text column with name “Text” you copy a long form text. After pressing the button OpenAI writes a short summary into another text-column.
@AkDk7 sorry for that.

// 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 XXXXX',
        },
    });
    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
        });
}

Hi @Ralf.

Thanks for updating.

So basically, this should do the trick. But you have to add the bearer token you got from ChatGPT and put it into the variable bearer in the script. I do not have a paid account, so I cannot test the output to write a fully functioning script. Additionally, the script (JavaScript and not Python) acts a bit different since you do not get a dialog to select a text. You just have to click on the button in the specific row.

Here is the script. If you create the columns with the exact names, it should work.

const table = base.context.currentTable;
const row = base.context.currentRow;

const bearer = "";
const question = row['Question'];
const openaiUrl = "https://api.openai.com/v1/engines/text-davinci-002/completions";

if (question) {
    // Construct the body
    // Change the field name to match yours.
    let body = {
      "prompt": question + "\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 ' + bearer,
      },
    });
    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.
    base.modifyRow(table, row, {'Answer': str});
}
2 Likes

@AkDk7

Thank you so much!
It works like a charm!

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.