@@ -58,16 +58,18 @@ async function getFormattedDocumentData(userId, documentType) {
5858 * @returns {Promise<Object> } JSON response from OpenAI
5959 */
6060async function callOpenAIForFormatting ( prompt ) {
61- // Get OpenAI API key
62- const apiKey = getOpenAIApiKey ( ) ;
63-
64- // Initialize OpenAI client
65- const openai = new OpenAI ( {
66- apiKey : apiKey
67- } ) ;
68-
69- console . log ( "Calling OpenAI API for formatting" ) ;
7061 try {
62+ // Get OpenAI API key
63+ console . log ( "Attempting to get OpenAI API key" ) ;
64+ const apiKey = getOpenAIApiKey ( ) ;
65+ console . log ( "Successfully retrieved API key" ) ;
66+
67+ // Initialize OpenAI client
68+ const openai = new OpenAI ( {
69+ apiKey : apiKey
70+ } ) ;
71+
72+ console . log ( "Calling OpenAI API for formatting" ) ;
7173 // Call OpenAI with strict instruction for JSON
7274 const response = await openai . chat . completions . create ( {
7375 model : "gpt-4o-mini" ,
@@ -78,24 +80,28 @@ async function callOpenAIForFormatting(prompt) {
7880 } ,
7981 { role : "user" , content : prompt }
8082 ] ,
81- temperature : 0.1 , // Low temperature for consistency
82- response_format : { type : "json_object" } // Enforces JSON response
83+ temperature : 0.1 ,
84+ response_format : { type : "json_object" }
8385 } ) ;
8486
85- // Extract the JSON response
86- const responseContent = response . choices [ 0 ] . message . content ;
8787 console . log ( "OpenAI response received" ) ;
88+ const responseContent = response . choices [ 0 ] . message . content ;
8889
8990 try {
9091 // Parse JSON
91- return JSON . parse ( responseContent ) ;
92+ const parsed = JSON . parse ( responseContent ) ;
93+ console . log ( "Successfully parsed OpenAI response into JSON" ) ;
94+ return parsed ;
9295 } catch ( parseError ) {
9396 console . error ( "Error parsing OpenAI response:" , parseError ) ;
94- console . error ( "Raw response:" , responseContent ) ;
97+ console . error ( "Raw response (first 100 chars) :" , responseContent . substring ( 0 , 100 ) ) ;
9598 throw new Error ( "Failed to parse OpenAI response" ) ;
9699 }
97100 } catch ( error ) {
98- console . error ( "Error calling OpenAI API:" , error ) ;
101+ console . error ( "Error in OpenAI API call:" , error . message ) ;
102+ if ( error . response ) {
103+ console . error ( "OpenAI API error details:" , error . response . data ) ;
104+ }
99105 throw error ;
100106 }
101107}
0 commit comments