|
1 | 1 | # Replicate JavaScript client |
2 | 2 |
|
3 | | -A JavaScript client for [Replicate](https://replicate.com/). It lets you run models from Node.js and the web (coming soon!), and various other things on Replicate. |
| 3 | +A JavaScript client for [Replicate](https://replicate.com/). It lets you run |
| 4 | +models from Node.js and the web (coming soon!), and do various other things with |
| 5 | +Replicate. |
4 | 6 |
|
5 | 7 | ## Usage |
6 | 8 |
|
7 | 9 | Install with `npm install replicate` |
8 | 10 |
|
9 | | -Set your API token as `REPLICATE_API_TOKEN` in your environment. |
| 11 | +Set your API token as an environment variable called `REPLICATE_API_TOKEN`. |
10 | 12 |
|
11 | 13 | To run a prediction and return its output: |
12 | 14 |
|
13 | | -```jsx |
14 | | -import replicate from "@replicate/client"; |
| 15 | +```js |
| 16 | +import replicate from "replicate"; |
15 | 17 |
|
16 | | -let output = await replicate.predict({ |
17 | | - version: "<MODEL VERSION>", |
18 | | - input: { |
19 | | - // your model inputs need to be set here |
20 | | - prompt: "painting of a cat by andy warhol", |
21 | | - }, |
22 | | -}); |
23 | | - |
24 | | -// "https://replicate.delivery/pbxt/lGWovsQZ7jZuNtPvofMth1rSeCcVn5xes8dWWdWZ64MlTi7gA/out-0.png" |
25 | | -``` |
26 | | - |
27 | | -Or, you can run a prediction in the background to do more advanced stuff: |
28 | | - |
29 | | -```jsx |
30 | | -let prediction = await replicate.predictions.create({ |
31 | | - version: "<MODEL VERSION>", |
32 | | - input: { |
33 | | - prompt: "painting of a cat by andy warhol", |
34 | | - }, |
35 | | -}); |
36 | | - |
37 | | -prediction.status |
38 | | -// "starting" |
39 | | - |
40 | | -await prediction.reload() |
41 | | -prediction.status |
42 | | -// "processing" |
43 | | - |
44 | | -prediction.logs |
45 | | -// Using seed: 53168 |
46 | | -// 0%| | 0/50 [00:00<?, ?it/s] |
47 | | -// 2%|▏ | 1/50 [00:00<00:12, 3.83it/s] |
48 | | -// ... |
49 | | - |
50 | | -await prediction.wait() |
| 18 | +// Set your model's input parameters here |
| 19 | +const input = { |
| 20 | + prompt: "painting of a cat by andy warhol", |
| 21 | +}; |
51 | 22 |
|
52 | | -prediction.status |
53 | | -// "succeeded" |
| 23 | +const prediction = await replicate.predict("<MODEL VERSION>", input); |
54 | 24 |
|
55 | | -prediction.output |
| 25 | +console.log(prediction.output); |
56 | 26 | // "https://replicate.delivery/pbxt/lGWovsQZ7jZuNtPvofMth1rSeCcVn5xes8dWWdWZ64MlTi7gA/out-0.png" |
57 | | - |
58 | | -``` |
59 | | - |
60 | | -## Features |
61 | | - |
62 | | -- Implements best practices such as throttling requests |
63 | | -- Pagination |
64 | | -- Fully typed |
65 | | - |
66 | | -## API |
67 | | - |
68 | | -### Constructor |
69 | | - |
70 | | -```jsx |
71 | | -const replicate = new Replicate(options); |
72 | | -``` |
73 | | - |
74 | | -| name | type | description | |
75 | | -| ----------------- | ------ | ---------------------------------------- | |
76 | | -| options.auth | string | Required. API access token | |
77 | | -| options.userAgent | string | Required. Identifier of your app | |
78 | | -| options.baseUrl | string | Defaults to https://api.replicate.com/v1 | |
79 | | - |
80 | | -### `replicate.models.get` |
81 | | - |
82 | | -```jsx |
83 | | -const response = await replicate.models.get(options); |
84 | | -``` |
85 | | - |
86 | | -| name | type | description | |
87 | | -| ------------------- | ------ | ------------------------------------------------------------------- | |
88 | | -| options.model_owner | string | Required. The name of the user or organization that owns the model. | |
89 | | -| options.model_name | string | Required. The name of the model. | |
90 | | - |
91 | | -Example for `response.data` |
92 | | - |
93 | | -```jsx |
94 | | -{ |
95 | | - url: "https://replicate.com/replicate/hello-world", |
96 | | - owner: "replicate", |
97 | | - name: "hello-world", |
98 | | - description: "A tiny model that says hello", |
99 | | - visibility: "public", |
100 | | - github_url: "https://github.com/replicate/cog-examples", |
101 | | - paper_url: null, |
102 | | - license_url: null, |
103 | | - latest_version: { /* ... */ } |
104 | | -} |
105 | | -``` |
106 | | - |
107 | | -### `replicate.models.listVersions` |
108 | | - |
109 | | -```jsx |
110 | | -const response = await replicate.models.listVersions(options); |
111 | | -``` |
112 | | - |
113 | | -| name | type | description | |
114 | | -| ------------------- | ------ | ------------------------------------------------------------------- | |
115 | | -| options.model_owner | string | Required. The name of the user or organization that owns the model. | |
116 | | -| options.model_name | string | Required. The name of the model. | |
117 | | - |
118 | | -Example for `response.data` |
119 | | - |
120 | | -```jsx |
121 | | -{ |
122 | | - previous: null, |
123 | | - next: null, |
124 | | - results: [ |
125 | | - { |
126 | | - id: "5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa", |
127 | | - created_at: "2022-04-26T19:29:04.418669Z", |
128 | | - cog_version: "0.3.0", |
129 | | - openapi_schema: { /* ... */ } |
130 | | - }, |
131 | | - { |
132 | | - id: "e2e8c39e0f77177381177ba8c4025421ec2d7e7d3c389a9b3d364f8de560024f", |
133 | | - created_at: "2022-03-21T13:01:04.418669Z", |
134 | | - cog_version: "0.3.0", |
135 | | - openapi_schema: { /* ... */ } |
136 | | - } |
137 | | - ] |
138 | | -} |
139 | | -``` |
140 | | - |
141 | | -### `replicate.models.getVersion` |
142 | | - |
143 | | -```jsx |
144 | | -const response = await replicate.models.getVersion(options); |
145 | | -``` |
146 | | - |
147 | | -| name | type | description | |
148 | | -| ------------------- | ------ | ------------------------------------------------------------------- | |
149 | | -| options.model_owner | string | Required. The name of the user or organization that owns the model. | |
150 | | -| options.model_name | string | Required. The name of the model. | |
151 | | -| options.id | string | Required. The model version | |
152 | | - |
153 | | -Example for `response.data` |
154 | | - |
155 | | -```jsx |
156 | | -{ |
157 | | - id: "5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa", |
158 | | - created_at: "2022-04-26T19:29:04.418669Z", |
159 | | - cog_version: "0.3.0", |
160 | | - openapi_schema: { /* ... */ } |
161 | | -} |
162 | | -``` |
163 | | - |
164 | | -### `replicate.models.getCollection` |
165 | | - |
166 | | -```jsx |
167 | | -const response = await replicate.models.getCollection(options); |
168 | | -``` |
169 | | - |
170 | | -| name | type | description | |
171 | | -| ----------------------- | ------ | -------------------------------------------------------------------------- | |
172 | | -| options.collection_slug | string | Required. The slug of the collection. See http://replicate.com/collections | |
173 | | - |
174 | | -### `replicate.predictions.create` |
175 | | - |
176 | | -```jsx |
177 | | -const response = await replicate.predictions.create(options); |
178 | | -``` |
179 | | - |
180 | | -| name | type | description | |
181 | | -| ------------------------- | ------ | ------------------------------------------------------------------------- | |
182 | | -| options.version | string | Required. The model version | |
183 | | -| options.input | object | Required. An object with the models inputs | |
184 | | -| options.webhook_completed | string | A URL which will receive a POST request upon completion of the prediction | |
185 | | - |
186 | | -Example for `response.data` |
187 | | - |
188 | | -```jsx |
189 | | -{ |
190 | | - id: "ufawqhfynnddngldkgtslldrkq", |
191 | | - version: "5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa", |
192 | | - urls: { |
193 | | - get: "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq", |
194 | | - cancel: "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq/cancel" |
195 | | - }, |
196 | | - created_at: "2022-04-26T22:13:06.224088Z", |
197 | | - started_at: null, |
198 | | - completed_at: null, |
199 | | - status: "succeeded", |
200 | | - input: { |
201 | | - text: "Alice" |
202 | | - }, |
203 | | - output: null, |
204 | | - error: null, |
205 | | - logs: null, |
206 | | - metrics: {} |
207 | | -} |
208 | 27 | ``` |
209 | 28 |
|
210 | | -### `replicate.predictions.get` |
211 | | - |
212 | | -```jsx |
213 | | -const response = await replicate.predictions.get(options); |
214 | | -``` |
| 29 | +If you want to do something like updating progress while the prediction is |
| 30 | +running, you can pass in an `onUpdate` callback function: |
215 | 31 |
|
216 | | -| name | type | description | |
217 | | -| -------------------- | ------ | ----------- | |
218 | | -| options.predictionId | string | Required. | |
| 32 | +```js |
| 33 | +import replicate from "replicate"; |
219 | 34 |
|
220 | | -Example for `response.data` |
| 35 | +// Set your model's input parameters here |
| 36 | +const input = { |
| 37 | + prompt: "painting of a cat by andy warhol", |
| 38 | +}; |
221 | 39 |
|
222 | | -```jsx |
223 | | -{ |
224 | | - id: "ufawqhfynnddngldkgtslldrkq", |
225 | | - version: "5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa", |
226 | | - urls: { |
227 | | - get: "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq", |
228 | | - cancel: "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq/cancel" |
| 40 | +await replicate.predict("<MODEL VERSION>", input, { |
| 41 | + onUpdate: (prediction) => { |
| 42 | + console.log(prediction.output); |
229 | 43 | }, |
230 | | - created_at: "2022-04-26T22:13:06.224088Z", |
231 | | - started_at: null, |
232 | | - completed_at: null, |
233 | | - status: "starting", |
234 | | - input: { |
235 | | - text: "Alice" |
236 | | - }, |
237 | | - output: null, |
238 | | - error: null, |
239 | | - logs: null, |
240 | | - metrics: {} |
241 | | -} |
242 | | -``` |
243 | | - |
244 | | -### `replicate.predictions.list` |
245 | | - |
246 | | -```jsx |
247 | | -const response = await replicate.predictions.list(); |
248 | | -``` |
249 | | - |
250 | | -`replicate.predictions.list()` has no options. |
251 | | - |
252 | | -Example for `response.data` |
253 | | - |
254 | | -```jsx |
255 | | -{ |
256 | | - previous: null, |
257 | | - next: "https://api.replicate.com/v1/predictions?cursor=cD0yMDIyLTAxLTIxKzIzJTNBMTglM0EyNC41MzAzNTclMkIwMCUzQTAw", |
258 | | - results: [ |
259 | | - { |
260 | | - id: "jpzd7hm5gfcapbfyt4mqytarku", |
261 | | - version: "b21cbe271e65c1718f2999b038c18b45e21e4fba961181fbfae9342fc53b9e05", |
262 | | - urls: { |
263 | | - get: "https://api.replicate.com/v1/predictions/jpzd7hm5gfcapbfyt4mqytarku", |
264 | | - cancel: "https://api.replicate.com/v1/predictions/jpzd7hm5gfcapbfyt4mqytarku/cancel" |
265 | | - }, |
266 | | - created_at: "2022-04-26T20:00:40.658234Z", |
267 | | - started_at: "2022-04-26T20:00:84.583803Z", |
268 | | - completed_at: "2022-04-26T20:02:27.648305Z", |
269 | | - source: "web", |
270 | | - status: "succeeded" |
271 | | - }, |
272 | | - /* ... */ |
273 | | - ] |
274 | | -} |
275 | | -``` |
276 | | - |
277 | | -### `replicate.paginate` |
278 | | - |
279 | | -```jsx |
280 | | -const result = await replicate.paginate(replicate.predictions.list); |
281 | | -``` |
282 | | - |
283 | | -Auto-paginates the passed in endpoint and resolves with an array of all results |
284 | | - |
285 | | -### `replicate.paginate.iterator` |
286 | | - |
287 | | -```jsx |
288 | | -const asyncIterator = replicate.paginate.iterator(replicate.predictions.list); |
289 | | -``` |
290 | | - |
291 | | -Same as `replicate.paginate` but can be used as async iterator, e.g. |
292 | | - |
293 | | -```jsx |
294 | | -for await (const response of replicate.paginate.iterator( |
295 | | - replicate.predictions.list |
296 | | -)) { |
297 | | - /* do something with response */ |
298 | | -} |
299 | | -``` |
300 | | - |
301 | | -### `replicate.request` |
302 | | - |
303 | | -```jsx |
304 | | -const response = await replicate.request(route, parameters); |
305 | | -``` |
306 | | - |
307 | | -| name | type | description | |
308 | | -| ------------------ | ------ | ------------------------------------------------------------ | |
309 | | -| options.route | string | Required. REST API endpoint path. | |
310 | | -| options.parameters | object | URL, query, and request body parameters for the given route. | |
311 | | - |
312 | | -The `replicate.request()` method is used under the hood of the other request methods and can be utilized to try out experimental endpoints that have not been documented yet. |
313 | | - |
314 | | -## TypeScript |
315 | | - |
316 | | -The `Replicate` constructor and all `replicate.*` methods are fully typed. |
317 | | - |
318 | | -In order to get types for prediction inputs which depend on model versions, you can extend the `Replicate.ModelInputsByVersion` interface in one of two ways |
319 | | - |
320 | | -### 1. Install type packages from URLs |
321 | | - |
322 | | -Import types for JS/TS usage is to make dynamic type packages installable from the API. For example |
323 | | - |
324 | | -```bash |
325 | | -npm install --save-dev https://api.replicate.com/v1/models/{model_owner}/{model_name}/{version}.tar.gz |
326 | | -``` |
327 | | - |
328 | | -Because the package name begins with `@types/` the typescript language server in VS Code recognizes it automatically. Packages can be installed for multiple models/versions. |
329 | | - |
330 | | -### 2. Define types manually |
331 | | - |
332 | | -The types for each model version can be defined manually in a `replicate-models.d.ts` file that is located in your projects’ root path |
333 | | - |
334 | | -```bash |
335 | | -import { Replicate } from "replicate"; |
336 | | - |
337 | | -declare module "replicate-testing" { |
338 | | - namespace Replicate { |
339 | | - interface ModelInputsByVersion { |
340 | | - affe44672e418f636f9a5cf2e6f9632404a83d692e42ecfa3f3010e467b80659: { |
341 | | - prompt: string; |
342 | | - }; |
343 | | - } |
344 | | - } |
345 | | -} |
346 | | -``` |
347 | | -
|
348 | | -The types can be imported in JS files with a triple slash directives (must be the first line in the file) |
349 | | -
|
350 | | -```bash |
351 | | -/// <reference path="./replicate-models.d.ts" /> |
| 44 | +}); |
352 | 45 | ``` |
353 | 46 |
|
354 | | -In TS files the types will be imported automatically when the file lives in the project root. |
355 | | -
|
356 | | -## Contributing |
357 | | -
|
358 | | -See [CONTRIBUTING.md](https://example.com) |
359 | | -
|
360 | 47 | ## License |
361 | 48 |
|
362 | 49 | [Apache 2.0](LICENSE) |
0 commit comments