Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a singleton instance of Replicate as default entrypoint #188

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

aron
Copy link
Contributor

@aron aron commented Jan 13, 2024

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

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 free to 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.

None yet

1 participant