Class PartUtils

java.lang.Object
uno.anahata.ai.internal.PartUtils

public class PartUtils extends Object
Utility methods for working with Gemini Part objects.

This class provides helpers for converting files to blobs, calculating the size of parts in bytes, and estimating token counts.

Author:
anahata
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    calculateApproxTokenSize(com.google.genai.types.Part part)
    Estimates the token count of a Part using a simple heuristic (bytes / 4).
    static long
    calculateSizeInBytes(com.google.genai.types.Part part)
    Calculates the size of a Part in bytes when serialized to JSON.
    static com.google.genai.types.Part
    toPart(File file)
    Converts a local file into a Gemini Part containing a Blob.
    static String
    toString(com.google.genai.types.Blob blob)
    Returns a human-readable string representation of a Blob.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • PartUtils

      public PartUtils()
  • Method Details

    • toPart

      public static com.google.genai.types.Part toPart(File file) throws IOException
      Converts a local file into a Gemini Part containing a Blob.

      It uses Apache Tika to automatically detect the MIME type of the file.

      Parameters:
      file - The file to convert.
      Returns:
      A Part object containing the file's data and MIME type.
      Throws:
      IOException - if the file cannot be read.
    • toString

      public static String toString(com.google.genai.types.Blob blob)
      Returns a human-readable string representation of a Blob.
      Parameters:
      blob - The blob to describe.
      Returns:
      A string containing the MIME type and size in bytes.
    • calculateSizeInBytes

      public static long calculateSizeInBytes(com.google.genai.types.Part part)
      Calculates the size of a Part in bytes when serialized to JSON.
      Parameters:
      part - The part to measure.
      Returns:
      The size in bytes.
    • calculateApproxTokenSize

      public static int calculateApproxTokenSize(com.google.genai.types.Part part)
      Estimates the token count of a Part using a simple heuristic (bytes / 4).

      This is a rough approximation and may vary depending on the model's tokenizer.

      Parameters:
      part - The part to analyze.
      Returns:
      The approximate number of tokens.