I am stuck in a problem, I want to send files with prompts. I am using Scala and Akka. I am able to send small jpegs as base64 and it is working but sending images other than that or files such pdfs, docs, etc are returning a Bad Request or no response.
Python and several other popular languages have library support for dealing with the File API, if you can’t find it in the library you are using, then the REST API is the way to go.
Yes you are correct, the multipart form I am trying to build is wrong. I checked out the resources you provided. I tried building the multipart function but failed :D. I am not understanding the file part of my Multipart form data.
Right, the large file (too large to be sent inline) is first uploaded using the File API. Then, when you formulate the query, at the location in the prompt where the file is supposed to show up, you give it a Part that contains the uri that you got from the File API.
Files uploaded to the File API are free (no storage fee). They live there for two days and they are automatically cleaned up. From this description it should be obvious that this is not your Google Drive, it is of course in the Google cloud. You can delete them faster if you want, there is another File API function to do that adjacent in the documentation. That might be useful because there is an overall storage cap per project, so if you have many big files you could run into the maximum. It’s pretty generous, I have not come across “you have used up all your space” type restriction myself.
It has not been easy for anyone and the documentation is, well, confusing. Here is an example of people struggling and in the end successful with the File API (with enough code to help): How to upload files with media.upload in REST API?
I don’t know the languages in question, but a few thoughts based on me implementing the File API:
You probably don’t want Multipart.FormData, since that usually has a very different encoding. More likely to work is Multipart.Related.
I always see it as the metadata portion first, and then the file content. But I’m not sure that’s required.
You don’t need to specify all that metadata. In fact, you don’t need to specify any. (The mime type is required, but you can specify that as the mime type of the data part in the HTTP body.)
That suggests that you may not even need to use mutlipart anything and can just send the data.
If you can use Vertex AI and Google Cloud Storage instead, that will be easier since there are usually libraries that support Google Cloud Storage already.
If something isn’t working, seeing the error may be able to help us further. Meanwhile, I’ll read over the code and my notes for the last time I did this with REST. It is possible.
I’m not sure what you are building and if this will work for you, but if you simply need the context from the files you are uploading, then consider context caching. Then you could use a language with a simpler Gemini API SDK like Python to upload your prompts/files and create a cache. Then simply use the cache id in your C# program via REST to call the model with the needed context. The only issue is it cost money, however, google is giving everyone $150 in free credits to start.
I can’t use the Gemini SDK, It is not supported in Scala. I have the interactions on the backend with Akka so I have battle it out with the REST API documentation.