google-gemini / generative-ai-python

The official Python library for the Google Gemini API
https://pypi.org/project/google-generativeai/
Apache License 2.0
1.19k stars 227 forks source link

Allow count_tokens with no `contents`. #342

Closed MarkDaoust closed 1 month ago

MarkDaoust commented 1 month ago

Fixes: https://github.com/google-gemini/generative-ai-python/issues/326

mayureshagashe2105 commented 1 month ago
model = genai.GenerativeModel(model_name, system_instruction="Talk in rhymes")
model.count_tokens("")

I think this would still raise the following :

https://github.com/google-gemini/generative-ai-python/blob/efead6bea6768f6f4a3d90d348647b0a54fe2435/google/generativeai/types/content_types.py#L267-L269

MarkDaoust commented 1 month ago

Hmmm... It goes through to_contentS first, that catches None:

https://github.com/google-gemini/generative-ai-python/blob/efead6bea6768f6f4a3d90d348647b0a54fe2435/google/generativeai/types/content_types.py#L300-L303

But to should it let "" through?

Updated title.

mayureshagashe2105 commented 1 month ago

It goes through to_contentS first, that catches None

I don't think it will(tested it locally): "" is not None. It will go to to_content method and would raise the error. Even if we change to_contents to handle "", something like:

 def to_contents(contents: ContentsType) -> list[glm.Content]: 
     if not contents: 
         return [] 

The request goes through but the API raises error as contents is a required field for the GenerateContentRequest