I’m currently testing integrating OpenAI’s GPT API on a small-scale project that involves index.html(web-page), index.cjs (where the code is run and was previously index.js) And then the necessary files such as the .env , package-lock.json, node_modules , and package.json. I’ve done the following commands via gitbash already:
-npm install dotenv
-npm install openai
I declared dotenv at the beginning of my index.cjs file and I correctly set up my API key in the .env by the following format: OPENAI_API_KEY = example_api
I did the following troubleshooting steps:
- verified .env file is present in under the root project directory and that the API key is correctly configured.
- Attempted to print the API key to the console but got “undefined”.
3.Attempted different API keys from my account and a friends whom we both have no usage and are on a free plan. I used the gpt-3.5-turbo model, somehow I got error 429 on both given that neither has ever used it.
4.Restarted VS code to make sure changes to the .env file and environment variable were applied.
5.Restarted GitBash. - Got the following errors: “RateLimitError: 429″(Exceeding quota), “NotFoundError: 404″(Model unavailable when I had used GPT-4”, and “AuthenticationError”(incorrect API key).
What I Tried:
I checked my .env file to ensure the OPENAI_API_KEY is correctly set and loaded using the dotenv package.
I logged the API key to the console to verify it was being loaded correctly.
I tested the connection to OpenAI’s API by running a script that attempts to generate a haiku using the gpt-3.5-turbo model.
I also tried using different API keys and different OpenAI models to see if they work under the free plan.
I checked my OpenAI dashboard to confirm my quota usage was at 0 and that I was on the free plan with no cost.
What I Expected:
I expected the OpenAI API key to load correctly from the .env file.
I anticipated the haiku generation to succeed, without hitting rate limits or quota errors.
I thought that the gpt-3.5-turbo model would work under the free tier without exceeding usage limits, given that no cost had been incurred yet.
I expected to see the generated haiku printed in the console if everything was set up correctly.
What Actually Happened:
The API key loaded correctly, as confirmed by the logged output.
However, despite having no usage cost, I received an “insufficient quota” error (status 429) indicating that I had exceeded my current quota, even though no usage had occurred.
The model I selected (gpt-3.5-turbo) should be available on the free plan, but I encountered a rate limit error suggesting a quota issue.
No haiku was generated due to this error, and instead, the error message pointed to an issue with my plan or quota. The following code:
index.cjs:
// Load environment variables before anything else
require('dotenv').config();
// Import the OpenAI client
const { OpenAI } = require('openai');
// Initialize the OpenAI client with the API key from the environment
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY, // The API key is automatically read from the .env file
});
async function generateHaiku() {
try {
// Call OpenAI's API to generate a haiku based on a prompt
const completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo", // Using GPT-3.5 for the free plan
messages: [
{ role: "system", content: "You are a helpful assistant." },
{
role: "user",
content: "Write a haiku about recursion in programming.",
},
],
});
// Log the generated haiku
console.log("Generated Haiku:", completion.choices[0].message.content);
} catch (error) {
// Catch any errors and log them
console.error("Error generating haiku:", error);
}
}
// Call the function to generate the haiku
generateHaiku();