Skip to content

Managing uploaded files in knowledge models

To manage documents uploaded to the knowledge model:

Define the constants:

python
import requests

API_KEY = '{YOUR_API_KEY}'
API_URL = 'https://constructor.app/api/platform-kmapi/v1'

Define the headers:

python
# Define the headers 
headers = {
    'X-KM-AccessKey': f'Bearer {API_KEY}'
}

List the files in the model:

python
def list_documents():
    response = requests.get(f'{API_URL}/knowledge-models/{KM_ID}/files', headers=headers)
    for doc in response.json()['results']:
        print(f'{doc["filename"]}, {doc["in_use"]}, {doc["indexing_status"]}, {doc["id"]}')

Upload a new file:

python
def upload_file(file_path):
    # Prepare the file for uploading
    files = {
        'file': open(file_path, 'rb')
    }

    # Make the request to upload the file
    response = requests.post(
        f'{API_URL}/knowledge-models/{KM_ID}/files',
        headers=headers,
        files=files
    )

    # Check the response from the API
    if response.status_code == 200:
        print("File uploaded successfully:", response.json())
    else:
        print("Failed to upload file. Status code:", response.status_code)
        print("Response:", response.json())
python
upload_file('./data.pdf')

List all the files in the knowledge model:

python
list_documents()
/data.pdf, True, done, 64daa9b83c0b41d38b2f92367b6020ff 
/9133.pdf, True, done, 983e6fe0381745d5904da285334c0df6 
/15870.pdf, True, done, 23e0b40f57234fc099c0e9505fd89544 
/about-form-1040-schedule-a.pdf, True, done, 429fc223bbbe435781537c64754b171f 
/adobe-acrobat-to-pdffiller-why-customers-switch-brands.pdf, True, done, ad4367ca06e44a038080efb7c91386c9 
/about-irs-form-w9.pdf, True, done, 10bd09cf22ec4a02bd3a635b438f3b0e 
/Can I access My Account from several computers.pdf, True, done, 447183737ed64fab946530c78d45a0d4 
/Can I correct a document.pdf, True, done, d3b9e56266ec46e0926e95881024d8c1 
/Can I convert a Microsoft Word doc document to PDF using pdfFiller.pdf, True, done, 91c1d452c210455482691b74ea542bbe 
/Can I convert my document to a PDF.pdf, True, done, d1385eff2fac45338fbb3495ea19a710 
/Can I edit a document.pdf, True, done, 73d96d02abcc4376bec988f96ae3efa6 
/Can I change a document.pdf, True, done, 6b2464c12aad4ae48facbea19dbe56aa 
/Can I alter a document.pdf, True, done, b376feb187af417d98b04bd137a0113c 
/Can I email documents to someone else after I finish editing.pdf, True, done, f6afdc2a28cb4b4cbd13a6688fa515a6 
/Besides using the Uploader, are there other ways to add documents in PDF to my account.pdf, True, done, 361e62829d57411e940d3a184b5c78cf 
/Can I convert a Microsoft PowerPoint ppt document to PDF using pdfFiller.pdf, True, done, e95a2d6d9e244e25b162ad0f80577cd5 
/Can I convert a Microsoft Excel xls or xlsx document to PDF using pdfFiller.pdf, True, done, e3368fc9af054a829d9338f4da11ddc9 
/Can I amend a document.pdf, True, done, 735a4c65282d47e4adb809db1a9c8b4a

Change the status of a file:

python
def update_file(file_id, tags=None, in_use=None):
    data = {
        "tags": tags,
        "in_use": in_use
    }

    response = requests.patch(
        f'{API_URL}/knowledge-models/{KM_ID}/files/{file_id}/metadata',
        headers=headers,
        json=data
    )

    return response.json()
python
print(update_file("983e6fe0381745d5904da285334c0df6", in_use="False"))
json
{
  'id': '983e6fe0381745d5904da285334c0df6',
  'tags': [],
  'filename': '/9133.pdf',
  'in_use': False,
  'indexing_status': 'done',
  'created_at': '2024-07-02T12:22:29.462032Z'
}