Hierarchy

  • BaseModule
    • Files

Constructors

Image for: Constructors
  • Parameters

    • apiClient: ApiClient

    Returns Files

Methods

Image for: Methods
  • Downloads a remotely stored file asynchronously to a location specified in the params object. This method only works on Node environment, to download files in the browser, use a browser compliant method like an tag.

    Parameters

    Returns Promise<void>

    The following code downloads an example file named "files/mehozpxf877d" as "file.txt".

    await ai.files.download({file: file.name, downloadPath: 'file.txt'});
    
  • Retrieves the file information from the service.

    Parameters

    Returns Promise<File>

    The Promise that resolves to the types.File object requested.

    const config: GetFileParameters = {
    name: fileName,
    };
    file = await ai.files.get(config);
    console.log(file.name);
  • Lists all current project files from the service.

    Parameters

    Returns Promise<Pager<File>>

    The paginated results of the list of files

    The following code prints the names of all files from the service, the size of each page is 10.

    const listResponse = await ai.files.list({config: {'pageSize': 10}});
    for await (const file of listResponse) {
    console.log(file.name);
    }
  • Uploads a file asynchronously to the Gemini API. This method is not available in Vertex AI. Supported upload sources:

    • Node.js: File path (string) or Blob object.
    • Browser: Blob object (e.g., File).

    Parameters

    • params: UploadFileParameters

      Optional parameters specified in the types.UploadFileParameters interface.

    Returns Promise<File>

    A promise that resolves to a types.File object.

    The mimeType can be specified in the config parameter. If omitted:

    • For file path (string) inputs, the mimeType will be inferred from the file extension.
    • For Blob object inputs, the mimeType will be set to the Blob's type property. Somex eamples for file extension to mimeType mapping: .txt -> text/plain .json -> application/json .jpg -> image/jpeg .png -> image/png .mp3 -> audio/mpeg .mp4 -> video/mp4

    This section can contain multiple paragraphs and code examples.

    types.UploadFileParameters#config for the optional config in the parameters.

    An error if called on a Vertex AI client.

    An error if the mimeType is not provided and can not be inferred, the mimeType can be provided in the params.config parameter.

    An error occurs if a suitable upload location cannot be established.

    The following code uploads a file to Gemini API.

    const file = await ai.files.upload({file: 'file.txt', config: {
    mimeType: 'text/plain',
    }});
    console.log(file.name);