Skip to content

Commit

Permalink
Add a timeout to avoid endless requests
Browse files Browse the repository at this point in the history
  • Loading branch information
brichet committed Nov 8, 2024
1 parent 2d30689 commit 23932d2
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/llm-models/codestral-completer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import { CompletionRequest } from '@mistralai/mistralai';

import { BaseCompleter, IBaseCompleter } from './base-completer';

/*
/**
* The Mistral API has a rate limit of 1 request per second
*/
const INTERVAL = 1000;

/**
* Timeout to avoid endless requests
*/
const REQUEST_TIMEOUT = 3000;

export class CodestralCompleter implements IBaseCompleter {
constructor(options: BaseCompleter.IOptions) {
// this._requestCompletion = options.requestCompletion;
Expand All @@ -24,11 +29,22 @@ export class CodestralCompleter implements IBaseCompleter {
let fetchAgain = false;

// Request completion.
const response = await this._mistralProvider.completionWithRetry(
const request = this._mistralProvider.completionWithRetry(
data,
{},
false
);
const timeoutPromise = new Promise<null>(resolve => {
return setTimeout(() => resolve(null), REQUEST_TIMEOUT);
});

const response = await Promise.race([request, timeoutPromise]);
if (response === null) {
return {
items: [],
fetchAgain: true
};
}

// Extract results of completion request.
let items = response.choices.map((choice: any) => {
Expand Down

0 comments on commit 23932d2

Please sign in to comment.