Semantic-aware chunking and clustering for LLM and RAG pipelines.
Project description
Semantic Chunker
Semantic Chunker is a lightweight Python package for semantically-aware chunking and clustering of text. It’s designed to support retrieval-augmented generation (RAG), LLM pipelines, and knowledge processing workflows by intelligently grouping related ideas.
Traditional chunking methods for LLM pipelines often rely on fixed-size windows or naive text boundaries, which can fragment meaning, split up related ideas, or fail to capture contextual coherence. This leads to inefficient retrieval, diluted embeddings, and suboptimal responses in RAG systems. Semantic Chunker addresses this by analyzing the meaning of each chunk — using sentence embeddings and clustering — to merge semantically similar chunks into more coherent units. The result is smarter preprocessing: fewer, denser, and more relevant chunks that enhance both the performance and interpretability of your downstream models.
Features
- Embedding-based chunk similarity (via Sentence Transformers)
- Token-aware merging with real model tokenizers
- Clustered chunk merging for optimized RAG inputs
- Preserves chunk metadata through merging
- Visual tools: attention heatmaps, semantic graphs, cluster previews
- Export options: JSON, Markdown, CSV
- CLI Interface for scripting and automation
- Debug mode with embeddings, similarity matrix, semantic pairs
Installation
pip install advanced-chunker
Quick Start
from semantic_chunker.core import SemanticChunker
chunks = [
{"text": "Artificial intelligence is a growing field."},
{"text": "Machine learning is a subset of AI."},
{"text": "Photosynthesis occurs in plants."},
{"text": "Deep learning uses neural networks."},
{"text": "Plants convert sunlight into energy."},
]
chunker = SemanticChunker(max_tokens=512)
merged_chunks = chunker.chunk(chunks)
for i, merged in enumerate(merged_chunks):
print(f"Chunk {i}:")
print(merged["text"])
print()
# Response
Merged Chunk 1
Text: Artificial intelligence is a growing field. Machine learning is a subset of AI. Deep learning uses n...
Metadata: [{'text': 'Artificial intelligence is a growing field.'}, {'text': 'Machine learning is a subset of AI.'}, {'text': 'Deep learning uses neural networks.'}]
Merged Chunk 2
Text: Photosynthesis occurs in plants. Plants convert sunlight into energy....
Metadata: [{'text': 'Photosynthesis occurs in plants.'}, {'text': 'Plants convert sunlight into energy.'}]
Debugging & Visualization
from semantic_chunker.visualization import plot_attention_matrix, plot_semantic_graph, preview_clusters
chunker = SemanticChunker(max_tokens=512)
debug = chunker.get_debug_info(chunks)
preview_clusters(debug["original_chunks"], debug["clusters"])
plot_attention_matrix(debug["similarity_matrix"], debug["clusters"])
plot_semantic_graph(debug["original_chunks"], debug["semantic_pairs"], debug["clusters"])
CLI Usage
Merge chunks semantically:
chunker chunk \
--chunks path/to/chunks.json \
--threshold 0.5 \
--similarity-threshold 0.4 \
--max-tokens 512 \
--preview \
--visualize \
--export \
--export-path output/merged \
--export-format json
Exports
Export clustered or merged chunks to:
.json: for ML/data pipelines.md: for human-readable inspection.csv: for spreadsheets or BI tools
Parameter Guide
| Argument | Description |
|---|---|
--chunks |
Path to a JSON list of text chunks (each as a dict with a text field) |
--threshold |
Agglomerative clustering distance threshold (controls granularity of clusters) |
--similarity-threshold |
Minimum cosine similarity required for two chunks to be considered semantically linked |
--max-tokens |
Maximum number of tokens allowed per merged chunk |
--preview |
Print out samples of clusters to the console |
--visualize |
Show attention matrix heatmap plot of inter-chunk similarity |
--export |
Enable file export of merged or clustered chunks |
--export-path |
Output file path prefix (e.g. output/merged) |
--export-format |
File format for export (json, csv, or md) |
Glossary
| Term | Meaning |
|---|---|
| Threshold | Controls how strictly the clustering groups similar chunks. Lower = more granular clusters. |
| Similarity Threshold | Sets the minimum cosine similarity to form a semantic pair between two chunks. |
| Semantic Pairs | Pairs of chunks with high similarity; used for graph-based visualizations. |
| Attention Matrix | The full NxN matrix of similarities between every chunk embedding. |
| Merged Chunk | The final recombined text after semantic grouping. |
| Cluster | A group of semantically similar chunks found by the algorithm. |
Architecture
Chunks → Embeddings → Cosine Similarity → Clustering → Merging
↓
Semantic Pairs (Optional)
↓
Visualization & Export (Optional)
Integrations
LangChain
Use SemanticChunkerSplitter to replace standard splitters:
from langchain_core.documents import Document
from semantic_chunker.integrations.langchain import SemanticChunkerSplitter
splitter = SemanticChunkerSplitter(cluster_threshold=0.4, return_merged=True)
docs = [Document(page_content="Some long technical text here", metadata={"source": "report.pdf"})]
split = splitter.split_documents(docs)
Testing
pytest tests/
Contributing
Pull requests are welcome! Please open an issue first if you'd like to add a feature or fix a bug.
License
MIT License. See LICENSE for details.
Acknowledgements
- Sentence Transformers
- LangChain
- scikit-learn
- Hugging Face ecosystem
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file advanced_chunker-0.1.4.tar.gz.
File metadata
- Download URL: advanced_chunker-0.1.4.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fbf46dc29cda5fa86294f670e74ee289428d91bae0d41fb37083e116dcd0bbf8
|
|
| MD5 |
13e9cdd9da3073173bd4f0f9de541d0c
|
|
| BLAKE2b-256 |
b9f5a97bae4ba88dc640ae53c53aae61ba817c8abeb89f4a17680cd20412fc30
|
File details
Details for the file advanced_chunker-0.1.4-py3-none-any.whl.
File metadata
- Download URL: advanced_chunker-0.1.4-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
49b7eaea16a70c60e06e00c4dc1ed9543e7eafeca9a4f3dc87a7b9baa5f3135b
|
|
| MD5 |
b5e6cd383e5e85ea73b375546602e740
|
|
| BLAKE2b-256 |
64bfacaa9762db8f33520413c22a7ff8fab04f0c90c41399c261e513082fb46f
|