Class PartUtils
java.lang.Object
uno.anahata.ai.internal.PartUtils
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 -
Method Summary
Modifier and TypeMethodDescriptionstatic intcalculateApproxTokenSize(com.google.genai.types.Part part) Estimates the token count of aPartusing a simple heuristic (bytes / 4).static longcalculateSizeInBytes(com.google.genai.types.Part part) Calculates the size of aPartin bytes when serialized to JSON.static com.google.genai.types.PartConverts a local file into a GeminiPartcontaining aBlob.static StringtoString(com.google.genai.types.Blob blob) Returns a human-readable string representation of aBlob.
-
Constructor Details
-
PartUtils
public PartUtils()
-
-
Method Details
-
toPart
Converts a local file into a GeminiPartcontaining aBlob.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
Returns a human-readable string representation of aBlob.- 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 aPartin 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 aPartusing 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.
-
