Uploading files uploaded to ChatGPT to an external server via Actions

Hey there fellow developers.

I have a question regarding making chatgpt use actions of a server endpoint to upload PDFs or image documents that have been uploaded to the GTPs.

Let me give you an example. Here is the FastAPI based endpoint:

@app.post("/file-to-url")
async def upload_file(file_format: str, file: UploadFile = File(...)):
    # Generate a unique filename with the given format
    unique_id = str(uuid4())
    filename = f"{unique_id[:5]}.{file_format}"
    file_path = os.path.join(settings.STATIC_DIR, filename)

    # Check if the byte stream is not empty
    if file.file is None:
        raise HTTPException(status_code=400, detail="File is empty.")

    # Save the file asynchronously
    async with aiofiles.open(file_path, 'wb') as out_file:
        content = await file.read()  # Async read
        await out_file.write(content)

    # Construct the URL to access the file
    file_url = f"{settings.DOMAIN}/static/{filename}"

    return JSONResponse(content={"url": file_url})

What it does is recieves a file upload and generates a url to be used for another action which only takes URL inputs. The path in openapi actions is given below:

"paths": {
    "/file-to-url": {
      "post": {
        "summary": "Upload File",
        "operationId": "upload_file_file_to_url_post",
        "parameters": [
          {
            "required": true,
            "schema": { "title": "File Format", "type": "string" },
            "name": "file_format",
            "in": "query"
          }
        ],
        "requestBody": {
          "content": {
            "multipart/form-data": {
              "schema": {
                "$ref": "#/components/schemas/Body_upload_file_file_to_url_post"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": { "application/json": { "schema": {} } }
          },
          "422": {
            "description": "Validation Error",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/HTTPValidationError" }

When ChatGPT uses this action, it just ends up sending the filename from its local environment to the server without sending the file.

I have tried replacing it with either bytes or base64. The base64 method of upload works but only for the first 100 to 500 characters as those are the characters it prints out after it executes the python interpreter internally.

Can you guys please suggest whats the best approach?

2 Likes

seems like we are all stuck there. can’t pass all of your base64 string ! if you found a workaround, please let us know

2 Likes