forked from replicate/replicate-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredictions.js
More file actions
70 lines (63 loc) · 2.08 KB
/
predictions.js
File metadata and controls
70 lines (63 loc) · 2.08 KB
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
/**
* Create a new prediction
*
* @param {object} options
* @param {string} options.version - Required. The model version
* @param {object} options.input - Required. An object with the model inputs
* @param {object} [options.wait] - Whether to wait for the prediction to finish. Defaults to false
* @param {number} [options.wait.interval] - Polling interval in milliseconds. Defaults to 250
* @param {number} [options.wait.maxAttempts] - Maximum number of polling attempts. Defaults to no limit
* @param {string} [options.webhook] - An HTTPS URL for receiving a webhook when the prediction has new output
* @param {string[]} [options.webhook_events_filter] - You can change which events trigger webhook requests by specifying webhook events (`start`|`output`|`logs`|`completed`)
* @returns {Promise<object>} Resolves with the created prediction data
*/
async function createPrediction(options) {
const { wait, ...data } = options;
const prediction = this.request('/predictions', {
method: 'POST',
data,
});
if (wait) {
const { maxAttempts, interval } = wait;
return this.wait(await prediction, { maxAttempts, interval });
}
return prediction;
}
/**
* Fetch a prediction by ID
*
* @param {number} prediction_id - Required. The prediction ID
* @returns {Promise<object>} Resolves with the prediction data
*/
async function getPrediction(prediction_id) {
return this.request(`/predictions/${prediction_id}`, {
method: 'GET',
});
}
/**
* Cancel a prediction by ID
*
* @param {string} prediction_id - Required. The training ID
* @returns {Promise<object>} Resolves with the data for the training
*/
async function cancelPrediction(prediction_id) {
return this.request(`/predictions/${prediction_id}/cancel`, {
method: 'POST',
});
}
/**
* List all predictions
*
* @returns {Promise<object>} - Resolves with a page of predictions
*/
async function listPredictions() {
return this.request('/predictions', {
method: 'GET',
});
}
module.exports = {
create: createPrediction,
get: getPrediction,
cancel: cancelPrediction,
list: listPredictions,
};