Skip to content

Provide a singleton instance of Replicate as default entrypoint - #188

Open
aron wants to merge 4 commits into
mainfrom
single-instance-entrypoint
Open

Provide a singleton instance of Replicate as default entrypoint#188
aron wants to merge 4 commits into
mainfrom
single-instance-entrypoint

Conversation

@aron

@aronaron commented Jan 13, 2024

Copy link
Copy Markdown
Contributor

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

importreplicatereplicate.run(...)

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

constreplicate=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:

importreplicatefrom"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();

This makes the library very quick to get up and running. 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.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@aron