Upload Files to Contextualize Your Agent Requests
Agent Chat API
In StackSpot AI, you can upload files or images in the StackSpot AI chat to use in your interactions with Agents.
Portal
Step 1. In the StackSpot AI Portal, access the Chat;
Step 2. Click on the 'More Options' button and select Upload File;
Step 3. Choose the image or files you want to add;
Step 4. StackSpot AI will now have the context of the file you uploaded to use it in your chat.
IDE
Step 1. In the StackSpot AI IDE, access the Chat;
Step 2. Click on the 'More Options' button and select Upload File;
Step 3. Choose the image or files you want to add;
Step 4. StackSpot AI will now have the context of the file you uploaded to use it in your chat.
File Upload API (use with Agents API)
How to upload files
Prerequisites
You need to authenticate to use this feature. You have two options:
1. Authenticate with a Service Credential
-
Due to authentication restrictions, uploading directly via API is only available for Enterprise accounts.
-
Authenticate to get to use the script.
Follow the steps to authenticate on the Services Credential page refer to the StackSpot EDP documentation.
Activate the following permissions:
ai_devandai_admin.
Contact your account administrator if you cannot create a Service Credential.
- Copy the 'client id', 'client key', and 'realm' fields to use as environment variables (the first two should be secrets) or the curl example at the bottom of the page in the 'how to use it' section. You won't be able to see them again.
If you lose them, revoke the credential and create a new one.
- The following examples consider the access token to be set as the
JWTenvironment variable. You can extract it from the authenticationcurlwith thejqcommand to get the value for theaccess_tokenkey from the returned JSON, then save it as an environment variable using theexportcommand, such as:
export JWT=$(curl -s "https://idm.stackspot.com/$REALM/oidc/oauth/token" -H 'Content-Type: application/x-www-form-urlencoded' -d 'grant_type=client_credentials' -d "client_id=$CLIENT_ID" -d "client_secret=$CLIENT_KEY" | jq -r '.access_token')
To execute the request, add x-account-id to the headers.
Authenticate with Personal Access Token
If you have a Freemium account, use the following authentication method:
- In the StackSpot AI Portal, click your avatar and then select 'My Profile';
- In the Access Token section, click the 'Generate Client Key' button;
- Copy the Client Key.
For more details, see the Personal Access Token documentation.
How to call the API to upload a file
Example script:
file_paths=(
"/path/to/file1.pdf"
"/path/to/file2.pdf"
"/path/to/file3.pdf"
)
upload_ids=()
for file in "${file_paths[@]}"; do
upload_data=$(curl -s 'https://data-integration-api.stackspot.com/v2/file-upload/form' \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $JWT" \
-d "{\"file_name\": \"$(basename "$file")\", \"target_type\": \"CONTEXT\", \"expiration\": 60 }")
curl -s "$(jq -r '.url' <<< "$upload_data")" \
-F "key=$(jq -r '.form.key' <<< "$upload_data")" \
-F "x-amz-algorithm=$(jq -r '.form["x-amz-algorithm"]' <<< "$upload_data")" \
-F "x-amz-credential=$(jq -r '.form["x-amz-credential"]' <<< "$upload_data")" \
-F "x-amz-date=$(jq -r '.form["x-amz-date"]' <<< "$upload_data")" \
-F "x-amz-security-token=$(jq -r '.form["x-amz-security-token"]' <<< "$upload_data")" \
-F "policy=$(jq -r '.form.policy' <<< "$upload_data")" \
-F "x-amz-signature=$(jq -r '.form["x-amz-signature"]' <<< "$upload_data")" \
-F "file=@$file"
upload_ids+=("$(jq -r '.id' <<< "$upload_data")")
done
Use this script to integrate the API wherever needed, such as within a GitHub Actions workflow, Postman, etc.
Example
Here is an example of integration through the terminal:
- Open the terminal and authenticate with JWT;
- Run the command:
echo $JWT
- Execute the script provided by StackSpot AI; this example includes two images.
# Authenticate
export JWT=$(curl -s "https://idm.stackspot.com/$REALM/oidc/oauth/token" \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=client_credentials' \
-d "client_id=$CLIENT_ID" \
-d "client_secret=$CLIENT_KEY" | jq -r '.access_token')
# Upload files and keep their ids
file_paths=(
'/home/user/Pictures/StackSpot.png'
'/home/user/Pictures/StackSpot.jpg'
)
upload_ids=()
for file in "${file_paths[@]}"; do
upload_data=$(curl -s 'https://data-integration-api.stackspot.com/v2/file-upload/form' \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $JWT" \
-d "{\"file_name\": \"$(basename "$file")\", \"target_type\": \"CONTEXT\", \"expiration\": 60 }")
curl -s "$(jq -r '.url' <<< "$upload_data")" \
-F "key=$(jq -r '.form.key' <<< "$upload_data")" \
-F "x-amz-algorithm=$(jq -r '.form["x-amz-algorithm"]' <<< "$upload_data")" \
-F "x-amz-credential=$(jq -r '.form["x-amz-credential"]' <<< "$upload_data")" \
-F "x-amz-date=$(jq -r '.form["x-amz-date"]' <<< "$upload_data")" \
-F "x-amz-security-token=$(jq -r '.form["x-amz-security-token"]' <<< "$upload_data")" \
-F "policy=$(jq -r '.form.policy' <<< "$upload_data")" \
-F "x-amz-signature=$(jq -r '.form["x-amz-signature"]' <<< "$upload_data")" \
-F "file=@$file"
upload_ids+=("$(jq -r '.id' <<< "$upload_data")")
done
# Chat with this agent
curl 'https://genai-inference-app.stackspot.com/v1/agent/01JS0SSFDKPYXATPQ1J9RAW3B2/chat' \
-H 'Content-Type: application/json' \
-H "Authorization: Bearer $JWT" \
-d "{
\"streaming\": false,
\"user_prompt\": \"What information is contained in these images?\",
\"stackspot_knowledge\": false,
\"return_ks_in_response\": true,
\"upload_ids\": $(jq -c -n '$ARGS.positional' --args "${upload_ids[@]}")
}"
- Execute the following command to store the IDs in the array:
echo $upload_ids
The response will include the IDs of the files or images:
> echo $upload_ids
01JFAKEIDEXAMPLE
> echo ${upload_ids[@]}
02XFAKEIDEXAMPLE 01XFAKEIDEXAMPLE
- Use these these returned IDs (
upload_ids) to make API requests.