replicate / replicate-go

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

Add RunWithOptions method that supports returning file output as `io.ReadCloser` #77

Closed mattt closed 6 days ago

mattt commented 6 days ago

This PR introduces a new WithFileOutput() option for a new RunWithOptions method in the Go client SDK. This option abstracts away file outputs from Replicate models, making it easier to work with file outputs and allowing for future optimizations in file asset delivery.

The new option can be used as follows:

output, err := client.RunWithOptions(ctx, "black-forest-labs/flux-schnell", input, nil, WithFileOutput())
reader, ok := output.(io.ReadCloser)
if ok {
    defer reader.Close()
    // Read from the reader
    data, err := io.ReadAll(reader)
    // Or stream the data
    _, err := io.Copy(destinationWriter, reader)
}
mattt commented 6 days ago

Nice! Looks great, I wonder if we should mirror the Python/JS clients and return an interface that extends ReadCloser with a URL field so we can make the URL available too.

Great idea! I just made that change.