replicate / replicate-javascript

Node.js client for Replicate
https://replicate.com/docs/get-started/nodejs
Apache License 2.0
473 stars 193 forks source link

Support process.env.REPLICATE_API_TOKEN for implicit auth? #121

Closed zeke closed 12 months ago

zeke commented 1 year ago

To use the client today, you have to do this:

const replicate = new Replicate({
  auth: process.env.REPLICATE_API_TOKEN,
});

What if you could just do:

const replicate = new Replicate()

..and have it default to process.env.REPLICATE_API_TOKEN, if present?

This is similar to how the Python client works, and I kinda like it. Also makes code examples a lot cleaner.

mattt commented 1 year ago

I agree that the latter makes for better-looking sample code. But the implicit auth behavior in the Python client is among the most frequent points of confusion / friction from users. I think the Python koan of "Explicit is better than implicit" applies.

Not to say that we can't do better here!

What do you think about something like what we did in the Go client? Do you know of any good examples / conventions for JS client constructors that read from env like that?

zeke commented 1 year ago

the implicit auth behavior in the Python client is among the most frequent points of confusion / friction from users.

What makes it a point of friction? Is it because the client blows up weirdly if the token is missing? Or because you're forced to do auth that way?

I think what's different about what I'm proposing here is that you have the option: set the auth option explicitly, or implicitly fall back to the env var.

What do you think about something like what we did in the Go client?

client := replicate.NewClient(replicate.WithTokenFromEnv())

That could work, but might feel a bit unfamiliar. I can't recall ever seeing a pattern like that in JS land for loading from env.

mattt commented 1 year ago

@zeke You know, I'm starting to come around to this. Opened https://github.com/replicate/replicate-javascript/pull/127 with the change.