any-request.ts 635 B

123456789101112131415161718192021222324252627
  1. import ollama from 'ollama'
  2. // Set a timeout to abort the request after 1 second
  3. setTimeout(() => {
  4. console.log('\nAborting request...\n')
  5. ollama.abort()
  6. }, 1000) // 1000 milliseconds = 1 second
  7. ollama.generate({
  8. model: 'llama3.1',
  9. prompt: 'Write a long story',
  10. stream: true,
  11. }).then(
  12. async (stream) => {
  13. for await (const chunk of stream) {
  14. process.stdout.write(chunk.response)
  15. }
  16. }
  17. ).catch(
  18. (error) => {
  19. if (error.name === 'AbortError') {
  20. console.log('The request has been aborted')
  21. } else {
  22. console.error('An error occurred:', error)
  23. }
  24. }
  25. )