Learn R Programming

gemini.R (version 0.11.0)

countTokens: Count Tokens for Gemini Content (Including Images)

Description

Calculates the token count for a given content, including text and image data, using the Vertex AI Gemini API.

Usage

countTokens(
  jsonkey = NULL,
  model_id = NULL,
  content = NULL,
  region = "us-central1"
)

Value

A numeric value representing the token count of the content.

Arguments

jsonkey

A path to JSON file containing the service account key from Vertex AI.

model_id

The ID of the Gemini model.

content

The content (text, image, or list of text/image parts) for which to count tokens.

  • For text, provide a string.

  • For images, provide a list with data (base64 encoded image) and mimeType (e.g., "image/png", "image/jpeg").

  • For multiple content parts, provide a list where each element is either a text string or an image list.

region

The Google Cloud region where your Vertex AI resources are located (default is "us-central1"). See https://cloud.google.com/vertex-ai/docs/regions for available regions.

Examples

Run this code
if (FALSE) {
library(gemini.R)

# For text content
key_file <- "YOURAPIKEY.json"
model <- "2.0-flash"
token_count_text <- countTokens(
  jsonkey = key_file, 
  model_id = model, 
  content = "Hello, world!"
)
print(token_count_text)

# For image content (assuming 'image.jpg' is in your working directory)
image_data <- base64enc::base64encode("image.jpg")
image_content <- list(data = image_data, mimeType = "image/jpeg")
token_count_image <- countTokens(
  jsonkey = key_file,
  model_id = model,
  content = image_content
)
print(token_count_image)

# For multiple content parts (text and image)
content_parts <- list(
  list(text = "This is the first part."),
  list(data = image_data, mimeType = "image/jpeg"),
  list(text = "This is the last part")
)
token_count_parts <- countTokens(
  jsonkey = key_file,
  model_id = model,
  content = content_parts
)
print(token_count_parts)
}

Run the code above in your browser using DataLab