Am I using the FileData / Uri part wrong?

I have some reference materials on a website - a .txt file. Looks like I can have the API reference it, though I couldn’t find much documentation on it. The API doesn’t seem to like my request.

{"contents": [{
    "parts": [
      {
        "text": "USER QUERY HERE",
        "fileData": {
          "mimeType": "text/plain",
          "fileUri": "http://example.com/filename.txt"
        }
      }
    ]
  }

First of all, does the JSON look correct? If so, is the text file just included in the request and the chatbot can just see anything in there, as if it were a part of the user text? Would this be different than just including it in the main content parts ?

Here is the response I get:

{
  "error": {
    "code": 400,
    "message": "* GenerateContentRequest.contents: contents is not specified\n",
    "status": "INVALID_ARGUMENT"
  }
}

The problem is that each part can be either a text part or a fileData part or inlineData or one of the function parts. See https://ai.google.dev/api/rest/v1beta/Content#Part for details.
You’re allowed multiple parts - they just need to be different kinds in each. So that would look more like:

{"contents": [{
    "parts": [
      {
        "text": "USER QUERY HERE"
      },{
        "fileData": {
          "mimeType": "text/plain",
          "fileUri": "http://example.com/filename.txt"
        }
      }
    ]
  }

The second problem, however, is that although “text/plain” is probably a valid type (Vertex AI lists it as valid), you can’t import it from an external URL. The URL must be one that was provided when you uploaded the file using the File API.

When reading it in, it will treat it just like other parts in the prompt.

Wonderful - I think I tried it that way too but it also didn’t work. Perhaps it is related to your comment about needing to upload a file first. I haven’t tried that - I thought I could just reference my own URL links.

As for “reading it in and treating it like other parts in the prompt” - what would be the benefit, then? I already “pull in” text parts based on the user query as if it were an external file (that way i don’t always send up the entire reference doc). Is there a benefit to uploading a file vs. including it inside one of the text parts? Sure, a benefit would be that you don’t have to send it up every time but as I said, I already parse the user input so as to not load it if not necessary.

Is there a benefit such as not using as many tokens? Guess I’m just trying to figure out if my current solution is fine without having to also upload a file somewhere and then reference it.

The benefits are

  • UX management (ie - if the user uploads a file, you can treat it as a file, and not special case some files but not others)
  • network latency (ie - if you have it in the File API, then you don’t have to re-send it as a text part every step of the conversation)

There is no benefit in processing speed or number of tokens used.

Nope. If you want to get something from an external URL you need to fetch the content and store it in the File API yourself.

1 Like

Was very helpful. Thanks for your responses - and for answering all sub-questions within my questions :slight_smile: