diff --git a/agenta-web/src/components/EvaluationTable/ABTestingEvaluationTable.tsx b/agenta-web/src/components/EvaluationTable/ABTestingEvaluationTable.tsx index adc4e7ad37..fd357b6485 100644 --- a/agenta-web/src/components/EvaluationTable/ABTestingEvaluationTable.tsx +++ b/agenta-web/src/components/EvaluationTable/ABTestingEvaluationTable.tsx @@ -211,14 +211,72 @@ const ABTestingEvaluationTable: React.FC = ({ .catch(console.error) } + const runEvaluationWithRetry = async ( + id: string, + count: number = 1, + showNotification: boolean = true, + maxRetryCount: number, + retryDelay: number + ) => { + let retryCount = 0 + while (retryCount <= maxRetryCount) { + try { + await runEvaluation(id, count, showNotification) + // If the evaluation is successful, break out of the retry loop + break; + } catch (error) { + console.error(`Error in evaluation for ID ${id}, retrying...`) + retryCount++ + if (retryCount <= maxRetryCount) { + // Add a delay before retrying + await delay(retryDelay) + } else { + console.error(`Max retry count reached for evaluation ID ${id}.`) + // Handle the failure after max retries (if needed) + } + } + } + } + + const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms)) + const runAllEvaluations = async () => { + const batchSize = 10 // Number of evaluations to make in each batch + const maxRetryCount = 3 // Maximum number of times to retry a failed evaluation + const retryDelay = 3000 // Delay before retrying a failed evaluation (in milliseconds) + const delayBetweenBatches = 5000 // Delay between batches (in milliseconds) + setEvaluationStatus(EvaluationFlow.EVALUATION_STARTED) - Promise.all(rows.map((row) => runEvaluation(row.id!, rows.length - 1, false))) - .then(() => { + const runBatch = async (startIdx) => { + const endIdx = Math.min(startIdx + batchSize, rows.length) + const batchPromises = [] + + for (let index = startIdx; index < endIdx; index++) { + batchPromises.push( + runEvaluationWithRetry(rows[index].id!, rows.length - 1, false, maxRetryCount, retryDelay) + ) + } + + try { + await Promise.all(batchPromises) + } catch (err) { + console.error("Error in batch:", err) + // Handle the failure of the entire batch (if needed) + } + + // Schedule the next batch with a delay + const nextBatchStartIdx = endIdx + if (nextBatchStartIdx < rows.length) { + setTimeout(() => runBatch(nextBatchStartIdx), delayBetweenBatches) + } else { + // All batches completed setEvaluationStatus(EvaluationFlow.EVALUATION_FINISHED) message.success("Evaluations Updated!") - }) - .catch((err) => console.error("An error occurred:", err)) + } + } + + // Start the first batch + runBatch(0) } const runEvaluation = async ( diff --git a/agenta-web/src/components/EvaluationTable/SingleModelEvaluationTable.tsx b/agenta-web/src/components/EvaluationTable/SingleModelEvaluationTable.tsx index 5a1fdaad36..1ab2a1fda7 100644 --- a/agenta-web/src/components/EvaluationTable/SingleModelEvaluationTable.tsx +++ b/agenta-web/src/components/EvaluationTable/SingleModelEvaluationTable.tsx @@ -213,16 +213,76 @@ const SingleModelEvaluationTable: React.FC = ({ .catch(console.error) } + const runEvaluationWithRetry = async ( + id: string, + count: number = 1, + showNotification: boolean = true, + maxRetryCount: number, + retryDelay: number + ) => { + let retryCount = 0 + while (retryCount <= maxRetryCount) { + try { + await runEvaluation(id, count, showNotification) + // If the evaluation is successful, break out of the retry loop + break; + } catch (error) { + console.error(`Error in evaluation for ID ${id}, retrying...`) + retryCount++ + if (retryCount <= maxRetryCount) { + // Add a delay before retrying + await delay(retryDelay) + } else { + console.error(`Max retry count reached for evaluation ID ${id}.`) + // Handle the failure after max retries (if needed) + } + } + } + } + + const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms)) + const runAllEvaluations = async () => { + const batchSize = 10 // Number of evaluations to make in each batch + const maxRetryCount = 3 // Maximum number of times to retry a failed evaluation + const retryDelay = 3000 // Delay before retrying a failed evaluation (in milliseconds) + const delayBetweenBatches = 5000 // Delay between batches (in milliseconds) + setEvaluationStatus(EvaluationFlow.EVALUATION_STARTED) - Promise.all(rows.map((row) => runEvaluation(row.id!, rows.length - 1, false))) - .then(() => { + const runBatch = async (startIdx) => { + const endIdx = Math.min(startIdx + batchSize, rows.length) + const batchPromises = [] + + for (let index = startIdx; index < endIdx; index++) { + batchPromises.push( + runEvaluationWithRetry(rows[index].id!, rows.length - 1, false, maxRetryCount, retryDelay) + ) + } + + try { + await Promise.all(batchPromises) + } catch (err) { + console.error("Error in batch:", err) + // Handle the failure of the entire batch (if needed) + } + + // Schedule the next batch with a delay + const nextBatchStartIdx = endIdx + if (nextBatchStartIdx < rows.length) { + setTimeout(() => runBatch(nextBatchStartIdx), delayBetweenBatches) + } else { + // All batches completed setEvaluationStatus(EvaluationFlow.EVALUATION_FINISHED) message.success("Evaluations Updated!") - }) - .catch((err) => console.error("An error occurred:", err)) + } + } + + // Start the first batch + runBatch(0) } + + const runEvaluation = async ( id: string, count: number = 1,