replicate / replicate-go

Go client for Replicate
https://replicate.com
Apache License 2.0
65 stars 9 forks source link

Implement create prediction / training methods that take functional option arguments #50

Open mattt opened 6 months ago

mattt commented 6 months ago

The method signature for CreatePrediction is less than ideal:

func (r *Client) CreatePrediction(ctx context.Context, version string, input PredictionInput, webhook *Webhook, stream bool) (*Prediction, error) {}

This PR explores a new approach that uses functional arguments. For backwards compatibility, it adds new CreatePredictionWithOptions and CreateTrainingWithOptions methods, and ports the existing create methods to wrap them. In a future release, we could deprecate these older create methods and rename CreatePredictionWithOptions to CreatePrediction.

import (
  context

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

ctx := context.TODO()
input := replicate.PredictionInput{"prompt": "A fresh coat of paint"}
webhook := &replicate.Webhook{
    URL:    "https://example.com/webhook",
    Events: []replicate.WebhookEventType{"start", "completed"},
}
opts := []replicate.CreatePredictionOption{
    replicate.WithModel("owner", "model"),
    replicate.WithInput(input),
    replicate.WithWebhook(webhook),
    replicate.WithStream(true),
}
prediction, err := client.CreatePredictionWithOptions(ctx, opts...)