pageindex-js
v0.0.8
Published
A Javascript/Typescript client for PageIndex
Downloads
673
Readme
PageIndex TypeScript SDK
A fully typed TypeScript client for the PageIndex API. This SDK allows you to interact with PageIndex for document submission, OCR, tree generation, retrieval, and chat completions in a type-safe manner.
Table of Contents
Installation
npm install pageindex-js
# or
yarn add pageindex-jsInclude the SDK in your project:
import { PageIndexClient } from 'pageindex-js';Initialization
import { PageIndexClient } from './pageindex-js';
const client = new PageIndexClient('YOUR_API_KEY');Document Submission
Upload a PDF document for processing:
const result = await client.submitDocument('./example.pdf');
console.log(result.doc_id);OCR
Get OCR processing results:
const ocrResult = await client.getOcr('doc_id_here', 'page'); // 'page' or 'node'
console.log(ocrResult);Tree Generation
Retrieve the document tree structure:
const tree = await client.getTree('doc_id_here', true); // true to include node summaries
console.log(tree);Check if the document is ready for retrieval:
const ready = await client.isRetrievalReady('doc_id_here');
console.log(ready); // true or falseRetrieval
Submit a query against a document:
const retrieval = await client.submitQuery('doc_id_here', 'What is the main topic?', true);
console.log(retrieval.retrieval_id);Get retrieval results:
const retrievalResult = await client.getRetrieval('retrieval_id_here');
console.log(retrievalResult);Chat Completions
Generate a chat completion, optionally scoped to one or more documents:
const messages = [
{ role: 'user', content: 'Summarize the document.' }
];
const completion = await client.chatCompletions({messages: [], stream: false, doc_id: 'doc_id_here'});
console.log(completion);Streaming responses:
for await (const chunk of client.chatCompletions({messages: [], stream: true, doc_id: 'doc_id_here'})) {
console.log(chunk);
}Document Management
Get metadata for a document:
const doc = await client.getDocument('doc_id_here');
console.log(doc);Delete a document:
await client.deleteDocument('doc_id_here');List all documents with pagination:
const docs = await client.listDocuments(50, 0);
console.log(docs.documents);Error Handling
All API errors throw a PageIndexAPIError:
try {
await client.getDocument('invalid_id');
} catch (err) {
if (err instanceof PageIndexAPIError) {
console.error('API error:', err.message);
}
}