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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 60 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,18 @@ npm install replicate

## Usage

Create the client:
Set your `REPLICATE_API_TOKEN` in your environment:

```js
import Replicate from "replicate";

const replicate = new Replicate({
// get your token from https://replicate.com/account
auth: "my api token", // defaults to process.env.REPLICATE_API_TOKEN
});
```sh
# get your token from https://replicate.com/account
export REPLICATE_API_TOKEN="r8_123..."
```

Run a model and await the result:

```js
import replicate from "replicate";

const model = "stability-ai/stable-diffusion:27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478";
const input = {
prompt: "a 19th century portrait of a raccoon gentleman wearing a suit",
Expand Down Expand Up @@ -94,8 +92,12 @@ const output = await replicate.run(model, { input });

### Constructor

You can create a custom instance of the Replicate client using the `replicate.Replicate` constructor:

```js
const replicate = new Replicate(options);
import replicate from "replicate";

const replicate = new replicate.Replicate(options);
```

| name | type | description |
Expand All @@ -121,10 +123,10 @@ you can install a fetch function from an external package like
and pass it to the `fetch` option in the constructor.

```js
import Replicate from "replicate";
import replicate from "replicate";
import fetch from "cross-fetch";

const replicate = new Replicate({ fetch });
const replicate = new replicate.Replicate({ fetch });
```

You can also use the `fetch` option to add custom behavior to client requests,
Expand Down Expand Up @@ -778,4 +780,50 @@ You can call this method directly to make other requests to the API.

## TypeScript

The `Replicate` constructor and all `replicate.*` methods are fully typed.
The `Replicate` constructor and all `replicate.*` methods are fully typed. Types are accessible
via the named exports:

```ts
import type { Model, Prediction } from "replicate";
```

## Deprecated Constructor

Earlier versions of the Replicate library exported the `Replicate` constructor as the default
export. This will be removed in a future version, to migrate please update your code to use
the following pattern:

If you don't need to customize your Replicate client you can just remove the constructor code
entirely:

```js
// Deprecated
import Replicate from "replicate";

const replicate = new Replicate();

replicate.run(...);

// Fixed
import replicate from "replicate";

replicate.run(...);
```

If you need the Replicate construtor it's available on the `replicate` object.

```js
// Deprecated
import Replicate from "replicate";

const replicate = new Replicate({auth: "my-token"});

replicate.run(...);

// Fixed
import replicate from "replicate";

replicate = new replicate.Replicate({auth: "my-token"});

replicate.run(...);
```
78 changes: 74 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
declare module "replicate" {
type Status = "starting" | "processing" | "succeeded" | "failed" | "canceled";
type Visibility = "public" | "private";
type WebhookEventType = "start" | "output" | "logs" | "completed";
export type Status = "starting" | "processing" | "succeeded" | "failed" | "canceled";
export type Visibility = "public" | "private";
export type WebhookEventType = "start" | "output" | "logs" | "completed";

export interface ApiError extends Error {
request: Request;
Expand Down Expand Up @@ -82,7 +82,7 @@ declare module "replicate" {
retry?: number;
}

export default class Replicate {
export class Replicate {
constructor(options?: {
auth?: string;
userAgent?: string;
Expand Down Expand Up @@ -223,4 +223,74 @@ declare module "replicate" {
list(): Promise<Page<Training>>;
};
}

/** @deprecated */
class DeprecatedReplicate extends Replicate {
/** @deprecated Use `const Replicate = require("replicate").Replicate` instead */
constructor(...args: ConstructorParameters<typeof Replicate>);
}


/**
* Default instance of the Replicate class that gets the access token
* from the REPLICATE_API_TOKEN environment variable.
*
* Create a new Replicate API client instance.
*
* @example
*
* import replicate from "replicate";
*
* // Run a model and await the result:
* const model = 'owner/model:version-id'
* const input = {text: 'Hello, world!'}
* const output = await replicate.run(model, { input });
*
* @remarks
*
* NOTE: Use of this object as a constructor is deprecated and will
* be removed in a future version. Import the Replicate constructor
* instead:
*
* ```
* const Replicate = require("replicate").Replicate;
* ```
*
* Or using esm:
*
* ```
* import replicate from "replicate";
* const client = new replicate.Replicate({...});
* ```
*
* @type { Replicate & typeof DeprecatedReplicate & {ApiError: ApiError, Replicate: Replicate} }
*/
const replicate: Replicate & typeof DeprecatedReplicate & {
/**
* Create a new Replicate API client instance.
*
* @example
* // Create a new Replicate API client instance
* const Replicate = require("replicate");
* const replicate = new Replicate({
* // get your token from https://replicate.com/account
* auth: process.env.REPLICATE_API_TOKEN,
* userAgent: "my-app/1.2.3"
* });
*
* // Run a model and await the result:
* const model = 'owner/model:version-id'
* const input = {text: 'Hello, world!'}
* const output = await replicate.run(model, { input });
*
* @param {object} options - Configuration options for the client
* @param {string} options.auth - API access token. Defaults to the `REPLICATE_API_TOKEN` environment variable.
* @param {string} options.userAgent - Identifier of your app
* @param {string} [options.baseUrl] - Defaults to https://api.replicate.com/v1
* @param {Function} [options.fetch] - Fetch function to use. Defaults to `globalThis.fetch`
*/
Replicate: typeof Replicate
};

export default replicate;
}
Loading
Loading