-
Notifications
You must be signed in to change notification settings - Fork 209
/
abort-all-requests.ts
55 lines (51 loc) · 1.37 KB
/
abort-all-requests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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)
}
})