Skip to content

Commit e09ffab

Browse files
committed
Initial client
1 parent 1f15648 commit e09ffab

File tree

6 files changed

+239
-27
lines changed

6 files changed

+239
-27
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"editor.formatOnSave": true
3+
}

README.md

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,55 @@ This is a design document for figuring out the design of the future JavaScript c
88

99
Install with `npm install replicate`
1010

11+
Set your API token as `REPLICATE_API_TOKEN` in your environment.
12+
13+
To run a prediction and return its
14+
1115
```jsx
12-
import Replicate from "@replicate/client";
16+
import replicate from "@replicate/client";
1317

14-
const replicate = new Replicate({
15-
// get your token from https://replicate.com/account
16-
auth: "<YOUR TOKEN HERE>",
17-
userAgent: "my-app/1.2.3",
18+
let output = await replicate.predict({
19+
version: "<MODEL VERSION>",
20+
input: {
21+
// your model inputs need to be set here
22+
prompt: "painting of a cat by andy warhol",
23+
},
1824
});
1925

20-
const prediction = await replicate.createAndAwaitPrediction({
26+
// "https://replicate.delivery/pbxt/lGWovsQZ7jZuNtPvofMth1rSeCcVn5xes8dWWdWZ64MlTi7gA/out-0.png"
27+
```
28+
29+
Or, you can run a prediction in the background to do more advanced stuff:
30+
31+
```jsx
32+
let prediction = await replicate.predictions.create({
2133
version: "<MODEL VERSION>",
2234
input: {
23-
// your model inputs need to be set here
2435
prompt: "painting of a cat by andy warhol",
2536
},
2637
});
2738

28-
// {
29-
// id: "ufawqhfynnddngldkgtslldrkq",
30-
// version: "5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa",
31-
// urls: {
32-
// get: "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq",
33-
// cancel: "https://api.replicate.com/v1/predictions/ufawqhfynnddngldkgtslldrkq/cancel"
34-
// },
35-
// created_at: "2020-02-02T00:00:00.00000Z",
36-
// started_at: "2022-02-02T00:01:00.00000Z",
37-
// completed_at: "2022-02-02T00:02:00.00000Z",
38-
// status: "starting",
39-
// input: {
40-
// text: "Alice"
41-
// },
42-
// output: [
43-
// "https://replicate.com/api/models/stability-ai/stable-diffusion/files/9c3b6fe4-2d37-4571-a17a-83951b1cb120/out-0.png"
44-
// ],
45-
// error: null,
46-
// metrics: {}
47-
// }
39+
prediction.status
40+
// "starting"
41+
42+
await prediction.reload()
43+
prediction.status
44+
// "processing"
45+
46+
prediction.logs
47+
// Using seed: 53168
48+
// 0%| | 0/50 [00:00<?, ?it/s]
49+
// 2%|▏ | 1/50 [00:00<00:12, 3.83it/s]
50+
// ...
51+
52+
await prediction.wait()
53+
54+
prediction.status
55+
// "succeeded"
56+
57+
prediction.output
58+
// "https://replicate.delivery/pbxt/lGWovsQZ7jZuNtPvofMth1rSeCcVn5xes8dWWdWZ64MlTi7gA/out-0.png"
59+
4860
```
4961

5062
## Features

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Client from "./lib/client.js";
2+
const replicate = new Client({});
3+
export default replicate;

lib/client.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import fetch from "node-fetch";
2+
import pkg from "../package.json" assert { type: "json" };
3+
4+
class Prediction {
5+
static fromJSON(json) {
6+
const prediction = new Prediction();
7+
Object.assign(prediction, json);
8+
// TODO: make prediction.version a Version object
9+
return prediction;
10+
}
11+
}
12+
13+
class PredictionCollection {
14+
constructor({ client }) {
15+
this.client = client;
16+
}
17+
18+
async create({ version, input }) {
19+
const response = await this.client._request({
20+
method: "POST",
21+
path: `/v1/predictions`,
22+
body: {
23+
version,
24+
input,
25+
},
26+
});
27+
return Prediction.fromJSON(response);
28+
}
29+
30+
async get({ id }) {
31+
const response = await this.client._request({
32+
method: "GET",
33+
path: `/v1/predictions/${id}`,
34+
});
35+
return Prediction.fromJSON(response);
36+
}
37+
38+
async list() {
39+
const response = await this.client._request({
40+
method: "GET",
41+
path: `/v1/predictions`,
42+
});
43+
// TODO pagination
44+
return response.results.map((prediction) =>
45+
Prediction.fromJSON(prediction)
46+
);
47+
}
48+
}
49+
50+
export default class Client {
51+
constructor({ token }) {
52+
this.token = token || process.env.REPLICATE_API_TOKEN;
53+
this.predictions = new PredictionCollection({ client: this });
54+
}
55+
56+
async _request({ method, path, body }) {
57+
const response = await fetch(`https://api.replicate.com${path}`, {
58+
method: method,
59+
headers: this._headers(),
60+
body: JSON.stringify(body),
61+
});
62+
return await response.json();
63+
}
64+
65+
_headers() {
66+
return {
67+
Authorization: `Token ${this.token}`,
68+
"User-Agent": `replicate-js@${pkg.version}`,
69+
};
70+
}
71+
}

package-lock.json

Lines changed: 100 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "replicate",
3+
"version": "0.0.1",
4+
"description": "This is a design document for figuring out the design of the future JavaScript client for Replicate’s HTTP API. Let’s design the API here, before we write any code. README-driven development!",
5+
"main": "index.js",
6+
"type": "module",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/replicate/replicate-js.git"
13+
},
14+
"author": "",
15+
"license": "Apache-2.0",
16+
"bugs": {
17+
"url": "https://github.com/replicate/replicate-js/issues"
18+
},
19+
"homepage": "https://github.com/replicate/replicate-js#readme",
20+
"dependencies": {
21+
"node-fetch": "^3.3.0"
22+
}
23+
}

0 commit comments

Comments
 (0)