Documents

Documents allow you to analyze your own data using Alfa workplans. This guide explains how to upload documents, list them, and reference them in your workplan prompts.

Uploading documents

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

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_documents(file_paths):
9 """
10 Upload one or more documents for use with workplans.
11
12 Args:
13 file_paths (list): List of paths to files you want to upload
14
15 Returns:
16 dict: Response with uploaded document details
17 """
18 url = f"{BASE_URL}/v2/documents/add-documents"
19
20 # Prepare files for multipart upload
21 files = []
22 for path in file_paths:
23 file_name = path.split("/")[-1]
24 files.append(("files", (file_name, open(path, "rb"))))
25
26 # Make the request without Content-Type header (it's set automatically)
27 response = requests.post(
28 url,
29 headers={"x-api-key": API_KEY},
30 files=files
31 )
32
33 # Close all file handles
34 for _, (_, file_obj) in files:
35 file_obj.close()
36
37 if response.status_code == 202:
38 return response.json()
39 else:
40 print(f"Error uploading documents: {response.status_code}, {response.text}")
41 return None
42
43# Example usage
44file_paths = [
45 "/path/to/quarterly_report.pdf",
46 "/path/to/market_analysis.docx"
47]
48
49upload_result = upload_documents(file_paths)
50
51if upload_result and upload_result.get("success"):
52 print(f"Successfully uploaded {len(upload_result.get('added_listings', []))} documents")
53 for doc in upload_result.get("added_listings", []):
54 print(f"Document ID: {doc['file_id']}")
55 print(f"Name: {doc['name']}")
56 print(f"Size: {doc['size']} bytes")
57 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_documents(quarterly_reports)
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 documents

To view all documents that you’ve uploaded:

1def list_documents():
2 """
3 List all documents in your workspace.
4
5 Returns:
6 list: List of document objects with their details
7 """
8 url = f"{BASE_URL}/v2/documents/list_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 documents
20documents = list_documents()
21
22print(f"Found {len(documents)} documents:")
23for doc in documents:
24 print(f"ID: {doc['file_id']}")
25 print(f"Name: {doc['name']}")
26 print(f"Type: {doc['type']}")
27 print(f"Upload time: {doc['upload_time']}")
28 print("---")

Creating workplans with documents

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

1def create_workplan_with_document(prompt, document_id):
2 """
3 Create an workplan that references a document.
4
5 Args:
6 prompt (str): The prompt with {doc} placeholder
7 document_id (str): The ID of the document to reference
8
9 Returns:
10 str: The workplan ID if successful, empty string otherwise
11 """
12 url = f"{BASE_URL}/v2/workplans/create
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 workplan_id = data.get("workplan_id")
30 print(f"workplan created with ID: {workplan_id}")
31 return workplan_id
32 else:
33 print(f"Error creating workplan: {response.status_code}, {response.text}")
34 return ""
35
36# Example: Create an workplan to analyze a quarterly report
37document_id = "6fcc0ae7-809b-498c-b35b-4ced1114438b" # ID from a previous upload
38prompt = "Read the document {doc} and summarize its key points. Format the summary to be easy to read."
39
40workplan_id = create_workplan_with_document(prompt, document_id)

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

Understanding the document reference format

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

1{
2 "prompt": "Read the 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