countTokens | R Documentation |
Calculates the token count for a given content, including text and image data, using the Vertex AI Gemini API.
countTokens(
jsonkey = NULL,
model_id = NULL,
content = NULL,
region = "us-central1"
)
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.
|
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. |
A numeric value representing the token count of the content.
## Not run:
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)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.