martinshkreli / bookgen

A Node-based, GPT-powered book generator
53 stars 9 forks source link

New version still times out #2

Open cometbus opened 1 year ago

cometbus commented 1 year ago

I can't get it to complete a book it just times out

cometbus commented 1 year ago

const axios = require('axios');

const askOpenAI = async function (prompt, role, modelChoice = 'gpt-3.5-turbo', tokens = 5000, temp = 0.85, retries = 5, timeout = 340000) { 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.";
}

const requestBody = {
    method: 'post',
    url: 'https://api.openai.com/v1/chat/completions',
    headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${process.env.API_KEY}`,
    },
    data: {
        'model': modelChoice,
        "messages": [
            { "role": "system", "content": roleContent },
            { "role": "user", "content": prompt },
        ],
        "max_tokens": tokens,
        "temperature": temp,
    },
    timeout: timeout,
};

let attempt = 1;

while (attempt <= retries) {
    try {
        const response = await axios(requestBody);
        const data = response.data;

        if (data) {
            let elapsed = new Date() - now;
            console.log(`\nOpenAI response time: ${elapsed}ms\n`);
            return data;
        } else {
            throw new Error('Error: Empty response from OpenAI API');
        }
    } catch (error) {
        console.error(`Error on attempt ${attempt}: ${error.message}`);

        if (attempt === retries) {
            throw new Error(`Failed after ${retries} attempts`);
        }

        attempt++;
    }
}

};

exports.askOpenAI = askOpenAI;