Node.js OpenAI library v4.20.1 constantly timing out on basic requests

I’m experiencing ongoing timeout issues when trying to use OpenAI’s API with the Node.js library version 4.20.1. Even for very simple queries, like basic math questions, I keep running into timeouts.

Here’s the implementation I’m currently working with:

const { OpenAI } = require('openai');

async function handleChatRequest(request, response) {
  try {
    const client = new OpenAI({
      apiKey: process.env.OPENAI_SECRET_KEY,
    });

    const userInput = request.body.message;

    const completion = await client.chat.completions.create({
      model: "gpt-4", 
      messages: [{ role: 'user', content: userInput }],
    });

    response.json(completion.choices);
  } catch (err) {
    console.log('OpenAI request failed:', err);
    response.status(500).json({ error: 'Request failed' });
  }
}

exports.handleChatRequest = handleChatRequest;

What am I missing? I’m attempting to create a simple ChatGPT clone using the official OpenAI API, but these timeout errors are really hindering progress. Any suggestions on what might be going wrong?

I encountered similar timeout issues with the Node.js library version 4.20.1. A common solution is to explicitly set a timeout in the client configuration. GPT-4 tends to take significantly more time for responses than GPT-3.5, so increasing the timeout may resolve the problem. For instance, you can set it to 60 seconds as follows:

const client = new OpenAI({
  apiKey: process.env.OPENAI_SECRET_KEY,
  timeout: 60000, // 60 seconds
});

Additionally, ensure your internet connection is stable and monitor the rate limits. I temporarily switched to gpt-3.5-turbo while troubleshooting, which helped pinpoint the cause of the timeouts. It’s also beneficial to log the complete error response to gain more insight into the timeout issues.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.