replicate-go icon indicating copy to clipboard operation
replicate-go copied to clipboard

Implement create prediction / training methods that take functional option arguments

Open mattt opened this issue 1 year ago • 0 comments

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) {}
  • It's too long. It takes too many arguments.
  • It's inflexible. It can't accommodate future changes to requirements for creating a prediction. For example, creating predictions for a model without a version or for a deployment.
  • Its last arguments are vague. There's no clue for what nil for webhook and false for stream mean at the call site.

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...)

mattt avatar Mar 06 '24 13:03 mattt