Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

9.4 KB
Raw Blame
"""
Central registry for text splitters.

This module provides a factory function to create different types of text splitters
based on configuration, similar to how embeddings_config.py works for embeddings.
"""

from __future__ import annotations

import re
from typing import Optional, List, Any, TYPE_CHECKING

from loguru import logger

from ...constants import (
    DEFAULT_LOCAL_SEARCH_CHUNK_OVERLAP,
    DEFAULT_LOCAL_SEARCH_CHUNK_SIZE,
    DEFAULT_LOCAL_SEARCH_SPLITTER_TYPE,
    DEFAULT_LOCAL_SEARCH_TEXT_SEPARATORS,
)

# ``langchain_text_splitters`` is imported lazily inside ``get_text_splitter``
# (see below), NOT at module load. Importing *any* of its submodules runs the
# package ``__init__``, which eagerly pulls sentence-transformers, torch,
# spacy, nltk and konlpy — ~4-8 s and hundreds of MB of RSS. Because this
# module sits on the app-startup import chain (scheduler / blueprints /
# search engines all import ``LibraryRAGService`` → ``embeddings.splitters``),
# importing it eagerly added ~19 s to server boot on CI and tripped the
# UI-test startup watchdog. Deferring the import to call time keeps boot
# cheap and also means a broken optional splitter dependency (spacy / konlpy)
# can no longer crash startup — only the indexing path that actually needs
# it (the original intent of issue #4490, which the concrete-submodule
# imports did not actually achieve since they still run the package init).

# ``langchain_text_splitters`` must always be imported parent-first before
# importing any of its submodules. In CPython, concurrent submodule-first
# imports can create import-lock order inversion and raise ``_DeadlockError``.
#
# Keep these imports function-local and lazy so the package stays off the
# application startup path. The AST guard in the test suite enforces that
# every source import of ``langchain_text_splitters.<submodule>`` has a
# parent-package import earlier in the same executable scope.

if TYPE_CHECKING:
    from langchain_core.embeddings import Embeddings

# Valid splitter type options
VALID_SPLITTER_TYPES = [
    "recursive",
    "token",
    "sentence",
    "semantic",
]


def get_text_splitter(
    splitter_type: str = DEFAULT_LOCAL_SEARCH_SPLITTER_TYPE,
    chunk_size: int = DEFAULT_LOCAL_SEARCH_CHUNK_SIZE,
    chunk_overlap: int = DEFAULT_LOCAL_SEARCH_CHUNK_OVERLAP,
    text_separators: Optional[List[str]] = None,
    embeddings: Optional[Embeddings] = None,
    **kwargs,
) -> Any:
    """
    Get text splitter based on type.

    Args:
        splitter_type: Type of splitter ('recursive', 'token', 'sentence', 'semantic')
        chunk_size: Maximum size of chunks
        chunk_overlap: Overlap between chunks
        text_separators: Custom separators for recursive splitting and semantic
            safety bounds
        embeddings: Embeddings instance (required for 'semantic' type)
        **kwargs: Additional splitter-specific parameters

    Returns:
        A text splitter instance

    Raises:
        ValueError: If splitter_type is invalid or required parameters are missing
        ImportError: If required dependencies are not installed
    """
    # Normalize splitter type
    splitter_type = splitter_type.strip().lower()

    # Validate splitter type
    if splitter_type not in VALID_SPLITTER_TYPES:
        logger.error(f"Invalid splitter type: {splitter_type}")
        raise ValueError(
            f"Invalid splitter type: {splitter_type}. "
            f"Must be one of: {VALID_SPLITTER_TYPES}"
        )

    logger.info(
        f"Creating text splitter: type={splitter_type}, "
        f"chunk_size={chunk_size}, chunk_overlap={chunk_overlap}"
    )

    # Create the appropriate splitter. The ``langchain_text_splitters``
    # imports are intentionally function-local — see the module docstring:
    # importing the package eagerly pulls torch/sentence-transformers and
    # must stay off the app-startup import path.
    #
    # Enforce the parent-first import invariant before any submodule-level
    # imports occur. Non-semantic branches warm it here; the semantic branch
    # warms it below after dependency validation and before constructing its
    # BoundedSemanticChunker.
    if splitter_type != "semantic":
        import langchain_text_splitters  # noqa: F401

    if splitter_type == "token":
        from langchain_text_splitters.base import TokenTextSplitter

        return TokenTextSplitter(
            chunk_size=chunk_size,
            chunk_overlap=chunk_overlap,
        )

    if splitter_type == "sentence":
        from langchain_text_splitters.sentence_transformers import (
            SentenceTransformersTokenTextSplitter,
        )

        try:
            return SentenceTransformersTokenTextSplitter(
                chunk_overlap=chunk_overlap,
                tokens_per_chunk=chunk_size,
            )
        except ValueError as exc:
            # SentenceTransformersTokenTextSplitter is backed by a fixed
            # sentence-transformers tokenizer. It raises at construction,
            # before any chunk is emitted, when tokens_per_chunk (our
            # chunk_size) exceeds that tokenizer's max_seq_length. The raw
            # error names an internal model the user never configured, which
            # reads as an embedding failure. Translate only that specific cap
            # error into a settings-side message that points at the chunk_size
            # the user actually set; re-raise anything else unchanged.
            #
            # The cap is model-specific (LangChain reads it from
            # model.max_seq_length; 384 is the default all-mpnet-base-v2
            # value but not universal), so read it back from LangChain's own
            # message rather than hardcoding a number that could silently lie
            # if the tokenizer ever changes. See issue #4800.
            message = str(exc)
            if "maximum token limit" not in message:
                raise
            cap_match = re.search(r"is:\s*(\d+)", message)
            if cap_match:
                cap = cap_match.group(1)
                limit_phrase = f"capped at {cap} tokens per chunk"
                fix_phrase = f"Lower the chunk size to {cap} or less"
            else:
                limit_phrase = (
                    "capped at the tokenizer's maximum sequence length"
                )
                fix_phrase = "Lower the chunk size to fit that limit"
            raise ValueError(
                f"chunk_size={chunk_size} is too large for the 'sentence' "
                f"chunking method: it uses a fixed sentence-transformers "
                f"tokenizer {limit_phrase}. {fix_phrase}, or choose a "
                f"different chunking method ('recursive', 'token', or "
                f"'semantic') for larger chunks."
            ) from exc

    if splitter_type == "semantic":
        # Semantic chunking requires embeddings
        if embeddings is None:
            raise ValueError(
                "Semantic splitter requires 'embeddings' parameter. "
                "Please provide an embeddings instance."
            )

        try:
            from langchain_experimental.text_splitter import SemanticChunker
        except ImportError as e:
            logger.exception("Failed to import SemanticChunker")
            raise ImportError(
                "Semantic chunking requires langchain-experimental. "
                "Install it with: pip install langchain-experimental"
            ) from e

        # Enforce the parent-first import invariant before constructing
        # BoundedSemanticChunker.__init__ imports
        # langchain_text_splitters.character, so warm the parent package first.
        import langchain_text_splitters  # noqa: F401

        from .bounded_semantic_chunker import BoundedSemanticChunker

        # Get breakpoint threshold from kwargs or use default
        breakpoint_threshold_type = kwargs.get(
            "breakpoint_threshold_type", "percentile"
        )
        breakpoint_threshold_amount = kwargs.get(
            "breakpoint_threshold_amount", None
        )

        # Create semantic chunker
        chunker_kwargs = {"embeddings": embeddings}

        if breakpoint_threshold_type:
            chunker_kwargs["breakpoint_threshold_type"] = (
                breakpoint_threshold_type
            )

        if breakpoint_threshold_amount is not None:
            chunker_kwargs["breakpoint_threshold_amount"] = (
                breakpoint_threshold_amount
            )

        logger.info(
            f"Creating SemanticChunker with threshold_type={breakpoint_threshold_type}, "
            f"threshold_amount={breakpoint_threshold_amount}"
        )

        semantic_chunker = SemanticChunker(**chunker_kwargs)
        return BoundedSemanticChunker(
            semantic_chunker=semantic_chunker,
            chunk_size=chunk_size,
            separators=text_separators,
        )

    # "recursive" or default
    from langchain_text_splitters.character import (
        RecursiveCharacterTextSplitter,
    )

    # Use custom separators if provided, otherwise use defaults
    if text_separators is None:
        text_separators = list(DEFAULT_LOCAL_SEARCH_TEXT_SEPARATORS)

    return RecursiveCharacterTextSplitter(
        chunk_size=chunk_size,
        chunk_overlap=chunk_overlap,
        length_function=len,
        separators=text_separators,
    )


def is_semantic_chunker_available() -> bool:
    """Check if semantic chunking is available."""
    import importlib.util

    return importlib.util.find_spec("langchain_experimental") is not None