Custom documents

Custom documents allow you to analyze your own data using Alfa agents. This guide explains how to upload documents, list them, and reference them in your agent prompts.

Uploading custom documents

You can upload your own documents (PDFs, Word files, Excel spreadsheets, etc.) to be processed and made available to your agents.

1import requests
2
3BASE_URL = "https://alfa.boosted.ai/client"
4API_KEY = "YOUR_API_KEY_HERE"
5
6headers = {"x-api-key": API_KEY} # No Content-Type for multipart uploads
7
8def upload_custom_documents(file_paths, base_path=""):
9 """
10 Upload one or more custom documents for use with agents.
11
12 Args:
13 file_paths (list): List of paths to files you want to upload
14 base_path (str, optional): Folder path where files should be stored
15
16 Returns:
17 dict: Response with uploaded document details
18 """
19 url = f"{BASE_URL}/custom-documents/add-documents"
20
21 # Add optional base_path parameter if provided
22 if base_path:
23 url += f"?base_path={base_path}"
24
25 # Prepare files for multipart upload
26 files = []
27 for path in file_paths:
28 file_name = path.split("/")[-1]
29 files.append(("files", (file_name, open(path, "rb"))))
30
31 # Make the request without Content-Type header (it's set automatically)
32 response = requests.post(
33 url,
34 headers={"x-api-key": API_KEY},
35 files=files
36 )
37
38 # Close all file handles
39 for _, (_, file_obj) in files:
40 file_obj.close()
41
42 if response.status_code == 202:
43 return response.json()
44 else:
45 print(f"Error uploading documents: {response.status_code}, {response.text}")
46 return None
47
48# Example usage
49file_paths = [
50 "/path/to/quarterly_report.pdf",
51 "/path/to/market_analysis.docx"
52]
53
54upload_result = upload_custom_documents(file_paths, "financial_reports")
55
56if upload_result and upload_result.get("success"):
57 print(f"Successfully uploaded {len(upload_result.get('added_listings', []))} documents")
58 for doc in upload_result.get("added_listings", []):
59 print(f"Document ID: {doc['file_id']}")
60 print(f"Name: {doc['name']}")
61 print(f"Path: {doc['full_path']}")
62 print(f"Size: {doc['size']} bytes")
63 print("---")

Example: Uploading quarterly reports

1

Prepare your documents

Gather the documents you want to analyze (PDFs, Excel files, Word documents, etc.)

2

Upload the documents

1# Upload Q1 and Q2 reports to a designated folder
2quarterly_reports = [
3 "Q1_2023_Financials.pdf",
4 "Q2_2023_Financials.pdf"
5]
6
7result = upload_custom_documents(quarterly_reports, "quarterly_reports/2023")
8
9# Store document IDs for later use
10document_ids = {}
11if result and result.get("success"):
12 for doc in result.get("added_listings", []):
13 document_ids[doc["name"]] = doc["file_id"]
14 print(f"Uploaded: {doc['name']} (ID: {doc['file_id']})")

For large documents, the upload process may take some time as the system processes and indexes the content for searching.

Listing custom documents

To view all custom documents that you’ve uploaded:

1def list_custom_documents():
2 """
3 List all custom documents in your workspace.
4
5 Returns:
6 list: List of document objects with their details
7 """
8 url = f"{BASE_URL}/custom-documents"
9
10 response = requests.get(url, headers=headers)
11
12 if response.status_code == 200:
13 data = response.json()
14 return data.get("documents", [])
15 else:
16 print(f"Error listing documents: {response.status_code}, {response.text}")
17 return []
18
19# Get and display all custom documents
20documents = list_custom_documents()
21
22print(f"Found {len(documents)} custom documents:")
23for doc in documents:
24 print(f"ID: {doc['file_id']}")
25 print(f"Name: {doc['name']}")
26 print(f"Path: {doc['full_path']}")
27 print(f"Type: {doc['type']}")
28 print(f"Upload time: {doc['upload_time']}")
29 print("---")

Creating agents with custom documents

You can reference custom documents in your agent prompts using a special syntax. The agent will then be able to read and analyze these documents.

1def create_agent_with_document(prompt, document_id):
2 """
3 Create an agent that references a custom document.
4
5 Args:
6 prompt (str): The prompt with {doc} placeholder
7 document_id (str): The ID of the custom document to reference
8
9 Returns:
10 str: The agent ID if successful, empty string otherwise
11 """
12 url = f"{BASE_URL}/agent/create-agent"
13
14 # Create the request payload with the document reference
15 payload = {
16 "prompt": prompt,
17 "args": {
18 "doc": {
19 "id": document_id,
20 "type": "custom_document"
21 }
22 }
23 }
24
25 response = requests.post(url, headers=headers, json=payload)
26
27 if response.status_code == 201:
28 data = response.json()
29 agent_id = data.get("agent_id")
30 print(f"Agent created with ID: {agent_id}")
31 return agent_id
32 else:
33 print(f"Error creating agent: {response.status_code}, {response.text}")
34 return ""
35
36# Example: Create an agent to analyze a quarterly report
37document_id = "6fcc0ae7-809b-498c-b35b-4ced1114438b" # ID from a previous upload
38prompt = "Read the custom document {doc} and summarize its key points. Format the summary to be easy to read."
39
40agent_id = create_agent_with_document(prompt, document_id)

The {doc} placeholder in your prompt will be replaced with the actual document content when the agent processes it.

Understanding the document reference format

The payload for creating an agent with a document reference follows this structure:

1{
2 "prompt": "Read the custom document {doc} and summarize its key points. Format the summary to be easy to read.",
3 "args": {
4 "doc": {
5 "id": "6fcc0ae7-809b-498c-b35b-4ced1114438b",
6 "type": "custom_document"
7 }
8 }
9}
  • prompt: Your instruction with the {doc} placeholder
  • args: A dictionary mapping placeholders to their values
  • doc: The placeholder name that matches what you used in the prompt
  • id: The unique ID of the document (obtained when uploading or listing documents)
  • type: Must be “custom_document” to indicate you’re referencing a document