Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.
This repository was archived by the owner on Apr 30, 2025. It is now read-only.

time our error with proposed fix #1

@cometbus

Description

@cometbus

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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions