This guide explains how to retrieve the list of uploaded documents in Paradigm using the OpenAI python client. Ensure you have the OpenAI Python client set up as per the initial setup guide.
Step-by-Step Instructions
Step 1: Install Required Library
Ensure you have the OpenAI library installed. If not, install it using pip:
pip install --upgrade openai
Step 2: Set Up the OpenAI client
Configure your OpenAI client with the API key and set the base URL to the Paradigm API:
from openai import OpenAI as OpenAICompatibleClient
import os
# Get API key from environment
api_key = os.getenv("PARADIGM_API_KEY")
# Our API base url
base_url = "https://paradigm.lighton.ai/api/v2"
# Configure the OpenAI compatible client
client = OpenAICompatibleClient(api_key=api_key, base_url=base_url)
Step 3: Fetch Paginated List of Documents
To retrieve all the documents, use the files.list()
method of the OpenAi python object.
response = client.files.list()
Step 4: Handle the Response
The method will return a SyncPage
object containing the paginated list of documents and additional information, as described below:
SyncPage
attributes
data
: List ofFileObject
itemsobject
: constant value set to"list"
next
: url to call to get the next page content if it existsprevious
: url to call to get the previous page content if it existscount
: total number of files available through all the pages
Handle this response as needed in your application.
# Example code to print all the documents information
for document in response.data:
print(document)
Handle pagination
The number of documents per page is currently set to 20 and cannot be modified by the end user.
To get the documents of a specific page, you can add the information through the extra_query
parameter of the files.list()
method, as shown below:
# Example snippet requesting the content of the second page
response = client.files.list(
extra_query={"page": 2}
)
Filter the documents by space type
A document can be in one of the three types of space:
- Private space: the documents in this space are only accessible to the user
- Company space: the documents in this space are accessible to every member of the company
- Workspace: the documents in this type of space are accessible to the members of the given workspace, which is a sub-selection of the company members.
To filter the documents, you can add the following parameters:
# Example snippet filtering the documents on the private space and the workspace n°2
response = client.files.list(
extra_query={
"company_scope": False,
"private_scope": True,
"workspace_scope": [2],
}
)
By default, every document is returned, modify the arguments reduce the number of returned files.
Conclusion
You have successfully retrieved a paginated list of uploaded documents from Paradigm using the specified method. Adjust the page value in the extra_query
parameter to navigate through the pages and modify the scope of your returned documents with the 3 arguments: private_scope
, company_scope
and workspace_scope