Skip to content

Commit

Permalink
Fetch again if the prompt has changed between the request and the res…
Browse files Browse the repository at this point in the history
…ponse
  • Loading branch information
brichet committed Dec 2, 2024
1 parent 1304f40 commit 3e7142a
Showing 1 changed file with 4 additions and 34 deletions.
38 changes: 4 additions & 34 deletions src/llm-models/codestral-completer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ export class CodestralCompleter implements IBaseCompleter {
this._mistralProvider = new MistralAI({ ...options.settings });
this._throttler = new Throttler(
async (data: CompletionRequest) => {
this._invokedData = data;
let fetchAgain = false;
const invokedData = data;

// Request completion.
const request = this._mistralProvider.completionWithRetry(
Expand All @@ -38,8 +37,9 @@ export class CodestralCompleter implements IBaseCompleter {
return setTimeout(() => resolve(null), REQUEST_TIMEOUT);
});

// Fetch again if the request is too long or if the prompt has changed.
const response = await Promise.race([request, timeoutPromise]);
if (response === null) {
if (response === null || invokedData.prompt !== this._currentData?.prompt) {
return {
items: [],
fetchAgain: true
Expand All @@ -51,37 +51,8 @@ export class CodestralCompleter implements IBaseCompleter {
return { insertText: choice.message.content as string };
});

// Check if the prompt has changed during the request.
if (this._invokedData.prompt !== this._currentData?.prompt) {
// The current prompt does not include the invoked one, the result is
// cancelled and a new completion will be requested.
if (!this._currentData?.prompt.startsWith(this._invokedData.prompt)) {
fetchAgain = true;
items = [];
} else {
// Check if some results contain the current prompt, and return them if so,
// otherwise request completion again.
const newItems: { insertText: string }[] = [];
items.forEach(item => {
const result = this._invokedData!.prompt + item.insertText;
if (result.startsWith(this._currentData!.prompt)) {
const insertText = result.slice(
this._currentData!.prompt.length
);
newItems.push({ insertText });
}
});
if (newItems.length) {
items = newItems;
} else {
fetchAgain = true;
items = [];
}
}
}
return {
items,
fetchAgain
items
};
},
{ limit: INTERVAL }
Expand Down Expand Up @@ -135,6 +106,5 @@ export class CodestralCompleter implements IBaseCompleter {
private _requestCompletion?: () => void;
private _throttler: Throttler;
private _mistralProvider: MistralAI;
private _invokedData: CompletionRequest | null = null;
private _currentData: CompletionRequest | null = null;
}

0 comments on commit 3e7142a

Please sign in to comment.