ollama-js icon indicating copy to clipboard operation
ollama-js copied to clipboard

Is there any way to stop the response using this lib?

Open ukicomputers opened this issue 1 year ago • 2 comments

Hi! Thanks for great library! Is there any way to stop the response using this lib? We are low on resources, so we want when user clicks stop button on our frontend (or closes the page) to stop generating response. We've tried this, but it didn't end the response flow:

try {
    for await (const part of response) {
      if (clientDisconnected) {
        break; // Stop sending data if the client disconnects
      }
      res.write(`${JSON.stringify(part)}`);
    }
  } catch (err) {
    console.error('Error while streaming response:', err);
  } finally {
    console.log("Response ended");
    res.end();
  }

Thanks in advance!

ukicomputers avatar Aug 09 '24 08:08 ukicomputers

Yes, you can either abort all ongoing request (see any request example) or you can abort a specific request (see specific request example)

hopperelec avatar Aug 12 '24 07:08 hopperelec

Hi! Is it also possible to abort a non-streamable chat/generate request?

Will the request be actually aborted using the code snippet below?

const olm = new Ollama();

setTimeout(() => {
  olm.abort()
}, 100) // asume that in 100 ms the user will trigger a new request =>the prior request is not of interest anymore

olm.chat({ // or olm.generate
    model: 'llama3.2',
    prompt: 'Write me a long long story',
    stream: false,
  }).then(
    async (resp) => {
      // do smth with the response
    }
  )
)

Thanks in advance!

gabrielBusuioc avatar Feb 28 '25 09:02 gabrielBusuioc