Skip to content

chenhhA/replicate-go

This branch is 1 commit ahead of replicate/replicate-go:main.

Folders and files

NameName
Last commit message
Last commit date
Jun 10, 2024
Jun 10, 2024
Oct 11, 2024
Oct 11, 2024
Jun 10, 2024
Jul 17, 2023
Aug 18, 2023
Jun 10, 2024
Mar 6, 2024
Jun 6, 2023
Oct 2, 2024
Oct 3, 2024
Mar 15, 2024
Mar 6, 2024
Jun 20, 2024
Nov 10, 2024
Mar 15, 2024
Sep 23, 2024
Mar 12, 2024
Aug 22, 2024
Jul 25, 2024
Mar 15, 2024
Oct 11, 2024
Oct 11, 2024
Mar 15, 2024
Dec 4, 2023
Oct 11, 2024
Sep 23, 2024
Mar 15, 2024
Nov 10, 2024
Oct 3, 2024
Jul 17, 2023
Oct 11, 2024
Oct 11, 2024
Mar 11, 2024
Mar 6, 2024
May 24, 2024

Repository files navigation

Replicate Go client

Go Reference

A Go client for Replicate. It lets you run models from your Go code, and everything else you can do with Replicate's HTTP API.

Requirements

  • Go 1.20+

Installation

Use go get to install the Replicate package:

go get -u github.com/replicate/replicate-go

Include the Replicate package in your project:

import "github.com/replicate/replicate-go"

Usage

Create a client

import (
	"context"
	"os"

	"github.com/replicate/replicate-go"
)

ctx := context.TODO()

// You can also provide a token directly with 
// `replicate.NewClient(replicate.WithToken("r8_..."))`
r8, err := replicate.NewClient(replicate.WithTokenFromEnv())
if err != nil {
	// handle error
}

Run a model

model := "stability-ai/sdxl"
version := "7762fd07cf82c948538e41f63f77d685e02b063e37e496e96eefd46c929f9bdc"

input := replicate.PredictionInput{
	"prompt": "An astronaut riding a rainbow unicorn",
}

webhook := replicate.Webhook{
	URL:    "https://example.com/webhook",
	Events: []replicate.WebhookEventType{"start", "completed"},
}

// Run a model by version and wait for its output
output, _ := r8.Run(ctx, fmt.Sprintf("%s:%s", model, version), input, &webhook)

// Run a model and wait for its output
output, _ := r8.Run(ctx, model, input, &webhook)

The Run method is a convenience method that creates a prediction, waits for it to finish, and returns the output. If you want a reference to the prediction, you can call CreatePrediction, call Wait on the prediction, and access its Output field.

prediction, _ := r8.CreatePrediction(ctx, version, input, &webhook, false)
_ = r8.Wait(ctx, prediction) // Wait for the prediction to finish

Some models take file inputs. Use the CreateFileFromPath, CreateFileFromBytes, or CreateFileFromBuffer method to upload a file and pass it as a prediction input.

// https://replicate.com/vaibhavs10/incredibly-fast-whisper
version := "3ab86df6c8f54c11309d4d1f930ac292bad43ace52d10c80d87eb258b3c9f79c"

file, _ := r8.CreateFileFromPath(ctx, "path/to/audio.mp3", nil)

input := replicate.PredictionInput{
	"audio": file,
}
prediction, _ := r8.CreatePrediction(ctx, version, input, nil, false)

Webhooks

To prevent unauthorized requests, Replicate signs every webhook and its metadata with a unique key for each user or organization. You can use this signature to verify the webhook indeed comes from Replicate before you process it.

This client includes a ValidateWebhookRequest convenience function that you can use to validate webhooks:

import (
	"github.com/replicate/replicate-go"
)

isValid, err := replicate.ValidateWebhookRequest(req, secret)

To learn more, see the webhooks guide.

License

Replicate's Go client is released under the Apache 2.0 license. See LICENSE.txt

About

Go client for Replicate

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%