generative-ai-js icon indicating copy to clipboard operation
generative-ai-js copied to clipboard

feat: add request signal configuration

Open WillianAgostini opened this issue 1 year ago • 6 comments

Fixes #48

Adding new signal options

  • [x] Tests pass
  • [x] Appropriate changes to documentation are included in the PR

WillianAgostini avatar Feb 02 '24 18:02 WillianAgostini

🦋 Changeset detected

Latest commit: 904795ee86436348cccf34d9ca8c761500135771

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@google/generative-ai Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Feb 02 '24 18:02 changeset-bot[bot]

Could you please take a look at this PR, @hsubox76? Thanks in advance!

gaodeng avatar Mar 17 '24 00:03 gaodeng

Sorry I haven't gotten back to this. One question: what's the expected behavior if both timeout and signal are specified? It seems like it might be unexpected behavior as is. We should probably log a warning?

Also heads-up, there is a plan to change timeout to timeoutMillis or timeoutMilliseconds so we might end up doing that all in one PR.

hsubox76 avatar Mar 29 '24 20:03 hsubox76

There is a real-world library(axios) that supports both options simultaneously:

import axios from 'axios';

axios
  .get('https://httpbin.org/delay/2', {
    // catch AxiosError: timeout of 1000ms exceeded
    timeout: 1000,
    // catch CanceledError: canceled
    signal: AbortSignal.timeout(1000),
  })
  .then((response) => console.log('then', response.data))
  .catch((reason) => console.error('catch', reason));

it behaves like Promise.race

tmkx avatar Mar 30 '24 03:03 tmkx

I thought that was actually going to be pretty complex but it looks like we can just collect all the signals the user wants to use (up to 2 in this case) and wrap them with AbortSignal.any(): https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal#aborting_a_fetch_with_timeout_or_explicit_abort

hsubox76 avatar Apr 01 '24 20:04 hsubox76

An additional wrinkle is that we don't want an abort signal to be set at the model level, you want to set it on a per-request level, otherwise it cancels all requests. We're working on another PR to do this.

hsubox76 avatar May 02 '24 18:05 hsubox76

Closing this PR - but we're working on another way of implementing this funcitonality.

hsubox76 avatar Jun 18 '24 17:06 hsubox76