Skip to content

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")
]
)
]

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 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 (middleware) for testing or request interception.

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 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.

The following image models are currently supported by the Replicate provider:

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.

import SwiftAISDK
import ReplicateProvider
import 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"))
}
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")]
]
)
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.