Hi, I am trying to use the
genai.upload_file(media / "test.txt")
API. The file successfully uploads but when I asked Gemini to give me a summary of the file, it says “I can’t directly access or read files from the internet. I’m a text-based AI, and I don’t have the ability to interact with external systems like file storage.” I tried the same with PDF and it gives same response. Is this expected (Gemini is not able to read from text or pdf files yet).
Welcome to the forums!
This response seems confusing, but there’s an underlying reason for it. By the time Gemini gets the tokens from the file - it doesn’t know it’s a file. It is just part of the stream of tokens.
This is true for images and movies as well - Gemini doesn’t see them as images or files or whatever, it just sees things as tokens.
As such, if you want it to be aware that there is a file, you need to include text before the fileData
part that says something like “Here is a file named test.txt”. You may need to experiment with what works best for you, but you’ll need something along these lines.
Good luck!
1 Like
@Pawan_Sarma
It seems, you forgot to pass the prompt along with the file. You can try the code below:
result = model.generate_content([genai.upload_file("test.txt"), "Give summary of this file."])
result.text
1 Like
When I did it like this
sample_pdf = genai.upload_file(media / "mappings.txt")
prompt = f"Read the contents of this text file: {sample_pdf}"
response = model.generate_content(prompt)
print(response.text)
it did not work however. why is that the case?
Because that’s not how the Gemini API works.
You need to specify the URL returned by the File API as a fileData
part. Not as part of the text to be tokenized. This way the tokenizer knows it has to go to the file store to fetch the file and convert it into tokens before passing it to the model.