Skip to main content

A multithreaded OCR API for document processing and chunking with simple interface

Project description

Chunktopus

A powerful document processing and chunking API by Unsiloed AI, featuring multithreaded OCR and intelligent document chunking using OpenAI's Vision models.

Installation

pip install chunktopus

Features

  • Process PDF, DOCX, and PPTX documents
  • Multiple chunking strategies:
    • Fixed size chunks
    • Semantic chunks (using OpenAI)
    • Page-based chunks
    • Paragraph-based chunks
    • Heading-based chunks
  • Fast parallel processing
  • RESTful API with FastAPI
  • Simple JavaScript-like API

Configuration

OpenAI API Key

Important: This package requires your own OpenAI API key. You must provide your API key in one of the following ways:

  1. Set it as an environment variable:
export OPENAI_API_KEY="your-api-key"
  1. Create a .env file in your project directory:
OPENAI_API_KEY=your-api-key
  1. Set it programmatically:
import os
os.environ["OPENAI_API_KEY"] = "your-api-key"
  1. Provide it directly in the API call (recommended for the simple API)

Usage

Using the Simple API (Recommended)

import chunktopus

# Process a document with your own OpenAI API key
result = chunktopus.process_sync({
    "filePath": "https://example.com/document.pdf",
    "credentials": {
        "apiKey": "your-openai-api-key"
    },
    # Optional parameters
    "strategy": "semantic",  # or "fixed", "page", "paragraph", "heading"
    "chunkSize": 1000,
    "overlap": 100
})

# Print the results
print(f"Number of chunks: {result['total_chunks']}")
for i, chunk in enumerate(result['chunks']):
    print(f"Chunk {i+1}: {chunk['text'][:100]}...")

# Async version is also available
import asyncio

async def main():
    result = await chunktopus.process({
        "filePath": "path/to/document.pdf",
        "credentials": {
            "apiKey": "your-openai-api-key"
        }
    })
    print(f"Number of chunks: {result['total_chunks']}")

asyncio.run(main())

Using as a Python Package (Legacy)

from chunktopus import chunktopus

# Process a document with your own OpenAI API key
result = chunktopus(
    file_path="path/to/document.pdf",
    credentials={"api_key": "your-openai-api-key"}
)

# Or process a document from a URL
result = chunktopus(
    file_path="https://example.com/document.pdf",
    credentials={"api_key": "your-openai-api-key"}
)

# Or process raw text
result = chunktopus(
    text="Your text content to chunk semantically...",
    credentials={"api_key": "your-openai-api-key"}
)

# Print the results
for i, chunk in enumerate(result):
    print(f"Chunk {i+1}: {chunk['text'][:100]}...")

Using from Command Line

After installation, you can use the chunktopus command:

# Process a file with your OpenAI API key
chunktopus path/to/document.pdf --api-key "your-openai-api-key"

# Output to a file
chunktopus path/to/document.pdf --api-key "your-openai-api-key" --output results.json

# Format as text instead of JSON
chunktopus path/to/document.pdf --api-key "your-openai-api-key" --format text

Running as a REST API

from chunktopus.server import run_server

# Start the server on port 8000
run_server(port=8000)

Using the document processing functions directly

from chunktopus.services import process_document_chunking
from chunktopus.utils.chunking import ChunkingStrategy

# Set your OpenAI API key
import os
os.environ["OPENAI_API_KEY"] = "your-api-key"

# Process a document with semantic chunking
result = process_document_chunking(
    file_path="path/to/document.pdf",
    file_type="pdf",
    strategy=ChunkingStrategy.SEMANTIC,
)

# Print the results
print(f"Number of chunks: {result['total_chunks']}")
for i, chunk in enumerate(result['chunks']):
    print(f"Chunk {i+1}: {chunk['text'][:100]}...")

API Endpoints

/chunking

POST endpoint that processes a document and returns chunks.

Parameters:

  • document_file: The document file (PDF, DOCX, PPTX)
  • strategy: Chunking strategy (semantic, fixed, page, paragraph, heading)
  • chunk_size: Size of chunks for fixed strategy (default: 1000)
  • overlap: Overlap size for fixed strategy (default: 100)

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chunktopus-0.1.2.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chunktopus-0.1.2-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file chunktopus-0.1.2.tar.gz.

File metadata

  • Download URL: chunktopus-0.1.2.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for chunktopus-0.1.2.tar.gz
Algorithm Hash digest
SHA256 195193754dec9a9a7b8f77fe31f25f9cabe125ffafcece6a3e8ee509702e544a
MD5 3aed9fdea573b5249c8514a4858f2d54
BLAKE2b-256 8156e40c5920b01ba1913b648af7c5da7a8bf25f88bb0a94f0a6e500a92fb619

See more details on using hashes here.

File details

Details for the file chunktopus-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: chunktopus-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for chunktopus-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 468bf12941e47e0018aa8d4229cfa2336f48f29b3e6766f7da943620bd64f269
MD5 b5852996accf84b5dabded67343ca3a7
BLAKE2b-256 8d5f0c0af141654cd9b579ce6da2bde9e5a1ec4602f39034d8e70b93c949c73e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page