Skip to main content

Replicate Provider

Replicate provides a marketplace to run any open-source model through a simple API. Perfect for experimentation and accessing diverse models including image generation, video, and specialized models.

Setup​

Installation​

import (
"github.com/digitallysavvy/go-ai/pkg/ai"
"github.com/digitallysavvy/go-ai/pkg/providers/replicate"
)

Configuration​

provider := replicate.New(replicate.Config{
APIKey: os.Getenv("REPLICATE_API_KEY"),
})

model, err := provider.LanguageModel("meta/meta-llama-3-70b-instruct")

Get API Key​

export REPLICATE_API_KEY=r8_...

Available Models​

Language Models​

Model IDContextPriceBest For
meta/meta-llama-3-70b-instruct8K$0.65/1MGeneral purpose
mistralai/mixtral-8x7b-instruct-v0.132K$0.30/1MBalanced

Image Generation​

Model IDQualityPriceBest For
stability-ai/sdxlHigh$0.0119/runImages
black-forest-labs/flux-proExcellent$0.055/runProfessional
black-forest-labs/flux-schnellGood$0.003/runFast

Video Generation​

Model IDQualityPriceBest For
stability-ai/stable-video-diffusionMedium$0.10/runVideo from image

provider.VideoModel(modelID) accepts both forms Replicate uses:

  • Unversioned — "owner/model" (no :version). Requests go to POST /models/{owner}/{model}/predictions, which always runs the model's latest version. This is now supported; previously only versioned IDs worked.
  • Versioned — "owner/model:version-hash". Requests go to POST /predictions with version in the body, pinning an exact version.
videoModel, err := provider.VideoModel("stability-ai/stable-video-diffusion")
if err != nil {
log.Fatal(err)
}

result, err := ai.GenerateVideo(ctx, ai.GenerateVideoOptions{
Model: videoModel,
Prompt: ai.VideoPrompt{Text: "A boat sailing across a calm lake"},
})

VideoModel also implements provider.VideoModelStarter / VideoModelStatusChecker for the async ai.ExperimentalStartVideo / ai.ExperimentalGetVideoStatus calls.

Examples​

Basic Text Generation​

package main

import (
"context"
"fmt"
"log"
"os"

"github.com/digitallysavvy/go-ai/pkg/ai"
"github.com/digitallysavvy/go-ai/pkg/providers/replicate"
)

func main() {
ctx := context.Background()
provider := replicate.New(replicate.Config{
APIKey: os.Getenv("REPLICATE_API_KEY"),
})

model, err := provider.LanguageModel("meta/meta-llama-3-70b-instruct")
if err != nil {
log.Fatal(err)
}

result, err := ai.GenerateText(ctx, ai.GenerateTextOptions{Model: model, Prompt: "Explain Replicate"})
if err != nil {
log.Fatal(err)
}
fmt.Println(result.Text)
}

Image Generation​

imageModel, err := provider.ImageModel("black-forest-labs/flux-pro")
if err != nil {
log.Fatal(err)
}

result, err := ai.GenerateImage(ctx, ai.GenerateImageOptions{
Model: imageModel,
Prompt: "A cyberpunk city at night",
Size: "1024x1024",
})

fmt.Printf("Image bytes: %d\n", len(result.Images[0].Data))

Best Practices​

  1. Model Discovery

    • Browse Replicate marketplace for models
    • Try different models easily
    • Great for prototyping
  2. Cost Management

    • Pay-per-run pricing
    • Monitor usage carefully
    • Cache results when possible
  3. Use Cases

    • Experimentation and testing
    • Specialized model access
    • Image/video generation

Workflow Serialization​

Replicate image models can cross a workflow boundary with providerutils.SerializeModel / DeserializeModel. See Provider Serialization for the mechanism; video models are not yet serializable.

See Also​