Help

Script

Topic Labels: Automations Data
704 1
cancel
Showing results for 
Search instead for 
Did you mean: 
zhyk01
4 - Data Explorer
4 - Data Explorer

What's wrong with this script?

 

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

 

1 Reply 1

That's quite a script to troubleshoot without any additional information.
What's the context? What's the workflow? Does it work at all? If so, what is the expected behavior compared to what it's actually doing?
Does it throw an exception? If so, what is it?
What are you sending to the API? What are you expecting back?
What troubleshooting steps have you taken so far (if any)? What were the results of your troubleshooting?

The more information, the better.
It can sometimes prove impossible to troubleshoot other people's scripts because we don't have much (if any) information about their tables, fields, field types, relationships, etc.
On top of that, it can prove incredibly difficult when people don't give us any context into the workflows that the script might be affected by or be integrated into. The context of any relevant workflows can completely change the design pattern of a script that might be best suited for a certain implementation.