Sentence Chunking

Split documents at natural sentence boundaries

Splits text at sentence boundaries while respecting token limits. Preserves natural reading flow.

Quick Start

Agent with Sentence Chunking
from praisonaiagents import Agent

agent = Agent(
    instructions="Answer questions from documents.",
    knowledge={
        "sources": ["article.pdf"],
        "chunker": {
            "type": "sentence",
            "chunk_size": 512,
            "chunk_overlap": 64
        }
    }
)

response = agent.start("What are the main arguments?")
Direct API
from praisonaiagents.knowledge.chunking import Chunking

chunker = Chunking(
    chunker_type="sentence",
    chunk_size=512,
    chunk_overlap=64
)

chunks = chunker.chunk("Your document text here. With multiple sentences.")
for chunk in chunks:
    print(chunk.text)

When to Use

Parameters

ParameterTypeDefaultDescription
chunk_sizeint512Max tokens per chunk
chunk_overlapint128Token overlap between chunks
tokenizer_or_token_counterstr"gpt2"Tokenizer for counting

Examples

News Articles

python
agent = Agent(
    instructions="Summarize news articles.",
    knowledge={
        "sources": ["news/"],
        "chunker": {
            "type": "sentence",
            "chunk_size": 256  # Short chunks for news
        }
    }
)

Long-form Content

python
agent = Agent(
    instructions="Analyze essays and papers.",
    knowledge={
        "sources": ["essays/"],
        "chunker": {
            "type": "sentence",
            "chunk_size": 1024,
            "chunk_overlap": 128
        }
    }
)

How It Works

Diagram

Document

Detect Sentences

Group by Token Limit

Chunk 1: Sent 1-3

Chunk 2: Sent 4-6

Chunk N

Sentences are grouped together until the token limit is reached, then a new chunk starts.