Fix Qwen api concurrency limit issue

This commit is contained in:
Qu Yi 2024-11-09 01:29:57 +08:00
parent c8302c27ac
commit a368451614

View file

@ -49,12 +49,12 @@ export class Qwen {
async embed(text) { async embed(text) {
if (!text || typeof text !== 'string') { if (!text || typeof text !== 'string') {
console.error('Invalid embedding input: text must be a non-empty string.'); console.error('Invalid embedding input: text must be a non-empty string:', text);
return 'Invalid embedding input: text must be a non-empty string.'; return 'Invalid embedding input: text must be a non-empty string.';
} }
const data = { const data = {
model: 'text-embedding-v2', model: this.modelName,
input: { texts: [text] }, input: { texts: [text] },
parameters: { text_type: 'query' }, parameters: { text_type: 'query' },
}; };
@ -67,38 +67,68 @@ export class Qwen {
try { try {
const response = await this._makeHttpRequest(this.url, data); const response = await this._makeHttpRequest(this.url, data);
const embedding = response?.output?.embeddings?.[0]?.embedding; const embedding = response?.output?.embeddings?.[0]?.embedding;
return embedding || 'No embedding result received.'; return embedding || 'No embedding result received.';
} catch (err) { } catch (err) {
console.error('Error occurred:', err); console.log('Embed data:', data);
console.error('Embed error occurred:', err);
return 'An error occurred, please try again.'; return 'An error occurred, please try again.';
} }
} }
async _makeHttpRequest(url, data) { async _makeHttpRequest(url, data, maxRetries = 10) {
const headers = { const headers = {
'Authorization': `Bearer ${this.apiKey}`, 'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}; };
let retryCount = 0;
while (retryCount < maxRetries) {
try {
const response = await fetch(url, { const response = await fetch(url, {
method: 'POST', method: 'POST',
headers, headers,
body: JSON.stringify(data), body: JSON.stringify(data),
}); });
if (!response.ok) { if (response.ok) {
const errorText = await response.text();
console.error(`Request failed, status code ${response.status}: ${response.statusText}`);
console.error('Error response content:', errorText);
throw new Error(`Request failed, status code ${response.status}: ${response.statusText}`);
}
const responseText = await response.text(); const responseText = await response.text();
try { try {
//Task completed successfully
return JSON.parse(responseText); return JSON.parse(responseText);
} catch (err) { } catch (err) {
console.error('Failed to parse response JSON:', err); console.error('Failed to parse response JSON:', err);
throw new Error('Invalid response JSON format.'); throw new Error('Invalid response JSON format.');
} }
} else {
const errorText = await response.text();
if (response.status === 429 || response.statusText.includes('Too Many Requests')) {
// Handle rate limiting
retryCount++;
if (retryCount >= maxRetries) {
console.error('Exceeded maximum retry attempts, unable to get request result.');
throw new Error(`Request failed after ${maxRetries} retries due to rate limiting.`);
}
//Reached Qwen concurrency limit, waiting in queue
const waitTime = Math.random() * 1000; // Random wait between 0 to 1 seconds
await new Promise(resolve => setTimeout(resolve, waitTime));
continue; // Retry the request
} else {
console.error(`Request failed, status code ${response.status}: ${response.statusText}`);
console.error('Error response content:', errorText);
throw new Error(`Request failed, status code ${response.status}: ${response.statusText}`);
}
}
} catch (err) {
// Handle network errors or other exceptions
console.error('Error occurred during HTTP request:', err);
throw err; // Re-throw the error to be handled by the caller
}
}
// Exceeded maximum retries
console.error('Exceeded maximum retry attempts, unable to get request result.');
throw new Error(`Request failed after ${maxRetries} retries.`);
} }
} }