Assistant API - attachments

Hello All,

Currently I have integration with the assistants and I’m using the -curl HTTP request to create thread, run and receive the message output from the assistant. What I want to do now is include attachment inside my message, I see that can be done, but do first need to upload the file with another HTTP request, retrieve the file id and then pass the file id inside the message or I can directly upload the attachment via URL? The tool which I use have access to my machine so I can directly pass the URL which need to be uploaded?

The file must end up in OpenAI’s storage for your organization first, so that it can be added to a message attachment vector store as opposed to the assistant vector store.

This is the expanded section of *assistants->create message" from the API Reference:

So while you can make it look like in the user interface someone is uploading a document file directly to a message, it is really being added to the file search the AI can perform at its option.

Really asking about the full text of a document requires you to extract text and add it in thread message, possibly a data user message before the real user message, that you might track and delete after a single answer is obtained so you don’t keep paying for the context.

There’s helper methods in some libraries, but the real helper is to write the code yourself and see the operations going on, to facilitate further file and vector store management. (and these attachments are outside of easy management, needing multiple steps to find the auto-created store that expires after 7 days.)

2 Likes

So I would need to first upload a file via the HTPP request for uploading a file, then get the file id pass it into the message and then I can delete if with delete file with the same file id so it does not take space, everything will be automated, but my other questions will be how to get a file from UiPath response they pass the fileid, but how to save that on the machine via the HTTP requests.

Here’s a little demo function of uploading a file in Python. You can see getting the file id printed after you upload one.

def upload_file():
    global pwd
    menu_print()
    while True:
        filename = input("Enter the filename to upload (leave empty to exit): ").strip()
        
        if not filename:  # Exit on empty input
            print("Operation cancelled.")
            return
        
        file_path = pwd / filename
        try:
            with open(file_path, "rb") as file:
                response = client.files.create(file=file, purpose=purpose)
                print(f"File uploaded successfully: {response.filename} [{response.id}]")
                return  response.id # Exit after successful upload
        except FileNotFoundError:
            print("File not found. Please make sure the filename and path are correct.")
        except PermissionError as e:
            print(f"Permission error: {e}")
        except Exception as e:
            print(f"An unexpected error occurred: {e}")

Yes yes, I see having it in python is easier then making HTTP workflow with several API calls. My second questions was about how to retrieve the file if assistant return me a file via the HTTP.

Perhaps you are simply looking for the API reference. It is on the sidebar of this forum. The information is categorized by endpoint, of which “files” is one.

Yes I know that I was thinking that I will recieve fileID from the HTTP response, but what does this mean for my call can I get the actual file? and not only the ID

For files that are created by AI, they will have a purpose: assistants_output that can be a criteria when you list files. You can receive the created file in the tool attachment type of response or as an annotation, and then can download from storage, offer a download link, replace the source text in AI responses, etc.

image

1 Like

Thank you! I will take a look at this. I just need to start experimenting.