-
Notifications
You must be signed in to change notification settings - Fork 362
Add custom abort controller for requests #146
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) | ||
| } | ||
| } | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| }) | ||
|
|
||
|
|
@@ -83,7 +83,7 @@ export class Ollama { | |
|
|
||
| const itr = parseJSON<T | ErrorResponse>(response.body) | ||
| const abortableAsyncIterator = new AbortableAsyncIterator( | ||
| abortController, | ||
| request.abortController ?? abortController, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If taking an |
||
| itr, | ||
| () => { | ||
| const i = this.ongoingStreamedRequests.indexOf(abortableAsyncIterator) | ||
|
|
@@ -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() | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,7 +44,11 @@ export interface Options { | |
| stop: string[] | ||
| } | ||
|
|
||
| export interface GenerateRequest { | ||
| interface AbortableRequest { | ||
| abortController?: AbortController | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For what I've seen, consummers are generally taking an |
||
| } | ||
|
|
||
| export interface GenerateRequest extends AbortableRequest{ | ||
| model: string | ||
| prompt: string | ||
| suffix?: string | ||
|
|
@@ -95,7 +99,7 @@ export interface Tool { | |
| }; | ||
| } | ||
|
|
||
| export interface ChatRequest { | ||
| export interface ChatRequest extends AbortableRequest { | ||
| model: string | ||
| messages?: Message[] | ||
| stream?: boolean | ||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe not needed