Use Files as Inputs
Enable user file uploads for you apps and APIs.
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:
- Add a Files Input
To begin the process, you must integrate a mechanism to handle file uploads.
- Navigate to the Inputs section.
- Select the
Files
input.
- Enable User Uploads
Go to the input settings.
- Inside the Files settings, find the option labeled
expose in interface
. - Toggle or check this option to enable it.
-
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.
-
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 aForm
.
- From the builder, click on the
-
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.
- In the export configuration, navigate to the
-
Complete the Remaining Exporting Steps Follow the additional steps to export the project.
-
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
: AnUploadFile
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: