-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathretrieve_results.js
37 lines (30 loc) · 1.11 KB
/
retrieve_results.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
import OpenAI from "openai";
const openai = new OpenAI();
// Set the batch job ID here or pass it as an argument
const BATCH_JOB_ID = "";
const batchId = process.argv[2] ?? BATCH_JOB_ID;
let batch = await openai.batches.retrieve(batchId);
console.log("Batch: ", batch);
// Check the status of the batch job every 30 seconds until it is completed with a timeout of 20 minutes
const TIMEOUT = 1200000;
const startTime = Date.now();
while (
batch.status !== "completed" &&
batch.status !== "failed" &&
Date.now() - startTime < TIMEOUT
) {
batch = await openai.batches.retrieve(batch.id);
console.log("Batch status: ", batch.status);
await new Promise((resolve) => setTimeout(resolve, 30000));
}
if (batch.status === "completed") {
console.log("Batch completed");
const batchResults = await openai.batches.results(batch.id);
console.log("Batch results: ", batchResults);
// Retrieve the results
const resultsFile = await openai.files.content(batchResults.output_file_id);
const results = await resultsFile.text();
console.log("Results: ", results);
} else {
console.log("Batch job failed or timed out");
}