let tableName = "Table 1";
let viewName = "Books (ChatGPT)";
let apiKey = "sk-key";
let table = base.getTable(tableName);
let view = table.getView(viewName);
let record = await input.recordAsync('Select a book to summarize', table);
if (record) {
let recordId = record.id;
let bookTitle = record.getCellValue("Name");
let bookAuthor = record.getCellValue("Autor");
let prompt = "Provide detailed yet concise bullet-point summary of " + bookTitle + " " + bookAuthor + "in exactly this format: Long-Form Summary: Themes: Lessons from Reading the Book: Category Tags: Key Quotes:"
// let prompt = "Предоставьте подробное, но лаконичное описание в виде маркированного списка книги " + bookTitle + " " + bookAuthor + " в точно таком же формате: Сводка длинного формата: Уроки из чтения книги: Категории тегов: Ключевые цитаты:"
async function sendRequest(prompt) {
try {
let body = {
"prompt": prompt,
"temperature": 0.7,
"max_tokens": 600,
"model": "text-davinci-003",
"top_p": 1.0,
"frequency_penalty": 0.0,
"presence_penalty": 0.0
}
let response = await fetch(openaiUrl, {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + apiKey,
}
});
let data = await response.json();
return data;
} catch (err) {
console.log("sendRequest err: ", err);
}
}
function convertResponseToObject(data) {
if (data && data.choices) {
let responseText = data.choices[0].text;
console.log("responseText: ", responseText);
//let bookSummary = responseText.split("Long-Form Summary:") ? responseText.split("Themes:")[0].replace("Long-Form Summary:", "").trim() : null;
//let bookLessons = responseText.split("Lessons from Reading the Book:") ? responseText.split("Lessons from Reading the Book:")[1].split("Category Tags:")[0].trim() : null;
//let bookTags = responseText.split("Category Tags:") ? responseText.split("Category Tags:")[1].split("Key Quotes:")[0].trim() : null;
//let bookQuotes = responseText.split("Key Quotes:") ? responseText.split("Key Quotes:")[1].trim() : null;
let bookSummary = responseText;
let bookLessons = responseText;
let bookTags = responseText;
let bookQuotes = responseText;
let responseObj = {};
responseObj.bookSummary = bookSummary;
responseObj.bookLessons = bookLessons;
responseObj.bookTags = bookTags;
responseObj.bookQuotes = bookQuotes;
return responseObj;
} else {
return null
}
}
async function updateRecord(recordId, responseObj) {
let updateResponse = await table.updateRecordAsync(recordId, {
"bookSummary": responseObj.bookSummary,
"bookLessons": responseObj.bookLessons,
"bookTags": responseObj.bookTags,
"bookQuotes": responseObj.bookQuotes,
});
console.log("updateResponse: ", updateResponse);
}
let data = await sendRequest(prompt);
let responseObj = convertResponseToObject(data);
await updateRecord(recordId, responseObj)
} else {
console.log('No record was selected');
}