Hello Google Community,
I’m currently working on a project that involves integrating image file uploads into a chatbot using the Google Generative AI API. I’ve encountered a problem with the image upload functionality and would appreciate any guidance or suggestions.
I am trying to use the fileManager
from the Google Generative AI SDK to upload an image file, but I’m encountering issues.
const uploadedFile = await fileManager.uploadFile(userImage.filepath, userImage.mimetype);
fileUri = uploadedFile.uri;
The error I’m encountering
TypeError: _google_generative_ai__WEBPACK_IMPORTED_MODULE_0__.GoogleAIFileManager is not a constructor
Steps taken
- Importing
fileManager
: I attempted to importGoogleAIFileManager
from@google/generative-ai
but received the error mentioned above. - Code Implementation: The code for uploading the image is as follows:
import { GoogleGenerativeAI } from "@google/generative-ai";
// API key for accessing Google Gemini API
const apiKey = process.env.API_KEY;
const genAI = new GoogleGenerativeAI(apiKey);
const fileManager = new GoogleAIFileManager(apiKey); // Error occurs here
- Workaround: I’ve used the
axios
package to manually upload the file, which should work correctly. Here’s the code snippet:
const axios = require('axios');
const fs = require('fs');
const FormData = require('form-data');
async function uploadToGemini(filePath, mimeType) {
const form = new FormData();
form.append('file', fs.createReadStream(filePath), { contentType: mimeType });
const response = await axios.post('https://api.generativeai.googleapis.com/v1beta3/files:upload', form, {
headers: {
...form.getHeaders(),
'Authorization': `Bearer YOUR_API_KEY`,
}
});
return response.data;
}
Questions:
- Correct Usage: Is
GoogleAIFileManager
the correct way to handle file uploads, or is there an alternative approach I should use? - API Endpoint: Could there be a discrepancy with the API endpoint for file uploads? The URL
https://api.generativeai.googleapis.com/v1beta3/files:upload
is being used, but should it be different? - Authentication Issues: Are there any authentication or permissions issues that might be affecting the usage of
fileManager
?
Any insights or suggestions would be highly appreciated as I work to resolve this issue.