generative-ai-python icon indicating copy to clipboard operation
generative-ai-python copied to clipboard

How does google.generativeai.count_message_tokens work?

Open ryuryukke opened this issue 1 year ago • 3 comments

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

ryuryukke avatar Nov 27 '23 09:11 ryuryukke

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

ryuryukke avatar Nov 27 '23 09:11 ryuryukke

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.

Shafiyullah avatar Dec 07 '23 14:12 Shafiyullah

It seems to be working fine with gemini-pro

huberemanuel avatar Mar 01 '24 20:03 huberemanuel

count_message_tokens was a palm based interface. We've stopped development on those (use GenerativeModel.count_tokens). Closing.

MarkDaoust avatar May 17 '24 22:05 MarkDaoust