Skip to content

Commit bcf8540

Browse files
committed
Enhance OpenAI API key retrieval and error handling; improve logging for API calls and responses
1 parent 32d0f94 commit bcf8540

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

functions-node/utils/apiUtils.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
1-
/**
2-
* Get OpenAI API key from environment variable
3-
* @returns {string} API key
4-
* @throws {Error} If API key is not configured
5-
*/
61
function getOpenAIApiKey() {
7-
const apiKey = process.env.OPENAI_API_KEY;
2+
const apiKey = process.env.OPENAI_API_KEY ||
3+
(process.env.FIREBASE_CONFIG ?
4+
JSON.parse(process.env.FIREBASE_CONFIG).openai?.apikey : null);
85

96
if (!apiKey) {
107
console.error('OpenAI API key not found in environment');
11-
throw new Error('OpenAI API key not configured. Please set OPENAI_API_KEY environment variable');
8+
throw new Error('OpenAI API key not configured');
129
}
1310

1411
return apiKey;
15-
}
16-
17-
module.exports = {
18-
getOpenAIApiKey
19-
};
12+
}

functions-node/utils/documentFormatUtils.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,18 @@ async function getFormattedDocumentData(userId, documentType) {
5858
* @returns {Promise<Object>} JSON response from OpenAI
5959
*/
6060
async 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

Comments
 (0)