forked from replicate/replicate-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrainings.js
More file actions
83 lines (73 loc) · 2.31 KB
/
trainings.js
File metadata and controls
83 lines (73 loc) · 2.31 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
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Create a new training
*
* @param {string} model_owner - Required. The username of the user or organization who owns the model
* @param {string} model_name - Required. The name of the model
* @param {string} version_id - Required. The version ID
* @param {object} options
* @param {string} options.destination - Required. The destination for the trained version in the form "{username}/{model_name}"
* @param {object} options.input - Required. An object with the model inputs
* @param {string} [options.webhook] - An HTTPS URL for receiving a webhook when the training updates
* @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 data for the created training
*/
async function createTraining(model_owner, model_name, version_id, options) {
const { ...data } = options;
if (data.webhook) {
try {
// eslint-disable-next-line no-new
new URL(data.webhook);
} catch (err) {
throw new Error("Invalid webhook URL");
}
}
const response = await this.request(
`/models/${model_owner}/${model_name}/versions/${version_id}/trainings`,
{
method: "POST",
data,
}
);
return response.json();
}
/**
* Fetch a training by ID
*
* @param {string} training_id - Required. The training ID
* @returns {Promise<object>} Resolves with the data for the training
*/
async function getTraining(training_id) {
const response = await this.request(`/trainings/${training_id}`, {
method: "GET",
});
return response.json();
}
/**
* Cancel a training by ID
*
* @param {string} training_id - Required. The training ID
* @returns {Promise<object>} Resolves with the data for the training
*/
async function cancelTraining(training_id) {
const response = await this.request(`/trainings/${training_id}/cancel`, {
method: "POST",
});
return response.json();
}
/**
* List all trainings
*
* @returns {Promise<object>} - Resolves with a page of trainings
*/
async function listTrainings() {
const response = await this.request("/trainings", {
method: "GET",
});
return response.json();
}
module.exports = {
create: createTraining,
get: getTraining,
cancel: cancelTraining,
list: listTrainings,
};