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

The count tokens function generates an `google.api_core.exceptions.PermissionDenied` error when using an uploaded file in the prompt using the files api #365

Closed haruiz closed 1 month ago

haruiz commented 1 month ago

Description of the bug:

After uploading a file using the file API and calling the count_tokens functions, I get the following error.

google.api_core.exceptions.PermissionDenied: 403 You do not have permission to access the File 8lqhztqcd6g1 or it may not exist

Here is my code:

GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
genai.configure(api_key=GOOGLE_API_KEY)

model_name = "models/gemini-1.5-pro-latest"
model = genai.GenerativeModel(model_name)

audio_file_path = "./../data/audio_example.mp3"
audio_sample = genai.upload_file(audio_file_path)
model.count_tokens(audio_sample)

Additional info: For some reason, if I comment the model.count_tokens call line, and run the following code, it works:

response = model.generate_content([
    "Describe the following audio:",
    audio_sample
])
print(response)

Actual vs expected behavior:

Return the number of tokens as shown in the notebook: https://github.com/google-gemini/cookbook/blob/main/quickstarts/Counting_Tokens.ipynb

Any other information you'd like to share?

No response

singhniraj08 commented 1 month ago

@haruiz, I just tried running the counting tokens tutorial with generative-ai-python 0.5.4 and model.count_tokens is able to count the numbers of tokens successfully for audio files. Please try out the tutorial and let us know if you face any issues and share a reproducible colab gist with us to reproduce the issue on our end. Thank you! image

haruiz commented 1 month ago

Hi @singhniraj08, thanks for your response. After updating the library 'generative-ai-python', it works perfectly! Thank you!