Here is some example Python code that uploads a file and then chats with it.
Example code:
import json
import requests
import time
class DocalysisAPI:
BASE_URL = "https://api1.docalysis.com/api/v1"
API_KEY = "REPLACE WITH YOUR API KEY"
@staticmethod
def make_request(method, endpoint, data=None):
print(f"Making Docalysis API request to {endpoint}")
headers = {
"Authorization": f"Bearer {DocalysisAPI.API_KEY}",
"Content-Type": "application/json",
}
data = json.dumps(data)
response = requests.request(method, f"{DocalysisAPI.BASE_URL}/{endpoint}", headers=headers, data=data)
if not 200 <= response.status_code < 300:
raise Exception(f'Unexpected status code: {response.status_code}')
response_json = response.json()
if not response_json.get('success', False):
error_message = response_json.get('error', 'No error message provided')
print(f"Request unsuccessful. Error: {error_message}")
return None
return response_json
@staticmethod
def upload_file_from_url(name, url):
payload = {'name': name, 'url': url}
return DocalysisAPI.make_request("POST", "files/create", payload)
@staticmethod
def upload_local_file(file_path, desired_file_name, desired_path=None):
headers = {
"Authorization": f"Bearer {DocalysisAPI.API_KEY}",
# Do not set "Content-Type" here; requests will do it for multipart.
}
with open(file_path, 'rb') as upload_this_file:
# The 'name' and 'path' are part of the form fields, not the headers
payload = {
'name': (None, desired_file_name),
'path': (None, desired_path),
}
files_data = {'file': (desired_file_name, upload_this_file, 'application/pdf')}
response = requests.post(f"{DocalysisAPI.BASE_URL}/files/create", headers=headers, files=files_data, data=payload)
if not 200 <= response.status_code < 300:
raise Exception(f'Unexpected status code: {response.status_code}')
response_json = response.json()
if not response_json.get('success', False):
error_message = response_json.get('error', 'No error message provided')
print(f"Request unsuccessful. Error: {error_message}")
return None
return response_json
@staticmethod
def get_file_info(file_id):
response = DocalysisAPI.make_request("GET", f"files/{file_id}/info")
if not response.get('success'):
raise Exception(response.get('error', "An unknown error occurred"))
return response.get("file")
@staticmethod
def chat(file_id, message):
payload = {'message': message}
return DocalysisAPI.make_request("GET", f"files/{file_id}/chat", payload)
@staticmethod
def chat_with_directory(dir_id, message):
# Function to initiate a chat about an uploaded document
payload = {'message': message}
response = DocalysisAPI.make_request("GET", f"directories/{dir_id}/chat", payload)
if response:
if response.get('success', False):
chat_response = response.get('response', 'No chat response provided')
return chat_response
else:
error_message = response.get('error', 'No error message provided')
print(f"Error occurred while processing: {error_message}")
else:
print("No response received from chat request.")
return None
@staticmethod
def wait_for_docalysis_file_ready(file_id, max_retries=50):
retries = 0
while retries < max_retries:
info = DocalysisAPI.get_file_info(file_id)
if info and info.get('processed_state', '') == 'processed':
print(f"File {file_id} is in 'processed' state and ready for chatting")
return info
time.sleep(2)
retries += 1
print(f"Waiting for file {file_id} to finish processing; retry count is {retries}")
raise Exception(f"Docalysis did not process the file in time")
if __name__ == "__main__":
# Replace these values with a URL to your PDF file and your desired uploaded name:
response_data = DocalysisAPI.upload_file_from_url('name_your_uploaded_file.pdf', 'https://docalysis.com/files/hkvy/download/Welcome_to_Docalysis.pdf?download=true')
# You can also use upload_local_file to upload a file from your local computer instead of a URL.
# response_data = DocalysisAPI.upload_local_file('/Users/me/Desktop/myfile.pdf', 'myfile.pdf')
if not response_data:
raise Exception("No response data from upload request")
file_id = response_data['file']['id']
DocalysisAPI.wait_for_docalysis_file_ready(file_id)
result = DocalysisAPI.chat(file_id, 'What is this document?')
print(result)