mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-08-28 18:03:03 +02:00
Fix Qwen api concurrency limit issue
This commit is contained in:
parent
c8302c27ac
commit
a368451614
1 changed files with 51 additions and 21 deletions
|
@ -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',
|
||||||
};
|
};
|
||||||
|
|
||||||
const response = await fetch(url, {
|
let retryCount = 0;
|
||||||
method: 'POST',
|
|
||||||
headers,
|
|
||||||
body: JSON.stringify(data),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
while (retryCount < maxRetries) {
|
||||||
const errorText = await response.text();
|
try {
|
||||||
console.error(`Request failed, status code ${response.status}: ${response.statusText}`);
|
const response = await fetch(url, {
|
||||||
console.error('Error response content:', errorText);
|
method: 'POST',
|
||||||
throw new Error(`Request failed, status code ${response.status}: ${response.statusText}`);
|
headers,
|
||||||
}
|
body: JSON.stringify(data),
|
||||||
|
});
|
||||||
|
|
||||||
const responseText = await response.text();
|
if (response.ok) {
|
||||||
try {
|
const responseText = await response.text();
|
||||||
return JSON.parse(responseText);
|
try {
|
||||||
} catch (err) {
|
//Task completed successfully
|
||||||
console.error('Failed to parse response JSON:', err);
|
return JSON.parse(responseText);
|
||||||
throw new Error('Invalid response JSON format.');
|
} catch (err) {
|
||||||
|
console.error('Failed to parse response JSON:', err);
|
||||||
|
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.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue