-
Notifications
You must be signed in to change notification settings - Fork 12
time our error with proposed fix #1
Description
I was getting a time out error.
node:internal/deps/undici/undici:11413
Error.captureStackTrace(err, this);
^
TypeError: fetch failed
at Object.fetch (node:internal/deps/undici/undici:11413:11) {
cause: HeadersTimeoutError: Headers Timeout Error
at Timeout.onParserTimeout [as callback] (node:internal/deps/undici/undici:9605:32)
at Timeout.onTimeout [as _onTimeout] (node:internal/deps/undici/undici:7905:17)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7) {
code: 'UND_ERR_HEADERS_TIMEOUT'
}
}
proposed fix:
const askOpenAI = async function (prompt, role, modelChoice = 'gpt-3.5-turbo', tokens=5000, temp=0.85) {
let now = new Date();
let roleContent = "You are an ChatGPT-powered chat bot.";
if (role == 'machine') {
roleContent = "You are a computer program attempting to comply with the user's wishes.";
}
if (role == 'writer') {
roleContent = "You are a professional fiction writer who is a best-selling author. You use all of the rhetorical devices you know to write a compelling book.";
}
return new Promise(function(resolve, reject) {
const timeout = setTimeout(() => {
reject(new Error('Request timed out after 5 minutes'));
}, 5 * 60 * 1000); // 5 minutes in milliseconds
fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
'headers': {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.API_KEY}`,
},
'body': JSON.stringify({
'model': modelChoice,
"messages": [
{"role": "system", "content": roleContent},
{"role": "user", "content": prompt},
],
"max_tokens": tokens,
"temperature": temp,
})
}).then(response => response.json()).then(data => {
clearTimeout(timeout);
let elapsed = new Date() - now;
console.log('\nOpenAI response time: ' + elapsed + 'ms\n');
resolve(data);
}).catch(error => {
clearTimeout(timeout);
reject(error);
});
});
}
exports.askOpenAI = askOpenAI;