google-gemini / generative-ai-js

The official Node.js / Typescript library for the Google Gemini API
https://www.npmjs.com/package/@google/generative-ai
Apache License 2.0
627 stars 126 forks source link

Feature Request: signal option #48

Closed tmkx closed 1 month ago

tmkx commented 7 months ago

can we add a new signal option? it will be more flexible and can be used to abort the stream when a user cancels chat sessions. it meets the timeout requirement as well, timeout is also very useful as it's more convenient BTW :)

const model = genAI.getGenerativeModel({ model: "gemini-pro" }, {
  signal: AbortSignal.timeout(10000),
});

https://github.com/google/generative-ai-js/blob/d8f22d2259c9e926dcc4ac955db896c235e0a3bc/packages/main/src/requests/request.ts#L103-L117

Originally posted by @tmkx in https://github.com/google/generative-ai-js/pull/31#issuecomment-1920556402

WillianAgostini commented 7 months ago

Just add this code below and the property signal in the requestOptions interface:

if (requestOptions?.signal) {
  fetchOptions.signal = requestOptions?.signal;
}
hsubox76 commented 1 month ago

Was released in https://github.com/google-gemini/generative-ai-js/releases/tag/%40google%2Fgenerative-ai%400.16.0

See PR https://github.com/google-gemini/generative-ai-js/pull/144 for details.