generative-ai-python
generative-ai-python copied to clipboard
How does google.generativeai.count_message_tokens work?
Hi there,
I tried to count the token number of a text using google.generativeai.count_message_tokens
according to the following code.
I got 12 for the input "a".
Does google.generativeai.count_message_tokens
include something besides a prompt, which is now "a" ?
I would appreciate any comments.
Thanks.
import google.generativeai as palm
import os
palm_api_key = os.getenv("PALM_API_KEY")
palm.configure(api_key=palm_api_key)
def count_tokens(string):
res = palm.count_message_tokens(model='models/chat-bison-001', prompt=string)
return res['token_count']
print(count_tokens('a'))
# 12
Furthermode, I tried another string "aaaaaaa", but got the same 12. Why does this happen?
import google.generativeai as palm
import os
palm_api_key = os.getenv("PALM_API_KEY")
palm.configure(api_key=palm_api_key)
def count_tokens(string):
res = palm.count_message_tokens(model='models/chat-bison-001', context=None, examples=None, messages="a")
return res['token_count']
print(count_tokens('aaaaaaa'))
# 12
I think the model you using, that cause the issue. Maybe, It's token limit have 12. I don't exact, where the issue is.
Try using different model names with the count_message_tokens function.
It seems to be working fine with gemini-pro
count_message_tokens
was a palm based interface. We've stopped development on those (use GenerativeModel.count_tokens
). Closing.