Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom abort controller for requests #146

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ollama from 'ollama'
setTimeout(() => {
console.log('\nAborting request...\n')
ollama.abort()
}, 1000) // 1000 milliseconds = 1 second
}, 500) // 1000 milliseconds = 1 second

ollama.generate({
model: 'llama3.1',
Expand Down
31 changes: 31 additions & 0 deletions examples/abort/custom-abort-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import ollama from 'ollama'

const abortController = new AbortController()

// Set a timeout to abort the request after 100 milliseconds before the stream starts
setTimeout(() => {
console.log('\nAborting request...\n')
abortController.abort()
}, 100) // 100 milliseconds


ollama.generate({
model: 'llama3.1',
prompt: 'Write a long story',
stream: true, // this could be a non-streamable request
abortController
}).then(
async (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)
}
}
)
7 changes: 4 additions & 3 deletions src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class Ollama {
if (request.stream) {
const abortController = new AbortController()
const response = await post(this.fetch, host, request, {
signal: abortController.signal,
signal: request.abortController?.signal ?? abortController.signal,
headers: this.config.headers
})

Expand All @@ -83,7 +83,7 @@ export class Ollama {

const itr = parseJSON<T | ErrorResponse>(response.body)
const abortableAsyncIterator = new AbortableAsyncIterator(
abortController,
request.abortController ?? abortController,
itr,
() => {
const i = this.ongoingStreamedRequests.indexOf(abortableAsyncIterator)
Expand All @@ -96,7 +96,8 @@ export class Ollama {
return abortableAsyncIterator
}
const response = await utils.post(this.fetch, host, request, {
headers: this.config.headers
headers: this.config.headers,
signal: request.abortController?.signal,
})
return await response.json()
}
Expand Down
24 changes: 14 additions & 10 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ export interface Options {
stop: string[]
}

export interface GenerateRequest {
interface AbortableRequest {
abortController?: AbortController
}

export interface GenerateRequest extends AbortableRequest{
model: string
prompt: string
suffix?: string
Expand Down Expand Up @@ -95,7 +99,7 @@ export interface Tool {
};
}

export interface ChatRequest {
export interface ChatRequest extends AbortableRequest {
model: string
messages?: Message[]
stream?: boolean
Expand All @@ -106,43 +110,43 @@ export interface ChatRequest {
options?: Partial<Options>
}

export interface PullRequest {
export interface PullRequest extends AbortableRequest {
model: string
insecure?: boolean
stream?: boolean
}

export interface PushRequest {
export interface PushRequest extends AbortableRequest {
model: string
insecure?: boolean
stream?: boolean
}

export interface CreateRequest {
export interface CreateRequest extends AbortableRequest {
model: string
path?: string
modelfile?: string
quantize?: string
stream?: boolean
}

export interface DeleteRequest {
export interface DeleteRequest extends AbortableRequest {
model: string
}

export interface CopyRequest {
export interface CopyRequest extends AbortableRequest {
source: string
destination: string
}

export interface ShowRequest {
export interface ShowRequest extends AbortableRequest {
model: string
system?: string
template?: string
options?: Partial<Options>
}

export interface EmbedRequest {
export interface EmbedRequest extends AbortableRequest {
model: string
input: string | string[]
truncate?: boolean
Expand All @@ -151,7 +155,7 @@ export interface EmbedRequest {
options?: Partial<Options>
}

export interface EmbeddingsRequest {
export interface EmbeddingsRequest extends AbortableRequest {
model: string
prompt: string
keep_alive?: string | number
Expand Down