Replicate Provider

Replicate is a platform for running open-source AI models. It is a popular choice for running image generation models.

Setup

The Replicate provider is available via the @ai-sdk/replicate module. You can install it with

pnpm
npm
yarn
pnpm add @ai-sdk/replicate

Provider Instance

You can import the default provider instance replicate from @ai-sdk/replicate:

import { replicate } from '@ai-sdk/replicate';

If you need a customized setup, you can import createReplicate from @ai-sdk/replicate and create a provider instance with your settings:

import { createReplicate } from '@ai-sdk/replicate';
const replicate = createReplicate({
apiKey: process.env.REPLICATE_API_KEY ?? '',
});

You can use the following optional settings to customize the Replicate provider instance:

  • baseURL string

    Use a different URL prefix for API calls, e.g. to use proxy servers. The default prefix is https://api.replicate.com/v1.

  • apiToken string

    API token that is being sent using the Authorization header. It defaults to the REPLICATE_API_TOKEN environment variable.

  • headers Record<string,string>

    Custom headers to include in the requests.

  • fetch (input: RequestInfo, init?: RequestInit) => Promise<Response>

    Custom fetch implementation.

Image Models

You can create Replicate image models using the .image() factory method. For more on image generation with the AI SDK see generateImage().

Model support for size and other parameters varies by model. Check the model's documentation on Replicate for supported options and additional parameters that can be passed via providerOptions.replicate.

Basic Usage

import { replicate } from '@ai-sdk/replicate';
import { experimental_generateImage as generateImage } from 'ai';
const { image } = await generateImage({
model: replicate.image('black-forest-labs/flux-schnell'),
prompt: 'The Loch Ness Monster getting a manicure',
aspectRatio: '16:9',
});

Model-specific options

import { replicate } from '@ai-sdk/replicate';
import { experimental_generateImage as generateImage } from 'ai';
const { image } = await generateImage({
model: replicate.image('recraft-ai/recraft-v3'),
prompt: 'The Loch Ness Monster getting a manicure',
size: '1365x1024',
providerOptions: {
replicate: {
style: 'realistic_image',
},
},
});

For more details, see the Replicate models page.