From 46fbca2c22c0068d16775a57fa10347cf01277b9 Mon Sep 17 00:00:00 2001 From: Mattt Date: Thu, 21 Sep 2023 07:13:50 -0700 Subject: [PATCH 1/5] Update tests to ensure all requests are mocked (#137) * Change expectation to hasAssertions for exception handling * Disable network access and warn on unmatched mocks --- index.test.ts | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/index.test.ts b/index.test.ts index fb65f29..a560da1 100644 --- a/index.test.ts +++ b/index.test.ts @@ -3,14 +3,30 @@ import Replicate, { ApiError, Prediction } from 'replicate'; import nock from 'nock'; import fetch from 'cross-fetch'; -describe('Replicate client', () => { - let client: Replicate; +let client: Replicate; +const BASE_URL = 'https://api.replicate.com/v1'; + +nock.disableNetConnect(); - const BASE_URL = 'https://api.replicate.com/v1'; +describe('Replicate client', () => { + let unmatched: Object[] = []; + const handleNoMatch = (req: unknown, options: any, body: string) => + unmatched.push({ req, options, body }); beforeEach(() => { client = new Replicate({ auth: 'test-token' }); client.fetch = fetch; + + unmatched = []; + nock.emitter.on("no match", handleNoMatch); + }); + + afterEach(() => { + nock.emitter.off("no match", handleNoMatch); + expect(unmatched).toStrictEqual([]); + + nock.abortPendingRequests(); + nock.cleanAll(); }); describe('constructor', () => { @@ -188,7 +204,7 @@ describe('Replicate client', () => { }, { "Content-Type": "application/json" }) try { - expect.assertions(2); + expect.hasAssertions(); await client.predictions.create({ version: '5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa', From fdf5f76741f3dd78288dfb933c86a00d28d7f8ee Mon Sep 17 00:00:00 2001 From: Zeke Sikelianos Date: Fri, 22 Sep 2023 11:56:09 -0700 Subject: [PATCH 2/5] document the replicate.run() method (#138) --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index 13968ca..a0beff1 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,33 @@ client.fetch = (url, options) => { }; ``` +### `replicate.run` + +Run a model and await the result. Unlike [`replicate.prediction.create`](#replicatepredictionscreate), this method returns only the prediction output rather than the entire prediction object. + +```js +const model = "stability-ai/sdxl:8beff3369e81422112d93b89ca01426147de542cd4684c244b673b105188fe5f"; +const input = { + prompt: "a 19th century portrait of a raccoon gentleman wearing a suit", +}; +const output = await replicate.run(identifier, options, progress); +``` + +| name | type | description | +| ------------------------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `identifier` | string | **Required**. The model version identifier in the format `{owner}/{name}:{version}`, for example `stability-ai/sdxl:8beff3369e81422112d93b89ca01426147de542cd4684c244b673b105188fe5f` | +| `options.input` | object | **Required**. An object with the model inputs. | +| `options.wait` | object | Options for waiting for the prediction to finish | +| `options.wait.interval` | number | Polling interval in milliseconds. Defaults to 500 | +| `options.webhook` | string | An HTTPS URL for receiving a webhook when the prediction has new output | +| `options.webhook_events_filter` | string[] | An array of events which should trigger [webhooks](https://replicate.com/docs/webhooks). Allowable values are `start`, `output`, `logs`, and `completed` | +| `options.signal` | object | An [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to cancel the prediction | +| `progress` | function | 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 prediction failed. + +Returns `Promise` which resolves with the output of running the model. + ### `replicate.models.get` ```js From 11f0e6e11d1c38c0e1b9584f6451199b348158e1 Mon Sep 17 00:00:00 2001 From: Zeke Sikelianos Date: Fri, 22 Sep 2023 12:03:09 -0700 Subject: [PATCH 3/5] fix run usage docs (#139) --- README.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a0beff1..2842358 100644 --- a/README.md +++ b/README.md @@ -150,10 +150,6 @@ client.fetch = (url, options) => { Run a model and await the result. Unlike [`replicate.prediction.create`](#replicatepredictionscreate), this method returns only the prediction output rather than the entire prediction object. ```js -const model = "stability-ai/sdxl:8beff3369e81422112d93b89ca01426147de542cd4684c244b673b105188fe5f"; -const input = { - prompt: "a 19th century portrait of a raccoon gentleman wearing a suit", -}; const output = await replicate.run(identifier, options, progress); ``` @@ -172,6 +168,14 @@ Throws `Error` if the prediction failed. Returns `Promise` which resolves with the output of running the model. +Example: + +```js +const model = "stability-ai/sdxl:8beff3369e81422112d93b89ca01426147de542cd4684c244b673b105188fe5f"; +const input = { prompt: "a 19th century portrait of a raccoon gentleman wearing a suit" }; +const output = await replicate.run(model, { input }); +``` + ### `replicate.models.get` ```js From 97c9effb80afb373b7ddc1c705c88b36afff966b Mon Sep 17 00:00:00 2001 From: akiva Date: Sun, 24 Sep 2023 09:03:25 -0700 Subject: [PATCH 4/5] Update index.d.ts (#140) The API call is actually in the opposite order --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 7f4461c..50ebd1c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -160,8 +160,8 @@ declare module 'replicate' { deployments: { predictions: { create( - deployment_name: string, deployment_owner: string, + deployment_name: string, options: { input: object; stream?: boolean; From 4b0d9cb0e226fab3d3d31de5b32261485acf5626 Mon Sep 17 00:00:00 2001 From: Mattt Zmuda Date: Sun, 24 Sep 2023 09:03:50 -0700 Subject: [PATCH 5/5] 0.18.1 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1e52f8b..9983fbc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "replicate", - "version": "0.18.0", + "version": "0.18.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "replicate", - "version": "0.18.0", + "version": "0.18.1", "license": "Apache-2.0", "devDependencies": { "@types/jest": "^29.5.3", diff --git a/package.json b/package.json index 1dd4c07..1a01fa3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "replicate", - "version": "0.18.0", + "version": "0.18.1", "description": "JavaScript client for Replicate", "repository": "github:replicate/replicate-javascript", "homepage": "https://github.com/replicate/replicate-javascript#readme",