replicate-javascript icon indicating copy to clipboard operation
replicate-javascript copied to clipboard

Provide a singleton instance of Replicate as default entrypoint

Open aron opened this issue 5 months ago • 0 comments

The Python library currently exports a singleton instance which can be used to run a model in a two liner:

import replicate

replicate.run(...)

Our JavaScript library has a different interface that requires the replicate instance to be instantiated. Our examples all include the line:

const replicate = Replicate();

This change removes the need to create the client instance, and brings it inline with the Python client making the library very quick to get up and running. Basic usage is now:

import replicate from "replicate";

replicate.run(...);

To ensure this is a backwards compatible change we use a Proxy instance to ensure that the library keeps working in its existing state though with a @deprecated type annotation which will show up in editors.

The constructor is still available via replicate.Replicate() for users that need to customize the configuration.

Changes

I've made the following additional changes.

  1. The README now promotes the singleton instance with a note about the deprecated constructor.
  2. The integration tests now have one file per import style, I've removed the index file and we just have tests now to keep it clean.
  3. I've added some documentation to the index.d.ts but as a next step I'd like to generate this from the index.js file itself.

Notes on ESM modules

In a next step I'd like to provide an ESM specific build which will remove the slightly janky flow (naming collision) for ESM & TypeScript users. (This is done in #191).

import replicate from "replicate";

const client = new replicate.Replicate();


// becomes
import { Replicate } from "replicate";

const replicate = new Replicate();

aron avatar Jan 13 '24 22:01 aron