https://github.com/replicate/replicate-go
Go client for Replicate
https://github.com/replicate/replicate-go
Last synced: 3 months ago
JSON representation
Go client for Replicate
- Host: GitHub
- URL: https://github.com/replicate/replicate-go
- Owner: replicate
- License: apache-2.0
- Created: 2023-05-23T11:42:44.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-11T14:57:51.000Z (over 1 year ago)
- Last Synced: 2025-03-29T20:02:59.151Z (10 months ago)
- Language: Go
- Homepage: https://replicate.com
- Size: 208 KB
- Stars: 90
- Watchers: 20
- Forks: 14
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Replicate Go client
[](https://pkg.go.dev/github.com/replicate/replicate-go)
A Go client for [Replicate](https://replicate.com).
It lets you run models from your Go code,
and everything else you can do with
[Replicate's HTTP API](https://replicate.com/docs/reference/http).
## Requirements
- Go 1.20+
## Installation
Use `go get` to install the Replicate package:
```console
go get -u github.com/replicate/replicate-go
```
Include the Replicate package in your project:
```go
import "github.com/replicate/replicate-go"
```
## Usage
### Create a client
```go
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
```go
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.
```go
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.
```go
// 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:
```go
import (
"github.com/replicate/replicate-go"
)
isValid, err := replicate.ValidateWebhookRequest(req, secret)
```
To learn more, see the [webhooks guide](https://replicate.com/docs/webhooks).
## License
Replicate's Go client is released under the Apache 2.0 license.
See [LICENSE.txt](LICENSE.txt)