Hello,
I’m working with the Gemini API and I want to initiate a conversation without having to resend the same file every time. Specifically, I need to upload a file once, provide it as a reference in future conversations, and then pass additional data (like category structure or keywords) during the chat.
Is there a way to start a chat session with a file, and then have the model reference that file without needing to resend it in every subsequent request?
Any help or advice would be greatly appreciated!
Thank you.
Hi @Baptiste_Richetin,
Welcome to forum, you can use context caching which aligns with your use case and doesn’t require you to send same file content again and again.
Here is a quick-start on how to use it: cookbook/quickstarts/Caching.ipynb at main · google-gemini/cookbook · GitHub
Also reference to developer doc: Context caching | Gemini API | Google AI for Developers
Hope this helps.
Thanks!!
Hi @Siva_Sravana_Kumar_N,
Thank you for your response and for pointing me to the documentation on context caching! I appreciate the guidance.
I went through the caching implementation, but I noticed that there is a minimum token requirement of 32,768 tokens for using cached content. However, depending on the size of my PDF files, I often do not reach this minimum.
Is there a way to bypass or lower this token threshold so that I can still use caching for smaller files? Or would you recommend an alternative approach in such cases?
Thanks again for your help!
Hi @Baptiste_Richetin,
I am not sure if we can bypass or lower this token threshold, other alternative you can use is try and upload the file using file api: cookbook/quickstarts/File_API.ipynb at main · google-gemini/cookbook · GitHub, this will hold the reference to the file context all the time and whenever you require the need to use again you can pass along with the prompt.
One more reference for document understanding: Explore document processing capabilities with the Gemini API | Google AI for Developers
Note: The File API accepts files under 2GB in size and can store up to 20GB of files per project. Files last for 2 days and cannot be downloaded from the API.