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.

3.8 KB
Raw Blame
"""Bound semantic document chunks with character-based recursive splitters."""

from __future__ import annotations

from collections.abc import Iterable
from typing import TYPE_CHECKING, Protocol

from langchain_core.documents.transformers import BaseDocumentTransformer
from local_deep_research.constants import DEFAULT_LOCAL_SEARCH_TEXT_SEPARATORS

if TYPE_CHECKING:
    from collections.abc import Sequence

    from langchain_core.documents import Document


class _DocumentSplitter(Protocol):
    """Minimal public document-splitting contract."""

    def split_documents(
        self, documents: Iterable[Document]
    ) -> list[Document]: ...


class BoundedSemanticChunker(BaseDocumentTransformer):
    """Bound semantic-comparison inputs and final chunks by character length."""

    def __init__(
        self,
        semantic_chunker: _DocumentSplitter,
        chunk_size: int,
        separators: list[str] | None = None,
    ) -> None:
        """Create recursive pre- and post-semantic bounding stages.

        Note:
            The parent ``langchain_text_splitters`` package is imported before
            its ``character`` submodule to preserve the parent-first import
            invariant required for safe concurrent cold imports.
        """
        import langchain_text_splitters  # noqa: F401
        from langchain_text_splitters.character import (
            RecursiveCharacterTextSplitter,
        )

        effective_separators = (
            separators.copy()
            if separators is not None
            else DEFAULT_LOCAL_SEARCH_TEXT_SEPARATORS.copy()
        )
        # RecursiveCharacterTextSplitter only guarantees the hard character
        # bound when it can fall back to splitting individual characters.
        # User-configured separator lists are allowed to omit that fallback,
        # so add it here instead of letting one separator-free span bypass the
        # cap this wrapper exists to enforce.
        if "" not in effective_separators:
            effective_separators.append("")

        def create_boundary_splitter() -> RecursiveCharacterTextSplitter:
            return RecursiveCharacterTextSplitter(
                chunk_size=chunk_size,
                # Semantic mode historically did not apply document overlap.
                # Zero overlap also prevents a near-size configured overlap
                # from amplifying large inputs into excessive requests.
                chunk_overlap=0,
                length_function=len,
                keep_separator=True,
                strip_whitespace=False,
                separators=effective_separators.copy(),
            )

        self._pre_splitter = create_boundary_splitter()
        self._semantic_chunker = semantic_chunker
        self._post_splitter = create_boundary_splitter()

    def split_text(self, text: str) -> list[str]:
        return [
            document.page_content for document in self.create_documents([text])
        ]

    def create_documents(
        self,
        texts: list[str],
        metadatas: list[dict[str, object]] | None = None,
    ) -> list[Document]:
        source_documents = self._pre_splitter.create_documents(texts, metadatas)
        semantic_documents = self._semantic_chunker.split_documents(
            source_documents
        )
        return self._post_splitter.split_documents(semantic_documents)

    def split_documents(self, documents: Iterable[Document]) -> list[Document]:
        """Bound semantic inputs and outputs while preserving their ordering."""
        texts = []
        metadatas = []
        for document in documents:
            texts.append(document.page_content)
            metadatas.append(document.metadata)
        return self.create_documents(texts, metadatas)

    def transform_documents(
        self, documents: Sequence[Document], **kwargs: object
    ) -> Sequence[Document]:
        return self.split_documents(documents)