-
Notifications
You must be signed in to change notification settings - Fork 204
/
index.js
431 lines (381 loc) · 14.3 KB
/
index.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
const ApiError = require("./lib/error");
const ModelVersionIdentifier = require("./lib/identifier");
const { createReadableStream, createFileOutput } = require("./lib/stream");
const {
transform,
withAutomaticRetries,
validateWebhook,
parseProgressFromLogs,
streamAsyncIterator,
} = require("./lib/util");
const accounts = require("./lib/accounts");
const collections = require("./lib/collections");
const deployments = require("./lib/deployments");
const files = require("./lib/files");
const hardware = require("./lib/hardware");
const models = require("./lib/models");
const predictions = require("./lib/predictions");
const trainings = require("./lib/trainings");
const webhooks = require("./lib/webhooks");
const packageJSON = require("./package.json");
/**
* Replicate API client library
*
* @see https://replicate.com/docs/reference/http
* @example
* // Create a new Replicate API client instance
* const Replicate = require("replicate");
* const replicate = new Replicate({
* // get your token from https://replicate.com/account
* auth: process.env.REPLICATE_API_TOKEN,
* userAgent: "my-app/1.2.3"
* });
*
* // Run a model and await the result:
* const model = 'owner/model:version-id'
* const input = {text: 'Hello, world!'}
* const output = await replicate.run(model, { input });
*/
class Replicate {
/**
* Create a new Replicate API client instance.
*
* @param {object} options - Configuration options for the client
* @param {string} options.auth - API access token. Defaults to the `REPLICATE_API_TOKEN` environment variable.
* @param {string} options.userAgent - Identifier of your app
* @param {string} [options.baseUrl] - Defaults to https://api.replicate.com/v1
* @param {Function} [options.fetch] - Fetch function to use. Defaults to `globalThis.fetch`
* @param {boolean} [options.useFileOutput] - Set to `false` to disable `FileOutput` objects from `run` instead of URLs, defaults to true.
* @param {"default" | "upload" | "data-uri"} [options.fileEncodingStrategy] - Determines the file encoding strategy to use
*/
constructor(options = {}) {
this.auth =
options.auth ||
(typeof process !== "undefined" ? process.env.REPLICATE_API_TOKEN : null);
this.userAgent =
options.userAgent || `replicate-javascript/${packageJSON.version}`;
this.baseUrl = options.baseUrl || "https://api.replicate.com/v1";
this.fetch = options.fetch || globalThis.fetch;
this.fileEncodingStrategy = options.fileEncodingStrategy || "default";
this.useFileOutput = options.useFileOutput === false ? false : true;
this.accounts = {
current: accounts.current.bind(this),
};
this.collections = {
list: collections.list.bind(this),
get: collections.get.bind(this),
};
this.deployments = {
get: deployments.get.bind(this),
create: deployments.create.bind(this),
update: deployments.update.bind(this),
delete: deployments.delete.bind(this),
list: deployments.list.bind(this),
predictions: {
create: deployments.predictions.create.bind(this),
},
};
this.files = {
create: files.create.bind(this),
get: files.get.bind(this),
list: files.list.bind(this),
delete: files.delete.bind(this),
};
this.hardware = {
list: hardware.list.bind(this),
};
this.models = {
get: models.get.bind(this),
list: models.list.bind(this),
create: models.create.bind(this),
versions: {
list: models.versions.list.bind(this),
get: models.versions.get.bind(this),
},
search: models.search.bind(this),
};
this.predictions = {
create: predictions.create.bind(this),
get: predictions.get.bind(this),
cancel: predictions.cancel.bind(this),
list: predictions.list.bind(this),
};
this.trainings = {
create: trainings.create.bind(this),
get: trainings.get.bind(this),
cancel: trainings.cancel.bind(this),
list: trainings.list.bind(this),
};
this.webhooks = {
default: {
secret: {
get: webhooks.default.secret.get.bind(this),
},
},
};
}
/**
* Run a model and wait for its output.
*
* @param {string} ref - Required. The model version identifier in the format "owner/name" or "owner/name:version"
* @param {object} options
* @param {object} options.input - Required. An object with the model inputs
* @param {{mode: "block", timeout?: number, interval?: number} | {mode: "poll", interval?: number }} [options.wait] - Options for waiting for the prediction to finish. If `wait` is explicitly true, the function will block and wait for the prediction to finish.
* @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 {AbortSignal} [options.signal] - AbortSignal to cancel the prediction
* @param {Function} [progress] - Callback function that receives the prediction object as it's updated. The function is called when the prediction is created, each time its updated while polling for completion, and when it's completed.
* @throws {Error} If the reference is invalid
* @throws {Error} If the prediction failed
* @returns {Promise<object>} - Resolves with the output of running the model
*/
async run(ref, options, progress) {
const { wait = { mode: "block" }, signal, ...data } = options;
const identifier = ModelVersionIdentifier.parse(ref);
let prediction;
if (identifier.version) {
prediction = await this.predictions.create({
...data,
version: identifier.version,
wait: wait.mode === "block" ? wait.timeout ?? true : false,
});
} else if (identifier.owner && identifier.name) {
prediction = await this.predictions.create({
...data,
model: `${identifier.owner}/${identifier.name}`,
wait: wait.mode === "block" ? wait.timeout ?? true : false,
});
} else {
throw new Error("Invalid model version identifier");
}
// Call progress callback with the initial prediction object
if (progress) {
progress(prediction);
}
const isDone = wait.mode === "block" && prediction.status !== "starting";
if (!isDone) {
prediction = await this.wait(
prediction,
{ interval: wait.mode === "poll" ? wait.interval : undefined },
async (updatedPrediction) => {
// Call progress callback with the updated prediction object
if (progress) {
progress(updatedPrediction);
}
// We handle the cancel later in the function.
if (signal && signal.aborted) {
return true; // stop polling
}
return false; // continue polling
}
);
}
if (signal && signal.aborted) {
prediction = await this.predictions.cancel(prediction.id);
}
// Call progress callback with the completed prediction object
if (progress) {
progress(prediction);
}
if (prediction.status === "failed") {
throw new Error(`Prediction failed: ${prediction.error}`);
}
return transform(prediction.output, (value) => {
if (
typeof value === "string" &&
(value.startsWith("https:") || value.startsWith("data:"))
) {
return this.useFileOutput
? createFileOutput({ url: value, fetch: this.fetch })
: value;
}
return value;
});
}
/**
* Make a request to the Replicate API.
*
* @param {string} route - REST API endpoint path
* @param {object} options - Request parameters
* @param {string} [options.method] - HTTP method. Defaults to GET
* @param {object} [options.params] - Query parameters
* @param {object|Headers} [options.headers] - HTTP headers
* @param {object} [options.data] - Body parameters
* @returns {Promise<Response>} - Resolves with the response object
* @throws {ApiError} If the request failed
*/
async request(route, options) {
const { auth, baseUrl, userAgent } = this;
let url;
if (route instanceof URL) {
url = route;
} else {
url = new URL(
route.startsWith("/") ? route.slice(1) : route,
baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`
);
}
const { method = "GET", params = {}, data } = options;
for (const [key, value] of Object.entries(params)) {
url.searchParams.append(key, value);
}
const headers = {
"Content-Type": "application/json",
"User-Agent": userAgent,
};
if (auth) {
headers["Authorization"] = `Bearer ${auth}`;
}
if (options.headers) {
for (const [key, value] of Object.entries(options.headers)) {
headers[key] = value;
}
}
let body = undefined;
if (data instanceof FormData) {
body = data;
// biome-ignore lint/performance/noDelete:
delete headers["Content-Type"]; // Use automatic content type header
} else if (data) {
body = JSON.stringify(data);
}
const init = {
method,
headers,
body,
};
const shouldRetry =
method === "GET"
? (response) => response.status === 429 || response.status >= 500
: (response) => response.status === 429;
// Workaround to fix `TypeError: Illegal invocation` error in Cloudflare Workers
// https://github.com/replicate/replicate-javascript/issues/134
const _fetch = this.fetch; // eslint-disable-line no-underscore-dangle
const response = await withAutomaticRetries(async () => _fetch(url, init), {
shouldRetry,
});
if (!response.ok) {
const request = new Request(url, init);
const responseText = await response.text();
throw new ApiError(
`Request to ${url} failed with status ${response.status} ${response.statusText}: ${responseText}.`,
request,
response
);
}
return response;
}
/**
* Stream a model and wait for its output.
*
* @param {string} identifier - Required. The model version identifier in the format "{owner}/{name}:{version}"
* @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 {AbortSignal} [options.signal] - AbortSignal to cancel the prediction
* @throws {Error} If the prediction failed
* @yields {ServerSentEvent} Each streamed event from the prediction
*/
async *stream(ref, options) {
const { wait, signal, ...data } = options;
const identifier = ModelVersionIdentifier.parse(ref);
let prediction;
if (identifier.version) {
prediction = await this.predictions.create({
...data,
version: identifier.version,
});
} else if (identifier.owner && identifier.name) {
prediction = await this.predictions.create({
...data,
model: `${identifier.owner}/${identifier.name}`,
});
} else {
throw new Error("Invalid model version identifier");
}
if (prediction.urls && prediction.urls.stream) {
const stream = createReadableStream({
url: prediction.urls.stream,
fetch: this.fetch,
...(signal ? { options: { signal } } : {}),
});
yield* streamAsyncIterator(stream);
} else {
throw new Error("Prediction does not support streaming");
}
}
/**
* Paginate through a list of results.
*
* @generator
* @example
* for await (const page of replicate.paginate(replicate.predictions.list) {
* console.log(page);
* }
* @param {Function} endpoint - Function that returns a promise for the next page of results
* @yields {object[]} Each page of results
*/
async *paginate(endpoint) {
const response = await endpoint();
yield response.results;
if (response.next) {
const nextPage = () =>
this.request(response.next, { method: "GET" }).then((r) => r.json());
yield* this.paginate(nextPage);
}
}
/**
* Wait for a prediction to finish.
*
* If the prediction has already finished,
* this function returns immediately.
* Otherwise, it polls the API until the prediction finishes.
*
* @async
* @param {object} prediction - Prediction object
* @param {object} options - Options
* @param {number} [options.interval] - Polling interval in milliseconds. Defaults to 500
* @param {Function} [stop] - Async callback function that is called after each polling attempt. Receives the prediction object as an argument. Return false to cancel polling.
* @throws {Error} If the prediction doesn't complete within the maximum number of attempts
* @throws {Error} If the prediction failed
* @returns {Promise<object>} Resolves with the completed prediction object
*/
async wait(prediction, options, stop) {
const { id } = prediction;
if (!id) {
throw new Error("Invalid prediction");
}
if (
prediction.status === "succeeded" ||
prediction.status === "failed" ||
prediction.status === "canceled"
) {
return prediction;
}
// eslint-disable-next-line no-promise-executor-return
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const interval = (options && options.interval) || 500;
let updatedPrediction = await this.predictions.get(id);
while (
updatedPrediction.status !== "succeeded" &&
updatedPrediction.status !== "failed" &&
updatedPrediction.status !== "canceled"
) {
/* eslint-disable no-await-in-loop */
if (stop && (await stop(updatedPrediction)) === true) {
break;
}
await sleep(interval);
updatedPrediction = await this.predictions.get(prediction.id);
/* eslint-enable no-await-in-loop */
}
if (updatedPrediction.status === "failed") {
throw new Error(`Prediction failed: ${updatedPrediction.error}`);
}
return updatedPrediction;
}
}
module.exports = Replicate;
module.exports.validateWebhook = validateWebhook;
module.exports.parseProgressFromLogs = parseProgressFromLogs;