Having the ability for users to upload files can be an essential functionality for many AI applications. Whether it’s allowing users to upload a report, a contract, or homework, integrating this feature can provide a richer interaction experience. This tutorial will walk you through enabling file uploads in your Stack AI application.

Prerequisites

  • Make sure you’re logged into Stack AI.
  • Have a project ready, or create a new project where you’d like to allow file uploads.

Step-by-step Guide:

  1. Add a Document Data Loader

To begin the process, you must integrate a mechanism to handle file uploads.

  • Navigate to the Data Loaders section.
  • Select the Document data loader.
  1. Enable User Uploads

Go to the data loader settings.

  • Inside the Document settings, find the option labeled expose in interface.
  • Toggle or check this option to enable it.
  1. Publish Your Project To reflect the changes and make the upload feature active:

    • Navigate to the project builder.
    • Click on the Publish button or its equivalent. This action will save and deploy your project with the new changes.
  2. Exporting Your Project Export your chatbot or form to make it available on various platforms.

    • From the builder, click on the Export option.
    • Choose whether you want to export as a Chatbot or a Form.
  3. Configure Inputs/Outputs To handle the incoming file data properly, configure the input settings:

    • In the export configuration, navigate to the inputs/outputs section.
    • Here, find the option related to the document node.
    • Enable this node as an input. This ensures that the chatbot or form can accept file uploads from users.
  1. Complete the Remaining Exporting Steps Follow the additional steps to export the project.

  2. Done! Congratulations, you’ve successfully integrated file upload functionality into your chatbot. Test the feature to ensure everything works as expected, and make any necessary adjustments.

Upload via API

You can upload files for your users via API and manage these files under the following endpoints:

1. Upload a file

  • Method: POST
  • Path: https://api.stack-ai.com/documents/{org_id}/{flow_id}/{node_id}/{user_id}
  • Description: This endpoint allows for uploading files to a chat specific to a user.
  • Parameters:
    • user_id: A string indicating the user’s ID. (required)
    • node_id: A string specifying the node ID. (required)
    • org_id: A string specifying the organization. (required)
    • flow_id: A string representing the flow ID. (required)
  • Body:
    • file: An UploadFile object for the file to be uploaded.
  • Headers:
    • Authorization: The private token of your account (passed with ‘Bearer ’).
  • Returns: The status or result of the upload operation.

2. Delete a file

  • Method: DELETE
  • Path: https://api.stack-ai.com/documents/{org_id}/{flow_id}/{node_id}/{user_id}?filename=filename
  • Description: This endpoint is used to delete a specific file from the user’s chat.
  • Parameters:
    • filename: A string indicating the name of the file to be deleted. (required)
    • user_id: A string indicating the user’s ID. (required)
    • node_id: A string specifying the node ID. (required)
    • org_id: A string specifying the organization. (required)
    • flow_id: A string representing the flow ID. (required)
  • Headers:
    • Authorization: The private token of your account (passed with ‘Bearer ’).
  • Returns: This endpoint does not return any content.

3. List User Files

  • Method: GET
  • Path: https://api.stack-ai.com/documents/{org_id}/{flow_id}/{node_id}/{user_id}
  • Description: Retrieves a list of files present in a specific user’s chat.
  • Parameters:
    • user_id: A string indicating the user’s ID. (required)
    • node_id: A string specifying the node ID. (required)
    • org_id: A string specifying the organization. (required)
    • flow_id: A string representing the flow ID. (required)
  • Headers:
    • Authorization: The private token of your account (passed with ‘Bearer ’).
  • Returns: A list of strings, each representing a file name in the user’s chat

Below is an example of how to call these api endpoints from python:

import requests

# Define the base URL
base_url = "https://api.stack-ai.com/"
node_id = "doc-0" # or another node id

# Function to upload a file to Supabase
def delete_file_from_user(user_id, org_id, flow_id, file=None, authorization=None):
    url = f"{base_url}/documents/{org_id}/{flow_id}/{node_id}/{user_id}"
    headers = {'Authorization': f'Bearer {authorization}'}
    files = {'file': file} if file else None
    response = requests.post(url, files=files, headers=headers)
    return response.json()

# Function to delete a file from Supabase
def delete_file_from_user(filename, user_id, org_id, flow_id, authorization=None):
    url = f"{base_url}/documents/{org_id}/{flow_id}/{node_id}/{user_id}?filename={filename}"
    headers = {'Authorization': f'Bearer {authorization}'}
    response = requests.delete(url, headers=headers)
    return response

# Function to list files in a Supabase bucket
def list_files_in_bucket_user(user_id, org, flow_id, authorization=None):
    url = f"{base_url}/documents/{org_id}/{flow_id}/{node_id}/{user_id}"
    headers = {'Authorization': f'Bearer {authorization}'}
    response = requests.get(url, headers=headers)
    return response.json()

# Example usage:
# Replace 'your_user_id', 'your_org', 'your_flow_id', 'path_to_file', and 'your_auth_token' with actual values

# Upload a file
upload_response = delete_file_from_user('your_user_id', 'your_org', 'your_flow_id', file=open('path_to_file', 'rb'), authorization='your_auth_token')

# Delete a file
delete_response = delete_file_from_user('filename.txt', 'your_user_id', 'your_org', 'your_flow_id', authorization='your_auth_token')

# List files
list_response = list_files_in_bucket_user('your_user_id', 'your_org', 'your_flow_id', authorization='your_auth_token')

print("Upload Response:", upload_response)
print("Delete Response:", delete_response)
print("List Files Response:", list_response)