google-gemini / generative-ai-js

The official Node.js / Typescript library for the Google Gemini API
https://www.npmjs.com/package/@google/generative-ai
Apache License 2.0
766 stars 169 forks source link

File upload using GoogleAIFileManager does not work for files from server? #281

Open curvejumpinc opened 1 month ago

curvejumpinc commented 1 month ago

Description of the bug:

I have a use case where the user will upload some file that will be sent to Gemini and then a conversation begins. I'm using the GoogleAIFileManager. The error I get is

Error: ENOENT: no such file or directory, open 'http://127.0.0.1:54321/storage/v1/object/sign/resumes/31a03e74-1639-45b6-bfa7-77447f1a4762/resume-yep5k4.pdf?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1cmwiOiJyZXN1bWVzLzMxYTAzZTc0LTE2MzktNDViNi1iZmE3LTc3NDQ3ZjFhNDc2Mi9yZXN1bWUteWVwNWs0LnBkZiIsImlhdCI6MTcyOTE0MzQwOCwiZXhwIjoxNzI5MTQ0MDA4fQ.Kjcfaaseb_vo6TRoNZq9td7_4Tr5zQS_gPSiyDaCUJI'

Is the API work for files with URLs like this?

Does it need to be a file hosted in a different way?

Actual vs expected behavior:

No response

Any other information you'd like to share?

No response

abdelkd commented 1 week ago

The same use case as mine where I described it in here #294. This change adds support for Buffer instead of file path.

Here's an example I use in my SvelteKit App:

const file = formData.get("file") as File | null
const fileBuffer = await file?.arrayBuffer();
if (!fileBuffer) return

const filename = Date.now() + file?.name

const fileResult = await fileManager.uploadFile(Buffer.from(fileBuffer), {
   displayName: filename.split("/")[2],
   mimeType: "application/pdf",
})