Skip to main content

An Semantic Chunking API for documents using OpenAI

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.1.tar.gz (13.4 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.1-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: chunktopus-0.1.1.tar.gz
  • Upload date:
  • Size: 13.4 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.1.tar.gz
Algorithm Hash digest
SHA256 ea66202f42fba171c4e8d06fbe71da092c453b1d3225c2bfee8443209ee68d59
MD5 f319f065085e3eb58f4412d88d18b857
BLAKE2b-256 0f6722f4d4387fa4845180f0ca4e23fcd984cc8873d5184c5cbcb27254447ca8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: chunktopus-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.1 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 18c999a50bcbe3b8eaad38477468c20c25f3f6477e5911b32cea3c14d86bc2a6
MD5 1a3401698d3ad1076b331f2bf230c6e3
BLAKE2b-256 3df1db96670a337165015ffc2ff5b2c87063db09a522d68baa803b133567b058

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