The files API endpoint can be used to upload a document to Paradigm to be then used in the Chat with docs application.
Prerequisites
In order to do so, here are the prerequisites:
- A Paradigm API key: if you do not have one, go to your Paradigm profile and generate a new API key.
- The desired document to upload: The file to upload cannot have a size above 100MB.
Usage methods
There are several ways to call the endpoint:
- With the OpenAI python client (recommended)
- With the python
requests
package: to avoid having the OpenAI layer - Through a curl request: good to do a quick check or a first test
OpenAI python client
Setup
Install the OpenAI python package with the following bash command:
pip install --upgrade openai
You can then setup the OpenAI python client in your script:
from openai import OpenAI as OpenAICompatibleClient
import os
# Get API key from environment
api_key = os.getenv("PARADIGM_API_KEY")
# Our base url
base_url = "https://paradigm.lighton.ai/api/v2"
# Configure the OpenAI client
client = OpenAICompatibleClient(api_key=api_key, base_url=base_url)
Here the Paradigm API key is set up in an environment variable, but you could perfectly forward it through argument parsing with argparse
.
Usage
To upload a file into Paradigm, you can use the client.files.create()
method.
An example is described below to upload the file called test_upload_file.pdf:
upload_response = client.files.create(
file=open("/path/to/test_upload_file.pdf", "rb"),
purpose="documents"
)
The given response would then follow the OpenAI format:
FileObject(
id=1,
bytes=9626,
created_at=1714656771,
filename='test_upload_file.pdf',
object='file',
purpose='documents',
status='success',
status_details=None
)
Python requests
package
You can also avoid using the OpenAI python class and directly send request to the API endpoint through the requests
package.
import requests
import os
# Get API key from environment
api_key = os.getenv("PARADIGM_API_KEY")
# Path of the file to upload
file_path = "/path/to/test_upload_file.pdf"
response = requests.request(
method="POST",
url=f"https://paradigm.lighton.ai/api/v2/files",
headers={
'Authorization': f"Bearer {api_key}"
},
data={"purpose": "documents"},
files={
"file": open(file_path, "rb"),
}
)
print(response.json())
You would then get a JSON answer as a dictionnary:
{
'id': 1,
'object': 'file',
'bytes': 9626,
'created_at': 1714659874,
'filename': 'test_upload_file.pdf',
'purpose': 'documents',
'status': 'success'
}
cURL request
If you would prefer sending a request to Paradigm with a simple cURL command, here is an example:
curl https://paradigm.lighton.ai/api/v2/files \
-H "Authorization: Bearer $PARADIGM_API_KEY" \
-F purpose="documents" \
-F file="@/path/to/test_upload_file.pdf"