Mar 09, 2023 03:14 PM - edited Mar 27, 2023 07:52 AM
Hey all,
I created a script to send one prompt to ChatGPT and fill a cell for every row in an Airtable view. It seems to be sporadically not working some cells, but when I refresh the page and run it again, it usually works. So strange!
The error says "Cannot read properties of undefined (reading '0')". It's odd because it seems the scripting extension is not able to read the value in a cell, once in a while, but Airtable support said the problem is with my code and with the API call specifically (i.e., that the API isn't returning anything).
Can someone help me troubleshoot this so it works on every cell in the view in one run? I added some console.logs for troubleshooting.
// Set up API request parameters
const openaiApiKey = "/";
const inputPrompt1 =
`respond with just the number. rate this response on a scale of 0 to 3 according to this rubric. then after the word "REASONING: " add your justification.`;
const inputPrompt2 =
`respond with just the number. rate this response on a scale of 0 to 3 according to this rubric. then after the word "REASONING: " add your justification.`;
const openaiUrl = "https://api.openai.com/v1/chat/completions";
const table = base.getTable("main");
const view = table.getView("view");
// Set up request headers
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${openaiApiKey}`,
};
// Set the prompt parameters
const max_tokens = 150;
const temp = 0;
// Loop through every record in the current view
const queryResult = await view.selectRecordsAsync({
fields: ["gpt1 Why", "gpt2 Why", "[essay1]", "[essay2]"]
});
for (const record of queryResult.records) {
// Get the current values of the "gpt1" and "gpt2" fields
const gpt1 = record.getCellValueAsString("gpt1 Why");
const gpt2 = record.getCellValueAsString("gpt2 Why");
// Only proceed if the "gpt1" field is blank
if (!gpt1) {
try {
// Set up prompt object with input text
// Get the input text from the "essay1" field
const inputText = record.getCellValueAsString("[essay1]");
const prompt = {
model: "gpt-3.5-turbo",
messages: [{
role: "user",
content: inputPromptTech + inputText
}],
max_tokens: max_tokens,
temperature: temp
};
// Set up request body
const body = JSON.stringify(prompt);
// Send API request using Airtable's built-in fetch function
const response = await fetch(openaiUrl, {
method: "POST",
headers: headers,
body: body,
});
// Parse response data
const responseData = await response.json();
// Get the parsed output from the response
const parsedOutput = responseData.choices[0].message.content;
const splitOutput = parsedOutput.split("REASONING: ");
const scoretext = splitOutput[0].trim();
// Extract number from substring using regex
const regex = /\d+/; // Matches one or more digits
const match = scoretext.match(regex);
const number = match ? parseInt(match[0]) : null;
// Update the "gpt1" field with the parsed output
await table.updateRecordAsync(record, {
"gpt1": number,
});
await table.updateRecordAsync(record, {
"gpt1 Why": splitOutput[1].trim(),
});
} catch (error) {
console.log(`Error occurred for record ${record.id}: ${error.message}`);
}
}
if (!gpt2) {
try {
// Set up prompt object with input text
// Get the input text from the "essay1" field
const inputText = record.getCellValueAsString("[essay2]");
const prompt = {
model: "gpt-3.5-turbo",
messages: [{
role: "user",
content: inputPromptFellowship + inputText
}],
max_tokens: max_tokens,
temperature: temp
};
// Set up request body
const body = JSON.stringify(prompt);
// Send API request using Airtable's built-in fetch function
const response = await fetch(openaiUrl, {
method: "POST",
headers: headers,
body: body,
});
// Parse response data
const responseData = await response.json();
// Get the parsed output from the response
const parsedOutput = responseData.choices[0].message.content;
const splitOutput = parsedOutput.split("REASONING: ");
const scoretext = splitOutput[0].trim();
// Extract number from substring using regex
const regex = /\d+/; // Matches one or more digits
const match = scoretext.match(regex);
const number = match ? parseInt(match[0]) : null;
console.log(inputText);
console.log(responseData);
// Update the "gpt2" field with the parsed output
await table.updateRecordAsync(record, {
"gpt2": number,
});
await table.updateRecordAsync(record, {
"gpt2 Why": splitOutput[1].trim(),
});
} catch (error) {
console.log(`Error occurred for record ${record.id}: ${error.message}`);
}
}
}