-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clarified aborting examples and readme (#157)
Clarified aborting examples and updated readme
- Loading branch information
1 parent
6b6221d
commit c97f231
Showing
5 changed files
with
109 additions
and
60 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,55 @@ | ||
import ollama from 'ollama' | ||
|
||
// Set a timeout to abort all requests after 5 seconds | ||
setTimeout(() => { | ||
console.log('\nAborting all requests...\n') | ||
ollama.abort() | ||
}, 5000) // 5000 milliseconds = 5 seconds | ||
|
||
// Start multiple concurrent streaming requests | ||
Promise.all([ | ||
ollama.generate({ | ||
model: 'llama3.2', | ||
prompt: 'Write a long story about dragons', | ||
stream: true, | ||
}).then( | ||
async (stream) => { | ||
console.log(' Starting stream for dragons story...') | ||
for await (const chunk of stream) { | ||
process.stdout.write(' 1> ' + chunk.response) | ||
} | ||
} | ||
), | ||
|
||
ollama.generate({ | ||
model: 'llama3.2', | ||
prompt: 'Write a long story about wizards', | ||
stream: true, | ||
}).then( | ||
async (stream) => { | ||
console.log(' Starting stream for wizards story...') | ||
for await (const chunk of stream) { | ||
process.stdout.write(' 2> ' + chunk.response) | ||
} | ||
} | ||
), | ||
|
||
ollama.generate({ | ||
model: 'llama3.2', | ||
prompt: 'Write a long story about knights', | ||
stream: true, | ||
}).then( | ||
async (stream) => { | ||
console.log(' Starting stream for knights story...') | ||
for await (const chunk of stream) { | ||
process.stdout.write(' 3>' + chunk.response) | ||
} | ||
} | ||
) | ||
]).catch(error => { | ||
if (error.name === 'AbortError') { | ||
console.log('All requests have 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Ollama } from 'ollama' | ||
|
||
// Create multiple ollama clients | ||
const client1 = new Ollama() | ||
const client2 = new Ollama() | ||
|
||
// Set a timeout to abort just the first request after 5 seconds | ||
setTimeout(() => { | ||
console.log('\nAborting dragons story...\n') | ||
// abort the first client | ||
client1.abort() | ||
}, 5000) // 5000 milliseconds = 5 seconds | ||
|
||
// Start multiple concurrent streaming requests with different clients | ||
Promise.all([ | ||
client1.generate({ | ||
model: 'llama3.2', | ||
prompt: 'Write a long story about dragons', | ||
stream: true, | ||
}).then( | ||
async (stream) => { | ||
console.log(' Starting stream for dragons story...') | ||
for await (const chunk of stream) { | ||
process.stdout.write(' 1> ' + chunk.response) | ||
} | ||
} | ||
), | ||
|
||
client2.generate({ | ||
model: 'llama3.2', | ||
prompt: 'Write a short story about wizards', | ||
stream: true, | ||
}).then( | ||
async (stream) => { | ||
console.log(' Starting stream for wizards story...') | ||
for await (const chunk of stream) { | ||
process.stdout.write(' 2> ' + chunk.response) | ||
} | ||
} | ||
), | ||
|
||
]).catch(error => { | ||
if (error.name === 'AbortError') { | ||
console.log('Dragons story request has been aborted') | ||
} else { | ||
console.error('An error occurred:', error) | ||
} | ||
}) | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.