Skip to content
Open
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
12 changes: 12 additions & 0 deletions src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,8 @@ export class MLCEngine implements MLCEngineInterface {
async chatCompletion(
request: ChatCompletionRequest,
): Promise<AsyncIterable<ChatCompletionChunk> | ChatCompletion> {
const initialInterruptSignal = this.interruptSignal;

// 0. Check model loaded and preprocess inputs
const [selectedModelId, selectedPipeline, selectedChatConfig] =
this.getLLMStates("ChatCompletionRequest", request.model);
Expand All @@ -771,6 +773,10 @@ export class MLCEngine implements MLCEngineInterface {
// 0.5 Block wait until this pipeline finishes all previous requests
const lock = this.loadedModelIdToLock.get(selectedModelId)!;
await lock.acquire();
// If interruptGenerate called during the wait for lock, respect that. But reset if locked from a previous call to interruptGenerate
if (initialInterruptSignal && this.interruptSignal) {
this.interruptSignal = false;
}

// 1. If request is streaming, return an AsyncIterable (an iterable version of `_generate()`)
if (request.stream) {
Expand Down Expand Up @@ -901,6 +907,8 @@ export class MLCEngine implements MLCEngineInterface {
async completion(
request: CompletionCreateParams,
): Promise<AsyncIterable<Completion> | Completion> {
const initialInterruptSignal = this.interruptSignal;

// 0. Check model loaded and preprocess inputs
const [selectedModelId, selectedPipeline, selectedChatConfig] =
this.getLLMStates("CompletionCreateParams", request.model);
Expand All @@ -920,6 +928,10 @@ export class MLCEngine implements MLCEngineInterface {
// 0.5 Block wait until this pipeline finishes all previous requests
const lock = this.loadedModelIdToLock.get(selectedModelId)!;
await lock.acquire();
// If interruptGenerate called during the wait for lock, respect that. But reset if locked from a previous call to interruptGenerate
if (initialInterruptSignal && this.interruptSignal) {
this.interruptSignal = false;
}

// 1. If request is streaming, return an AsyncIterable (an iterable version of `_generate()`)
if (request.stream) {
Expand Down
Loading