Observed since 21 Oct, 10:08 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
A powerful Python library for semantic document chunking and enrichment using AI
A powerful Python library for semantic document chunking and enrichment using AI. This library provides intelligent document chunking capabilities with various strategies to split text while preserving semantic meaning, particularly useful for processing markdown documentation.
Multiple chunking strategies:
Key Benefits:
Uses a hierarchical approach to split documents at natural breakpoints while maintaining context. It works by recursively splitting text using a list of separators in order of priority (headings, paragraphs, sentences, words).
Uses embeddings to find natural semantic breakpoints in text, creating chunks that preserve meaning regardless of formatting or explicit structure markers.
Processes documents by first identifying logical sections, then creating semantically meaningful chunks within each section. Enriches chunks with section and document summaries, providing rich context.
The most advanced chunker that uses LLMs to intelligently analyze document structure, creating optimal chunks with rich metadata and summaries.
pip install ai-chunking
To use the semantic chunking capabilities, you need to set up the following environment variables:
# Required for semantic chunkers (SemanticTextChunker, SectionBasedSemanticChunker, AutoAIChunker)
export OPENAI_API_KEY=your_openai_api_key_here
You can also set these environment variables in your Python code:
import os
os.environ["OPENAI_API_KEY"] = "your_openai_api_key_here"
Each chunker type supports processing multiple documents together:
from ai_chunking import RecursiveTextSplitter
chunker = RecursiveTextSplitter(
chunk_size=1000,
chunk_overlap=100
)
# List of markdown files to process
files = ['document1.md', 'document2.md', 'document3.md']
# Process all documents at once
all_chunks = chunker.chunk_documents(files)
print(f"Processed {len(files)} documents into {len(all_chunks)} chunks")
from ai_chunking import SectionBasedSemanticChunker, SemanticTextChunker
# Choose the chunker based on your needs
chunker = SemanticTextChunker(
chunk_size=1000,
similarity_threshold=0.85
)
# Or use the more advanced section-based chunker
# chunker = SectionBasedSemanticChunker(
# min_chunk_size=100,
# max_chunk_size=1000
# )
# List of files to process
markdown_files = ['user_guide.md', 'api_reference.md', 'tutorials.md']
# Process all documents, getting enriched chunks with metadata
all_chunks = chunker.chunk_documents(markdown_files)
# Access metadata for each chunk
for i, chunk in enumerate(all_chunks):
print(f"Chunk {i}:")
print(f" Source: {chunk.metadata.get('filename')}")
print(f" Tokens: {chunk.metadata.get('tokens_count')}")
if 'summary' in chunk.metadata:
print(f" Summary: {chunk.metadata.get('summary')}")
print(f" Text snippet: {chunk.text[:100]}...\n")
You can create your own markdown-specific chunking strategy:
from ai_chunking import BaseChunker
import re
class MarkdownChunker(BaseChunker):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.heading_pattern = re.compile(r'^#{1,6}\s+', re.MULTILINE)
def chunk_documents(self, files: list[str]) -> list[str]:
chunks = []
for file in files:
with open(file, 'r') as f:
content = f.read()
# Split on markdown headings while preserving them
sections = self.heading_pattern.split(content)
# Remove empty sections and trim whitespace
chunks.extend([section.strip() for section in sections if section.strip()])
return chunks
# Usage
chunker = MarkdownChunker()
chunks = chunker.chunk_documents(['README.md', 'CONTRIBUTING.md'])
Each chunker accepts different configuration parameters:
chunk_size: Maximum size of each chunk (default: 500)chunk_overlap: Number of characters to overlap between chunks (default: 50)separators: List of separators to use for splitting (default: ["\n\n", "\n", " ", ""])chunk_size: Maximum chunk size in tokens (default: 1024)min_chunk_size: Minimum chunk size in tokens (default: 128)similarity_threshold: Threshold for semantic similarity when determining breakpoints (default: 0.9)The chunker uses OpenAI's text-embedding-3-small model for embeddings to determine semantic breakpoints in the text. If chunks are too large, they are further split using recursive semantic chunking. If chunks are too small, they are merged with adjacent chunks when possible.
Each chunk includes metadata:
min_section_chunk_size: Minimum size for section chunks in tokens (default: 5000)max_section_chunk_size: Maximum size for section chunks in tokens (default: 50000)min_chunk_size: Minimum size for individual chunks in tokens (default: 128)max_chunk_size: Maximum size for individual chunks in tokens (default: 1000)page_content_similarity_threshold: Threshold for content similarity between pages (default: 0.8)The chunker uses OpenAI's text-embedding-3-small model for embeddings with dynamic threshold settings:
Each chunk includes rich metadata:
We welcome contributions! Please follow these steps:
For more details, see our Contributing Guidelines.
This project is licensed under the MIT License - see the LICENSE file for details.
If you use this software in your research, please cite:
@software{ai_chunking2024,
title = {AI Chunking: A Python Library for Semantic Document Processing},
author = {Desai, Amey},
year = {2024},
publisher = {GitHub},
url = {https://github.com/nexla-opensource/ai-chunking}
}
Download counts and popularity will appear here as the collector receives them.
| Version | Published | Direct dependencies | Open version |
|---|---|---|---|
| 0.1.9 | 19 Mar 2025 | 19 |
This describes our collected data; it is not a security assessment.
Shared repository data · GitHub
Observed since 21 Oct, 10:08 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package:c0e06a9c631acdbc1e6e268292a0b727b4aea61efd3904d339868a692362fd28