-
Notifications
You must be signed in to change notification settings - Fork 0
/
BatchGPT.js
118 lines (105 loc) · 4.12 KB
/
BatchGPT.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Initializes the batch processing and sets up a trigger to continue processing.
* @param {number} startRowGPT The starting row number for processing.
* @param {number} endRowGPT The ending row number for processing.
* @param {string} inColGPT The column letter where the prompts are.
* @param {string} outColGPT The column letter where the results should be written.
*/
function startGPTProcessing(
startRowGPT = 2,
endRowGPT = 100,
inColGPT = 'AO',
outColGPT = 'AP'
) {
deleteExistingTriggers('continueGPTProcessing');
const batchSize = 10; // Adjust this number based on the typical processing time per batch to stay under the 6-minute script runtime limit
let currentendRowGPT = startRowGPT + batchSize - 1;
if (currentendRowGPT > endRowGPT) currentendRowGPT = endRowGPT;
// Store settings in script properties
const properties = PropertiesService.getScriptProperties();
properties.setProperties({
inColGPT: inColGPT,
outColGPT: outColGPT,
endRowGPT: endRowGPT.toString(),
lastRowProcessedGPT: currentendRowGPT.toString(),
});
// Start the first batch
processGPTBatches(startRowGPT, currentendRowGPT, inColGPT, outColGPT);
if (currentendRowGPT < endRowGPT) {
ScriptApp.newTrigger('continueGPTProcessing')
.timeBased()
.after(10000) // Wait 10 seconds before continuing
.create();
}
}
/**
* Processes batches of prompts and writes the responses to a specified output column.
* @param {number} startRowGPT The starting row number for processing.
* @param {number} endRowGPT The ending row number for processing.
* @param {string} inColGPT The column letter where the prompts are.
* @param {string} outColGPT The column letter where the results should be written.
*/
function processGPTBatches(startRowGPT, endRowGPT, inColGPT, outColGPT) {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const promptsRange = `${inColGPT}${startRowGPT}:${inColGPT}${endRowGPT}`;
const prompts = sheet.getRange(promptsRange).getValues();
let results = [];
prompts.forEach((row, index) => {
if (row[0] !== '') {
try {
let response = GPT(row[0]); // Simulate an API call
results.push([response]);
} catch (e) {
console.error('Error processing prompt: ' + row[0], e);
results.push(['Error: ' + e.toString()]);
}
} else {
results.push(['']);
}
});
const outputRange = `${outColGPT}${startRowGPT}:${outColGPT}${
startRowGPT + results.length - 1
}`;
sheet.getRange(outputRange).setValues(results);
}
/**
* Continues processing the next batch after a delay.
*/
function continueGPTProcessing() {
const properties = PropertiesService.getScriptProperties();
const inColGPT = properties.getProperty('inColGPT');
const outColGPT = properties.getProperty('outColGPT');
const endRowGPT = parseInt(properties.getProperty('endRowGPT'));
let lastRow = parseInt(properties.getProperty('lastRowProcessedGPT'));
const batchSize = 10;
const startRowGPT = lastRow + 1;
let nextendRowGPT = startRowGPT + batchSize - 1;
if (nextendRowGPT > endRowGPT) nextendRowGPT = endRowGPT;
// Process the next batch
processGPTBatches(startRowGPT, nextendRowGPT, inColGPT, outColGPT);
// Update the last processed row
properties.setProperty('lastRowProcessedGPT', nextendRowGPT.toString());
if (nextendRowGPT < endRowGPT) {
deleteExistingTriggers('continueGPTProcessing');
ScriptApp.newTrigger('continueGPTProcessing')
.timeBased()
.after(10000) // Wait 10 seconds before continuing again
.create();
} else {
// All batches are processed, delete the trigger
deleteExistingTriggers('continueGPTProcessing');
console.log('All rows have been processed.');
}
}
/**
* Deletes all triggers associated with a given function name.
* @param {string} functionName The name of the function whose triggers need to be deleted.
*/
function deleteExistingTriggers(functionName) {
var allTriggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < allTriggers.length; i++) {
if (allTriggers[i].getHandlerFunction() === functionName) {
ScriptApp.deleteTrigger(allTriggers[i]);
}
}
}