my API key is working perfectly on POSTMAN but whenever i try to generate something from my project it shows error 500 in the console log of the browser.Here’s a snippet of my route.js file of the project
import { NextResponse } from “next/server”;
import OpenAI from “openai”;
// System prompt for generating flashcards
const systemPrompt = `
You are a FlashCard creator.
Your task is to generate flashcards that help users effectively learn and retain information across various subjects. Each flashcard should be concise, clear, and focused on a single concept or question.
Guidelines:
-
Topic Identification : Identify the key topics or concepts that need to be covered. Each flashcard should focus on one key concept, question, or fact.
-
Question and Answer Format : Structure each flashcard with a clear question or prompt on one side (Q) and a direct, informative answer (A) on the other side.
-
Brevity : Keep the content on each flashcard brief. The goal is to present the information in a way that is easy to review quickly.
-
Clarity : Ensure that both the questions and answers are easy to understand, avoiding unnecessary jargon unless the flashcard is intended for advanced learners who are familiar with specific terminology.
-
Variety : Use different types of questions to engage users, such as:
- Definition-based questions : “What is…?”
- Comparison questions : “How does X differ from Y?”
- Application questions : “How would you apply…?”
- True/False questions : “Is it true that…?”
- Fill-in-the-blank questions : “The process of… is known as…?”
-
Customization : Tailor the complexity and depth of the flashcards to the user’s level of knowledge (beginner, intermediate, advanced) and the subject matter.
-
Engagement : Where appropriate, include hints or mnemonics to help users remember difficult concepts.
-
Examples : When possible, provide an example in the answer to illustrate the concept or fact being discussed.
-
Review Cycles : Design flashcards to be reviewed in cycles to aid in long-term retention, with the possibility of more advanced or related flashcards being introduced as the user progresses.
-
Feedback : Encourage users to create their own flashcards based on the examples provided, reinforcing the learning process.
-
Only generate 10 flashcards.
Remember, the primary goal is to facilitate learning and retention through well-structured, clear, and concise flashcards.
Return in the following JSON format:
{
“flashcards” : [{
“front”: str,
“back” : str
}]
}
`;
export async function POST(req) {
try {
// Initialize OpenAI with your API key
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY, // Ensure the API key is set in your environment variables
});
// Get the input data from the request
const data = await req.text();
// Make the request to OpenAI's API
const completion = await openai.chat.completions.create({
messages: [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: data },
],
model: "gpt-4o", // Corrected model name
});
// Parse the JSON response
const flashcards = JSON.parse(completion.choices[0].message.content);
// Return the flashcards as a JSON response
return NextResponse.json(flashcards.flashcards);
} catch (error) {
console.error('Error generating flashcards:', error);
return NextResponse.json({ error: 'Failed to generate flashcards' }, { status: 500 });
}
}