-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathdeployments.js
166 lines (148 loc) · 5.17 KB
/
deployments.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const { transformFileInputs } = require("./util");
/**
* Create a new prediction with a deployment
*
* @param {string} deployment_owner - Required. The username of the user or organization who owns the deployment
* @param {string} deployment_name - Required. The name of the deployment
* @param {object} options
* @param {object} options.input - Required. An object with the model inputs
* @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`)
* @param {boolean} [options.block] - Whether to wait until the prediction is completed before returning. Defaults to false
* @returns {Promise<object>} Resolves with the created prediction data
*/
async function createPrediction(deployment_owner, deployment_name, options) {
const { input, block, ...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 headers = {};
if (block) {
headers["Prefer"] = "wait";
}
const response = await this.request(
`/deployments/${deployment_owner}/${deployment_name}/predictions`,
{
method: "POST",
headers,
data: {
...data,
input: await transformFileInputs(
this,
input,
this.fileEncodingStrategy
),
},
}
);
return response.json();
}
/**
* Get a deployment
*
* @param {string} deployment_owner - Required. The username of the user or organization who owns the deployment
* @param {string} deployment_name - Required. The name of the deployment
* @returns {Promise<object>} Resolves with the deployment data
*/
async function getDeployment(deployment_owner, deployment_name) {
const response = await this.request(
`/deployments/${deployment_owner}/${deployment_name}`,
{
method: "GET",
}
);
return response.json();
}
/**
* @typedef {Object} DeploymentCreateRequest - Request body for `deployments.create`
* @property {string} name - the name of the deployment
* @property {string} model - the full name of the model that you want to deploy e.g. stability-ai/sdxl
* @property {string} version - the 64-character string ID of the model version that you want to deploy
* @property {string} hardware - the SKU for the hardware used to run the model, via `replicate.hardware.list()`
* @property {number} min_instances - the minimum number of instances for scaling
* @property {number} max_instances - the maximum number of instances for scaling
*/
/**
* Create a deployment
*
* @param {DeploymentCreateRequest} config - Required. The deployment config.
* @returns {Promise<object>} Resolves with the deployment data
*/
async function createDeployment(deployment_config) {
const response = await this.request("/deployments", {
method: "POST",
data: deployment_config,
});
return response.json();
}
/**
* @typedef {Object} DeploymentUpdateRequest - Request body for `deployments.update`
* @property {string} version - the 64-character string ID of the model version that you want to deploy
* @property {string} hardware - the SKU for the hardware used to run the model, via `replicate.hardware.list()`
* @property {number} min_instances - the minimum number of instances for scaling
* @property {number} max_instances - the maximum number of instances for scaling
*/
/**
* Update an existing deployment
*
* @param {string} deployment_owner - Required. The username of the user or organization who owns the deployment
* @param {string} deployment_name - Required. The name of the deployment
* @param {DeploymentUpdateRequest} deployment_config - Required. The deployment changes.
* @returns {Promise<object>} Resolves with the deployment data
*/
async function updateDeployment(
deployment_owner,
deployment_name,
deployment_config
) {
const response = await this.request(
`/deployments/${deployment_owner}/${deployment_name}`,
{
method: "PATCH",
data: deployment_config,
}
);
return response.json();
}
/**
* Delete a deployment
*
* @param {string} deployment_owner - Required. The username of the user or organization who owns the deployment
* @param {string} deployment_name - Required. The name of the deployment
* @returns {Promise<boolean>} Resolves with true if the deployment was deleted
*/
async function deleteDeployment(deployment_owner, deployment_name) {
const response = await this.request(
`/deployments/${deployment_owner}/${deployment_name}`,
{
method: "DELETE",
}
);
return response.status === 204;
}
/**
* List all deployments
*
* @returns {Promise<object>} - Resolves with a page of deployments
*/
async function listDeployments() {
const response = await this.request("/deployments", {
method: "GET",
});
return response.json();
}
module.exports = {
predictions: {
create: createPrediction,
},
get: getDeployment,
create: createDeployment,
update: updateDeployment,
list: listDeployments,
delete: deleteDeployment,
};