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’);
// 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
});
@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
});
}
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});
}