-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow aborting individual requests (#97)
- Loading branch information
1 parent
e575b2b
commit e32a64f
Showing
4 changed files
with
137 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import ollama from 'ollama' | ||
import { AbortableAsyncIterator } from '../../src/utils' | ||
|
||
let stream: AbortableAsyncIterator<object> | ||
|
||
// Set a timeout to abort the request after 1 second | ||
setTimeout(() => { | ||
console.log('\nAborting request...\n') | ||
stream.abort() | ||
}, 1000) // 1000 milliseconds = 1 second | ||
|
||
try { | ||
ollama.generate({ | ||
model: 'llama2', | ||
prompt: 'Write a long story', | ||
stream: true, | ||
}).then( | ||
async (_stream) => { | ||
stream = _stream | ||
for await (const chunk of stream) { | ||
process.stdout.write(chunk.response) | ||
} | ||
} | ||
) | ||
} catch (error) { | ||
if (error.name === 'AbortError') { | ||
console.log('The request has been aborted') | ||
} else { | ||
console.error('An error occurred:', error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters