Skip to main content

A lightweight, rule-based text splitter for LLM context window management.

Project description

LLM Text Splitter

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.

Features

  • Context-Aware Splitting: Prioritizes splitting by natural document structures (paragraphs, lines) to maintain semantic coherence.
  • Arbitrary Fallback: If natural units are too large, it gracefully falls back to character-based splitting.
  • Configurable Overlap: Allows for character overlap between arbitrarily split sub-chunks to preserve context.
  • Multiple Strategies: Supports 'paragraphs', 'lines', and 'characters' splitting strategies.
  • Lightweight: No external NLP dependencies, relying solely on Python's standard library.

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:

from llm_text_splitter import LLMTextSplitter

long_document = """
## Introduction to Large Language Models

Large Language Models (LLMs) are a type of artificial intelligence model that have been trained on vast amounts of text data. They are capable of understanding and generating human-like text, making them incredibly versatile for a wide range of applications, from content creation to customer service.

## How LLMs Work

LLMs operate on a transformer architecture, which allows them to process words in relation to all other words in a sequence, rather than one by one. This enables them to grasp context and nuances in language far better than previous models. Training involves predicting the next word in a sentence, which helps them learn grammar, facts, and even some reasoning abilities.

This is a very long paragraph that might exceed the chunk limit on its own. It discusses the various applications of LLMs, including but not limited to, summarization, translation, code generation, creative writing, and question answering. The ability of these models to adapt to different tasks with minimal fine-tuning is what makes them so powerful and revolutionary in the field of AI. Their impact is being felt across industries, transforming how businesses interact with data and customers.

## Challenges and Future

Despite their power, LLMs face challenges such as computational cost, ethical considerations (bias, misinformation), and the need for significant data. Future developments aim to address these, focusing on efficiency, interpretability, and safety.

Another short paragraph.
"""

# --- Example 1: Splitting by Paragraphs (default strategy) ---
# Initialize splitter with a typical LLM context window size (e.g., 500 characters for demo)
# and a small overlap for arbitrary splits.
splitter_para = LLMTextSplitter(max_chunk_chars=500, overlap_chars=50)

print("--- Example 1: Splitting by Paragraphs (default strategy) ---")
print("This strategy attempts to keep paragraphs intact. If a paragraph is too long,")
print("it will then try to split it by lines, and finally by characters.\n")

chunks_by_paragraph = splitter_para.split_document(long_document, strategy="paragraphs")
for i, chunk in enumerate(chunks_by_paragraph):
    print(f"Chunk {i+1} (Length: {len(chunk)} chars):\n---\n{chunk}\n---")
    if i < len(chunks_by_paragraph) - 1:
        print("\n" + "="*40 + "\n") # Separator for readability

print("\n\n" + "#"*60 + "\n\n") # Major section separator

# --- Example 2: Splitting by Lines ---
splitter_lines = LLMTextSplitter(max_chunk_chars=200, overlap_chars=20)

print("--- Example 2: Splitting by Lines ---")
print("This strategy prioritizes splitting by newlines. If a line is too long,")
print("it will then split it arbitrarily by characters.\n")

chunks_by_line = splitter_lines.split_document(long_document, strategy="lines")
for i, chunk in enumerate(chunks_by_line):
    print(f"Chunk {i+1} (Length: {len(chunk)} chars):\n---\n{chunk}\n---")
    if i < len(chunks_by_line) - 1:
        print("\n" + "="*40 + "\n")

print("\n\n" + "#"*60 + "\n\n") # Major section separator

# --- Example 3: Splitting by Characters (arbitrary splitting) ---
splitter_chars = LLMTextSplitter(max_chunk_chars=100, overlap_chars=10)

print("--- Example 3: Splitting by Characters (arbitrary splitting) ---")
print("This strategy performs purely character-based splitting with overlap.")
print("Useful when no natural breaks are desired or available.\n")

chunks_by_char = splitter_chars.split_document(long_document, strategy="characters")
for i, chunk in enumerate(chunks_by_char):
    print(f"Chunk {i+1} (Length: {len(chunk)} chars):\n---\n{chunk}\n---")
    if i < len(chunks_by_char) - 1:
        print("\n" + "="*40 + "\n")

print("\n\n" + "#"*60 + "\n\n") # Major section separator

# --- Example 4: Handling a very long single line/paragraph with forced arbitrary splits ---
print("--- Example 4: Handling a very long single line/paragraph with forced arbitrary splits ---")
print("This demonstrates how the splitter breaks down content that lacks natural separators,")
print("ensuring chunks respect `max_chunk_chars` while using `overlap_chars`.\n")

very_long_line = "This is an extremely long sentence that will definitely exceed the chunk limit. It's designed to show how the splitter handles text that cannot be broken down by natural separators like paragraphs or short lines. The goal is to ensure no part of the text is lost, even if it means splitting mid-sentence. This is crucial for maintaining data integrity when feeding large documents into LLMs with strict context window constraints." * 3
splitter_long = LLMTextSplitter(max_chunk_chars=150, overlap_chars=20)
chunks_long_line = splitter_long.split_document(very_long_line, strategy="characters") # 'characters' best illustrates arbitrary split
for i, chunk in enumerate(chunks_long_line):
    print(f"Chunk {i+1} (Length: {len(chunk)} chars):\n---\n{chunk}\n---")
    if i < len(chunks_long_line) - 1:
        print("\n" + "="*40 + "\n")

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

llm_text_splitter-0.1.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_text_splitter-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file llm_text_splitter-0.1.0.tar.gz.

File metadata

  • Download URL: llm_text_splitter-0.1.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for llm_text_splitter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e2dca3334a9165b85465f96b3bc78f994544a8a6b28ec85a0e3f45adadaaad44
MD5 f53394ead0e40e14e6a3f9d6125888fc
BLAKE2b-256 ccb65c310f30af5fe6bcd6a0e1fe7067e9e2ebd445080a5c6a12ce541c506be0

See more details on using hashes here.

File details

Details for the file llm_text_splitter-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for llm_text_splitter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dc4044f57a45dfd825697bdaee5be0004d49335299e3e63def44f0af7ad20253
MD5 f311e5a1b7debd2bcd0d4fe8a5c4e305
BLAKE2b-256 066a5f66ae5a313ddc265da8a076caf104776386328e90b4d95336ca24683017

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page