LLM Text Splitter v0.2.0
A lightweight, rule-based text splitter designed for preparing long documents for Large Language Model (LLM) context windows. It intelligently breaks down text into manageable chunks, prioritizing meaningful structural breaks (like paragraphs or lines) before resorting to arbitrary character limits.
Key Features
- All-in-One Installation: Handles
.pdf,.docx,.html, and plain text files out-of-the-box with a single installation. - Rich Metadata: Each chunk is returned as a dictionary containing the text content and its metadata (e.g., source filename, path, chunk index), which is crucial for RAG (Retrieval-Augmented Generation) and source tracking.
- Robust Recursive Splitting: Employs a powerful recursive splitting strategy that prioritizes semantic boundaries (paragraphs, then lines, then sentences) before falling back to character splits.
- Configurable Overlap: Maintains context across hard splits with configurable character overlap.
- Modular & Extensible: Built with a clean
readersarchitecture, making it easy to add support for new file types in the future.
Installation
You can install llm-text-splitter using pip:
pip install llm-text-splitter
Usage
Here's how to use the LLMTextSplitter in your Python projects:
- Splitting a File
from llm_text_splitter import LLMTextSplitter
# Assume you have 'my_report.pdf' and 'my_notes.txt'
# Initialize the splitter with a target chunk size and overlap
splitter = LLMTextSplitter(max_chunk_chars=1000, overlap_chars=100)
try:
# Process a PDF file
pdf_chunks = splitter.split_file("my_report.pdf")
print(f"Split 'my_report.pdf' into {len(pdf_chunks)} chunks.")
# Each chunk is a dictionary with 'content' and 'metadata'
print("\n--- First PDF Chunk ---")
print("Content:", pdf_chunks[0]['content'][:200] + "...") # Print first 200 chars
print("Metadata:", pdf_chunks[0]['metadata'])
print("\n" + "="*50 + "\n")
# Process a plain text file
txt_chunks = splitter.split_file("my_notes.txt")
print(f"Split 'my_notes.txt' into {len(txt_chunks)} chunks.")
print("\n--- First TXT Chunk ---")
print("Content:", txt_chunks[0]['content'])
print("Metadata:", txt_chunks[0]['metadata'])
except FileNotFoundError as e:
print(e)
except Exception as e:
print(f"An error occurred: {e}")
- Splitting a Raw Text String Use split_text if you already have your text content in a string variable.
from llm_text_splitter import LLMTextSplitter
long_text = "This is the first paragraph. It contains multiple sentences.\n\nThis is the second paragraph. It is also quite long and will be chunked according to the recursive splitting rules to maintain semantic meaning where possible."
# Initialize splitter with a small chunk size for demonstration
splitter = LLMTextSplitter(max_chunk_chars=100, overlap_chars=15)
# Split the text string
chunks = splitter.split_text(long_text, base_metadata={"source": "manual_input"})
print(f"Split text into {len(chunks)} chunks:\n")
for chunk in chunks:
print(f"Content: {chunk['content']}")
print(f"Metadata: {chunk['metadata']}")
print("-" * 20)
Example Output:
Split text into 2 chunks:
Content: This is the first paragraph. It contains multiple sentences.
Metadata: {'source': 'manual_input', 'chunk_index': 0}
--------------------
Content: This is the second paragraph. It is also quite long and will be chunked according to the recursive splitting rules to maintain semantic meaning where possible.
Metadata: {'source': 'manual_input', 'chunk_index': 1}
--------------------
Metadata
Release files for llm-text-splitter 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| llm_text_splitter-0.2.0.tar.gz | 8.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| llm_text_splitter-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.3 kB
Release files / llm_text_splitter-0.2.0.tar.gz
| Download URL | llm_text_splitter-0.2.0.tar.gz |
|---|---|
| Size | 8.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5f5e9d9f648478fc18bc0fb94ddcc7014f0683c27d5a2288c25af1de4c7b62b7
|
|
BLAKE2b-256 checksum How to use checksums |
f1af778f6d18a2a5cef2379542ee05f30b5d49e9c21d51282b07b1f271b61e7b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|
Release files / llm_text_splitter-0.2.0-py3-none-any.whl
| Download URL | llm_text_splitter-0.2.0-py3-none-any.whl |
|---|---|
| Size | 7.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
04cba0257ca0370fc0334f9062a4cf69d8b3ef0b9d3c103b7260a8fc49d27a9d
|
|
BLAKE2b-256 checksum How to use checksums |
e1a1d23c7bf21de67da3e2a099a76ff07ba52559934977988b46477663a0a639
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|