replicate-javascript
replicate-javascript copied to clipboard
Provide a singleton instance of Replicate as default entrypoint
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.
- The README now promotes the singleton instance with a note about the deprecated constructor.
- 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.
- 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();