A Gemini and Gemma tokenizer in Java
It’s always interesting to know how the sausage is made, don’t you think? That’s why, a while ago, I looked at embedding model tokenization, and I implemented a little visualization to see the tokens in a colorful manner. Yet, I was still curious to see how Gemini would tokenize text…
Both LangChain4j Gemini modules (from
Vertex AI and from
Google AI Labs)
can count the tokens included in a piece of text.
However, both do so by calling a REST API endpoint method called countTokens
.
This is not ideal, as it requires a network hop to get the token counts, thus adding undesired extra latency.
Wouldn’t it be nicer if we could count tokens locally instead?