Replicate
This page adapts the original AI SDK documentation: Replicate (https://github.com/vercel/ai/blob/main/content/providers/01-ai-sdk-providers/60-replicate.mdx).
Replicate is a platform for running open-source AI models. It is a popular choice for running image generation models.
Add the library via Swift Package Manager and include these products:
// Package.swift (excerpt)dependencies: [ .package(url: "https://github.com/teunlao/swift-ai-sdk", from: "1.0.0")],targets: [ .target( name: "YourTarget", dependencies: [ .product(name: "SwiftAISDK", package: "swift-ai-sdk"), .product(name: "ReplicateProvider", package: "swift-ai-sdk") ] )]Provider Instance
Section titled “Provider Instance”You can import the default provider instance replicate:
import ReplicateProvider
let model = replicate.image("black-forest-labs/flux-schnell")If you need a customized setup, create a provider instance with your settings:
import ReplicateProvider
let replicate = createReplicate(settings: ReplicateProviderSettings( apiToken: ProcessInfo.processInfo.environment["REPLICATE_API_TOKEN"], baseURL: "https://api.replicate.com/v1"))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
Authorizationheader. It defaults to theREPLICATE_API_TOKENenvironment variable. -
headers Record<string,string>
Custom headers to include in the requests.
-
fetch (input: RequestInfo, init?: RequestInit) => Promise<Response>
Custom fetch implementation (middleware) for testing or request interception.
Image Models
Section titled “Image Models”You can create Replicate image models using the .image() factory method.
For more on image generation with the AI SDK see generateImage.
Note: Model support for
sizeand other parameters varies by model. Check the model’s documentation on Replicate for supported options and additional parameters that can be passed viaproviderOptions.replicate.
Supported Image Models
Section titled “Supported Image Models”The following image models are currently supported by the Replicate provider:
- black-forest-labs/flux-1.1-pro-ultra
- black-forest-labs/flux-1.1-pro
- black-forest-labs/flux-dev
- black-forest-labs/flux-pro
- black-forest-labs/flux-schnell
- bytedance/sdxl-lightning-4step
- fofr/aura-flow
- fofr/latent-consistency-model
- fofr/realvisxl-v3-multi-controlnet-lora
- fofr/sdxl-emoji
- fofr/sdxl-multi-controlnet-lora
- ideogram-ai/ideogram-v2-turbo
- ideogram-ai/ideogram-v2
- lucataco/dreamshaper-xl-turbo
- lucataco/open-dalle-v1.1
- lucataco/realvisxl-v2.0
- lucataco/realvisxl2-lcm
- luma/photon-flash
- luma/photon
- nvidia/sana
- playgroundai/playground-v2.5-1024px-aesthetic
- recraft-ai/recraft-v3-svg
- recraft-ai/recraft-v3
- stability-ai/stable-diffusion-3.5-large-turbo
- stability-ai/stable-diffusion-3.5-large
- stability-ai/stable-diffusion-3.5-medium
- tstramer/material-diffusion
You can also use versioned models.
The id for versioned models is the Replicate model id followed by a colon and the version ID ($modelId:$versionId), e.g.
bytedance/sdxl-lightning-4step:5599ed30703defd1d160a25a63321b4dec97101d98b4674bcc56e41f62f35637.
Note: You can also pass any available Replicate model ID as a string if needed.
Basic Usage
Section titled “Basic Usage”import SwiftAISDKimport ReplicateProviderimport Foundation
let result = try await generateImage( model: replicate.image("black-forest-labs/flux-schnell"), prompt: "The Loch Ness Monster getting a manicure", aspectRatio: "16:9")
if case let .binary(images) = result.images, let first = images.first { try first.write(to: URL(fileURLWithPath: "image.webp"))}Model-specific options
Section titled “Model-specific options”let result = try await generateImage( model: replicate.image("recraft-ai/recraft-v3"), prompt: "The Loch Ness Monster getting a manicure", size: "1365x1024", providerOptions: [ "replicate": ["style": .string("realistic_image")] ])Versioned Models
Section titled “Versioned Models”let result = try await generateImage( model: replicate.image( "bytedance/sdxl-lightning-4step:5599ed30703defd1d160a25a63321b4dec97101d98b4674bcc56e41f62f35637" ), prompt: "The Loch Ness Monster getting a manicure")For more details, see the Replicate models page.