Find answers from the community

Updated 2 months ago

import tiktoken

import tiktoken
from llama_index.core.callbacks import CallbackManager, TokenCountingHandler
token_counter = TokenCountingHandler(
tokenizer=tiktoken.encoding_for_model("gpt-3.5-turbo").encode
)
I use this to calculate token usage for gpt models.
How do i calculate token usage for gemini based models like gemini-pro or gemini-1.5-flash-latest?
L
n
15 comments
no idea lol does gemini provide a tokenizer?
The tokenizer in this case just has to be a callable function, that takes a string and returns a list
is it possible to use gemma?
could yea, using AutoTokenizer from huggingface
assuming thats the same tokenizer that gemini uses
oh i think gemma is only for google open source models
not for gemini models
having a hard time figuring how to calculate token usage for gemini.. when doing .query
gemma or openai token counting will be "close enough" in most cases imo πŸ˜…
looks like you can do this if you are using vertex
Im using gemini from google developer and not vertex.. in that case i have to try using vertex instead
Plain Text
from vertexai.preview.generative_models import GenerativeModel
gemini_pro_model = GenerativeModel("gemini-pro")

def tokenizer(text):
  count = gemini_pro_model.count_tokens("why is sky blue?")
  return [0]*count

...
tokenizer=tokenizer
...
Add a reply
Sign up and join the conversation on Discord