@@ -5,12 +5,6 @@ const { getLetterGrade } = require('./utils/gradeUtils');
55const { getOpenAIApiKey } = require ( './utils/apiUtils' ) ;
66const { calculateExactGradeStatistics, formatDataForAIPrediction } = require ( './utils/calculationUtils' ) ;
77
8- // Global variable to store the OpenAI client instance
9- let openaiClient = null ;
10-
11- // We'll use lazy initialization instead of onInit
12- console . log ( 'predictGrade module loaded - OpenAI client will be initialized on first use' ) ;
13-
148/**
159 * Cloud Function to predict final grade using AI
1610 */
@@ -77,14 +71,13 @@ exports.predictFinalGrade = functions.https.onCall(async (data, context) => {
7771 * @returns {Promise<Object> } Prediction result
7872 */
7973async function getAIPrediction ( data ) {
80- // Lazy initialization of OpenAI client
81- if ( ! openaiClient ) {
82- console . log ( 'Initializing OpenAI client on first use' ) ;
83- const apiKey = getOpenAIApiKey ( ) ;
84- openaiClient = new OpenAI ( {
85- apiKey : apiKey
86- } ) ;
87- }
74+ // Get API key
75+ const apiKey = getOpenAIApiKey ( ) ;
76+
77+ // Initialize OpenAI client
78+ const openai = new OpenAI ( {
79+ apiKey : apiKey
80+ } ) ;
8881
8982 // Create a highly structured prompt with clear JSON instructions
9083 const prompt = `
@@ -127,7 +120,7 @@ RESPOND ONLY WITH VALID JSON IN THIS EXACT FORMAT:
127120 try {
128121 console . log ( 'Calling OpenAI API for prediction' ) ;
129122 // Call OpenAI with strict instruction for JSON
130- const response = await openaiClient . chat . completions . create ( {
123+ const response = await openai . chat . completions . create ( {
131124 model : "gpt-4o-mini" ,
132125 messages : [
133126 {
0 commit comments