Skip to main content
DocumentChunking packs double-newline-separated paragraphs toward chunk_size. It splits an oversized paragraph at sentence boundaries.
1

Create a Python file

document_chunking.py
2

Set up your virtual environment

3

Install dependencies

4

Export your OpenAI API key

Set OpenAI Key

Set your OPENAI_API_KEY as an environment variable. You can get one from OpenAI.
5

Run PgVector

6

Run the script

The example sets split_on_pages=False so PDFReader combines the pages before applying DocumentChunking. Keep the default value of True to chunk each page independently.

Chunk Size

chunk_size is a target rather than a strict ceiling. Paragraph separators, a sentence longer than the target, and overlap can produce a longer chunk. Overlap is prepended without an added separator.

Document Chunking Params

Developer Resources