Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Files API #102

Merged
merged 17 commits into from
Apr 19, 2024
Merged

Add Files API #102

merged 17 commits into from
Apr 19, 2024

Conversation

hsubox76
Copy link
Collaborator

@hsubox76 hsubox76 commented Apr 16, 2024

Add file upload API.

  • See newly-added /samples/node/file-upload.js for example usage.
  • Introduces a top-level GoogleAIFileManager class with 4 methods:
    • uploadFile(fileName, fileMetadata) - metadata has properties mimeType, and optional name and displayName. Returns full File metadata including name, mimeType, created time, etc (invents a random name if not provided)
    • listFiles({ pageSize?: number, pageToken?: number })
    • getFile(filename)
    • deleteFile(filename)

This is a separate class outside of GoogleGenerativeAI and needs to be imported from a subpackage @google/generative-ai/files. The rationale is that this is meant for Node users only, for security reasons. Also it uses fs, which will break web builds. Making it a subpackage helps web users avoid importing this code.

This means that when instantiating GoogleAIFileManager, the user needs to provide the API key to the constructor, even if they've already provided it to GoogleGenerativeAI. These are essentially two separate tools, one for file management, and one for inference and interaction with the model.

The upload content type is multipart, which allows sending JSON metadata as well as the binary blob in the same request. Future improvements may include also offering a resumable upload type (perhaps a separate method). The code for generating the multipart content was taken from the Firebase Storage JS SDK's implementation of multipart upload.

Reference (internal):

Copy link

changeset-bot bot commented Apr 16, 2024

🦋 Changeset detected

Latest commit: 12332d2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@google/generative-ai Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@hsubox76 hsubox76 changed the title WIP: Add Files API Add Files API Apr 17, 2024
@hsubox76 hsubox76 marked this pull request as ready for review April 17, 2024 22:46
@hsubox76 hsubox76 requested a review from lahirumaramba April 17, 2024 22:46
Copy link
Collaborator

@lahirumaramba lahirumaramba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Christina! Looks great!
Added a few comments

packages/main/src/files/constants.ts Show resolved Hide resolved
packages/main/src/files/file-manager.ts Outdated Show resolved Hide resolved
mimeType: fileMetadata.mimeType,
};
uploadMetadata.displayName = fileMetadata.displayName;
uploadMetadata.name = fileMetadata.name;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Do we want to set the name and the displayName at initialization?

const uploadMetadata: FileMetadata = {
      mimeType: fileMetadata.mimeType,
      displayName:...
    };

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, that is a bit cleaner.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! We can now remove line 76, 77. and 78-80, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

packages/main/src/files/file-manager.ts Show resolved Hide resolved
Copy link
Collaborator

@lahirumaramba lahirumaramba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you.

mimeType: fileMetadata.mimeType,
};
uploadMetadata.displayName = fileMetadata.displayName;
uploadMetadata.name = fileMetadata.name;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! We can now remove line 76, 77. and 78-80, right?

@hsubox76 hsubox76 requested a review from lahirumaramba April 19, 2024 18:30
@hsubox76 hsubox76 merged commit a89d427 into main Apr 19, 2024
3 checks passed
@hsubox76 hsubox76 deleted the ch-files branch April 19, 2024 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants