const apiKey = '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';
const apiUrl = 'https://api.openai.com/v1/chat/completions';
const makeRequest = () => {
const requestBody = {
// Your request body here
};
fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
},
body: JSON.stringify(requestBody),
})
.then(response => {
if (response.ok) {
return response.json();
} else if (response.status === 429) {
// Implement a retry mechanism
console.log('Rate limit exceeded. Retrying after a delay...');
setTimeout(makeRequest, 5000); // Retry after 5 seconds (adjust as needed)
} else {
throw new Error(`HTTP error! Status: ${response.status}`);
}
})
.then(data => {
console.log('Response:', data);
})
.catch(error => {
console.error('Error:', error);
});
};
// Make the initial request
makeRequest();