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

Improve the ergonomics of awaiting predictions

Open zeke opened this issue 4 months ago • 7 comments

The replicate.run method provides a nice way of running a prediction and getting its output with a single line of code:

const output = await replicate.run(identifier, options, progress);

That's nice if I only want the prediction output. But what if I want the whole prediction object? For example, maybe I want to know the prediction id, or see how long the prediction took to run. In this case, the best option is this:

let prediction = await replicate.predictions.create({ version, input })
prediction = await replicate.wait(prediction);

That works, but it's not ideal for a few reasons:

  • It's more code. No longer a glamorous one-liner.
  • The fact that replicate.prediction.create is itself an async function makes me think that maybe I'm awaiting its completion too, when in fact I'm actually just awaiting the empty shell of a prediction.
  • The wait call is on the replicate object, rather than on the prediction instance itself. This is confusing.
  • I can't make the prediction a const because I have to re-assign it when I call wait. It feels weird to create a thing and then overwrite it entirely, especially in the age of JavaScript constants.
  • I would probably always have to look at the docs to get this right.

How can we make this better? My off-the-cuff idea is a wait option on the replicate.predictions.create method:

const prediction = await replicate.predictions.create({ version, input, wait: true })

What do folks think of that? Open to ideas here.

cc @replicate/product @replicate/hackers

zeke avatar Feb 16 '24 00:02 zeke