Prerequisites

Setup

  1. Add the Replicate API key to your environment variables
.env
REPLICATE_API_TOKEN=your-api-key
  1. The boilerplate already includes the necessary configuration for Replicate in src/lib/ai/replicate.ts. This file initializes the Replicate client with your API token.

Usage

To use Replicate AI in your application, you can create an API route that interacts with the Replicate client. This route can be used to generate images, text, and more using the AI models provided by Replicate. Create a new file in your API routes directory (e.g., app/api/replicate/route.ts). This file will contain the logic for interacting with the Replicate client. Here’s an example of how to set up a route that uses the Flux model:

import { kv } from '@vercel/kv';
import { replicate } from '@/lib/ai/replicate';
import { Ratelimit } from '@upstash/ratelimit';
import { NextRequest, NextResponse } from 'next/server';

// Allow streaming responses up to 30 seconds
export const maxDuration = 30;

// Create Rate limit
const ratelimit = new Ratelimit({
    redis: kv,
    limiter: Ratelimit.fixedWindow(5, '30s'),
});

export async function POST(req: NextRequest) {
    // call ratelimit with request ip
    const { success, remaining } = await ratelimit.limit(req.ip ?? 'ip');

    // block the request if unsuccessfull
    if (!success) return new Response('Rate limit exceeded', { status: 429 });
    const { prompt } = await req.json();

    const output = await replicate.run(
        "black-forest-labs/flux-dev", // Model Name
        {
            input: {
                prompt
            }
        }
    );
    return NextResponse.json(output);
}

Model Selection

Replicate provides a wide range of AI models that you can use in your application. You can find the full list of models on the Replicate website.