Categorygithub.com/replicate/replicate-go
modulepackage
0.26.0
Repository: https://github.com/replicate/replicate-go.git
Documentation: pkg.go.dev

# README

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

# Packages

No description provided by the author

# Functions

NewClient creates a new Replicate API client.
Paginate takes a Page and the Client request method, and iterates through pages of results.
No description provided by the author
ValidateWebhookRequest validates the signature from an incoming webhook request using the provided secret.
WithBaseURL sets the base URL for the client.
WithBlockUntilDone configures the run to block until the prediction is done.
WithFileOutput configures the run to automatically convert URLs in output to FileOutput objects.
WithHTTPClient sets the HTTP client used by the client.
WithPollingInterval sets the interval between attempts.
WithRetryPolicy sets the retry policy used by the client.
WithToken sets the auth token used by the client.
WithTokenFromEnv configures the client to use the auth token provided in the REPLICATE_API_TOKEN environment variable.
WithUserAgent sets the User-Agent header on requests made by the client.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
SSETypeDefault is the default type of SSEEvent.
SSETypeDone is the type of SSEEvent that indicates the prediction is done.
SSETypeError is the type of SSEEvent that indicates an error occurred during the prediction.
SSETypeLogs is the type of SSEEvent that contains logs from the prediction.
SSETypeOutput is the type of SSEEvent that contains output from the prediction.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
APIError represents an error returned by the Replicate API.
Client is a client for the Replicate API.
No description provided by the author
ConstantBackoff is a backoff strategy that returns a constant delay with some jitter.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ExponentialBackoff is a backoff strategy that returns an exponentially increasing delay with some jitter.
No description provided by the author
FileOutput is a custom type that implements io.ReadCloser and includes a URL field.
No description provided by the author
Identifier represents a reference to a Replicate model with an optional version.
No description provided by the author
ModelError represents an error returned by a model for a failed prediction.
No description provided by the author
Page represents a paginated response from Replicate's API.
No description provided by the author
No description provided by the author
No description provided by the author
SSEEvent represents a Server-Sent Event.
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

Backoff is an interface for backoff strategies.
No description provided by the author

# Type aliases

ClientOption is a function that modifies an options struct.
No description provided by the author
RunOption is a function that modifies RunOptions.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
WaitOption is a function that modifies an options struct.
No description provided by the author