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: