Airtable script from twitter

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