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.

2.8 KB
Raw Blame
import types

from langchain_core.documents import Document

from local_deep_research.embeddings.splitters import get_text_splitter


def test_semantic_comparisons_and_final_chunks_are_bounded_per_request() -> (
    None
):
    # Given
    cap = 48
    sentence = f"{'A' * 16}."
    oversized_sentence = f"{'L' * (cap * 4)}."
    source_documents = [
        Document(
            page_content=" ".join([oversized_sentence, sentence, sentence]),
            metadata={"source": "first"},
        ),
        Document(
            page_content=" ".join([sentence] * 5),
            metadata={"source": "second"},
        ),
    ]
    from local_deep_research.embeddings.providers.implementations.openai import (
        OpenAIEmbeddingsProvider,
    )

    embeddings = OpenAIEmbeddingsProvider.create_embeddings(
        settings_snapshot={
            "embeddings.openai.base_url": "http://localhost:1234/v1",
            "embeddings.openai.api_key": "",
            "embeddings.openai.model": "test-embedding-model",
            "embeddings.openai.dimensions": None,
            "embeddings.openai.chunk_size": 1,
        }
    )
    request_batches: list[list[str]] = []

    def fake_create(input, **kwargs):
        request_batches.append(list(input))
        return {"data": [{"embedding": [1.0, 0.0]} for _ in input]}

    embeddings.client = types.SimpleNamespace(create=fake_create)
    splitter = get_text_splitter(
        splitter_type="semantic",
        chunk_size=cap,
        # Semantic mode intentionally ignores configured overlap for its
        # safety bounds, including legacy near-size values like this one.
        chunk_overlap=cap - 1,
        embeddings=embeddings,
    )

    # When
    documents = splitter.split_documents(source_documents)

    # Then
    comparison_batches = request_batches.copy()
    comparison_inputs = [text for batch in comparison_batches for text in batch]
    assert comparison_inputs
    assert all(len(batch) == 1 for batch in comparison_batches)
    assert all(len(text) <= cap for text in comparison_inputs)
    assert documents
    assert all(len(document.page_content) <= cap for document in documents)
    assert [document.metadata["source"] for document in documents] == sorted(
        document.metadata["source"] for document in documents
    )
    assert {document.metadata["source"] for document in documents} == {
        "first",
        "second",
    }

    request_batches.clear()
    embeddings.embed_documents(
        [document.page_content for document in documents]
    )

    assert request_batches
    assert len(request_batches) == len(documents)
    assert all(len(batch) == 1 for batch in request_batches)
    assert [text for batch in request_batches for text in batch] == [
        document.page_content for document in documents
    ]
    assert all(len(text) <= cap for batch in request_batches for text in batch)