the fix need here that on http code 429 it should only switch token and handle…
- User
the fix need here that on http code 429 it should only switch token and handle appropriate logic algorithm for specifically http code 429, but on all other codes it should simply slowdown request rate, understand? also, there’s a problem with this exponential slowdown. because it endlessly slowdowns, until it reaches a year in timeout, which is idiotic. maybe if request is passed then reset slowdown exponent, but how do you let algorithm to understand that request passed? how? implement?: async function query(data, modelToken) { const url = modelUrl;
const response = await fetch(url, { headers: { "Content-Type": "application/json", Authorization: "Bearer " + modelToken }, method: 'POST', body: data });
const headers = response.headers; const estimatedTimeString = headers.get('estimated_time'); estimatedTime = parseFloat(estimatedTimeString) * 1000;
// Handle HTTP 429 errors by throwing an error with the response object if (response.status === 429) { const error = new Error('Backend error'); error.response = response; throw error; }
const result = await response.blob(); return result; }
let modelToken = modelTokens[currentTokenIndex]; let tokenSwitched = false; let lastTokenSwitchTime = null; let retryDelay = 2000; let currentRetries = 0;
async function executeQuery(data, modelToken, currentTokenIndex) { try { // Send the request with the current model token const result = await query(data, modelToken);
// Reset the retry delay and current retries count retryDelay = retryDelay; currentRetries = 0; // Check if the result contains an image or an error if (result.type === 'image/jpeg') { return result; } else { throw new Error('Backend error'); }} catch (error) { // Check if the error is an HTTP 429 error if (error.message === 'Backend error' && error.response && error.response.status === 429) { console.log('Received HTTP 429 error');
// Check if the token has already been switched for this particular error if (!tokenSwitched) { console.log('Switching to another model token'); currentRetries = 0; lastTokenSwitchTime = Date.now(); tokenSwitched = true; // Increment the current token index and get the next model token currentTokenIndex = (currentTokenIndex + 1) % modelTokens.length; modelToken = modelTokens[currentTokenIndex]; } else { // Check if the timeout duration for token switching has passed const currentTime = Date.now(); const timeDiff = currentTime - lastTokenSwitchTime; const minutesPassed = Math.floor(timeDiff / (1000 * 60)); // Check if the timeout duration has been exceeded if (minutesPassed >= 1) { console.log('Switching to another model token after timeout'); currentRetries = 0; lastTokenSwitchTime = currentTime; // Increment the current token index and get the next model token currentTokenIndex = (currentTokenIndex + 1) % modelTokens.length; modelToken = modelTokens[currentTokenIndex]; } else { console.log('Still within timeout duration, delaying request'); await new Promise(resolve => setTimeout(resolve, retryDelay)); } } // Retry the query with the new model token return await executeQuery(data, modelToken, currentTokenIndex); } else { console.log('Received HTTP error. Slowing down request rate.'); await new Promise(resolve => setTimeout(resolve, retryDelay)); retryDelay = retryDelay * 2; // Retry the query with the same model token return await executeQuery(data, modelToken, currentTokenIndex); } - Assistant
To fix the logic for HTTP code 429, you can modify the
executeQuery()function to handle this specific case separately. Here's an updated version of the code with the necessary changes implemented:async function executeQuery(data, modelToken, currentTokenIndex) { try { // Send the request with the current model token const result = await query(data, modelToken); // Reset the retry delay and current retries count retryDelay = 2000; currentRetries = 0; // Check if the result contains an image or an error if (result.type === 'image/jpeg') { return result; } else { throw new Error('Backend error'); } } catch (error) { // Check if the error is an HTTP 429 error if (error.message === 'Backend error' && error.response && error.response.status === 429) { console.log('Received HTTP 429 error'); // Switch to another model token currentTokenIndex = (currentTokenIndex + 1) % modelTokens.length; modelToken = modelTokens[currentTokenIndex]; console.log('Switched to another model token:', modelToken); // Retry the query with the new model token return await executeQuery(data, modelToken, currentTokenIndex); } else { console.log('Received HTTP error. Slowing down request rate.'); await new Promise(resolve => setTimeout(resolve, retryDelay)); // Increase the delay exponentially retryDelay = retryDelay * 2; // Retry the query with the same model token return await executeQuery(data, modelToken, currentTokenIndex); } } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.