Skip to content

shonun1/replicate-go

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

import (
	"context"
	"os"

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

ctx := context.Background()

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

// https://replicate.com/stability-ai/stable-diffusion
version := "ac732df83cea7fff18b8472768c88ad041fa750ff7682a21affe81863cbe77e4"

input := replicate.PredictionInput{
	"prompt": "an astronaut riding a horse on mars, hd, dramatic lighting",
}

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

// Run a model and wait for its output
output, err := r8.Run(ctx, version, input, &webhook)
if err != nil {
	// handle error
}
fmt.Println("output: ", output)

// Create a prediction
prediction, err := r8.CreatePrediction(ctx, version, input, &webhook, false)
if err != nil {
	// handle error
}

// Wait for the prediction to finish
err = r8.Wait(ctx, prediction)
if err != nil {
	// handle error
}
fmt.Println("output: ", output)


// 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, err := r8.CreatePrediction(ctx, version, input, &webhook, false)
if err != nil {
	// handle error
}

// Wait for the prediction to finish
err = r8.Wait(ctx, prediction)
if err != nil {
	// handle error
}
fmt.Println("output: ", prediction.Output)

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%