Repository navigation
[OMEGA-504] Bound markdown chunks #370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abelfx
wants to merge
11
commits into
singnet:main
Choose a base branch
from
abelfx:OMEGA-369-bound-markdown-chunks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+228
−11
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
eb6c693
Chunk heading-less markdown through the size pass
abelfx 8bf72cd
Cut chunks that still exceed MAX_CHUNK_CHARS
abelfx 6af87cb
Add tests for the markdown chunk size bound
abelfx e226b41
Merge branch 'main' into OMEGA-369-bound-markdown-chunks
abelfx 97ce0f3
Count the paragraph separator against MAX_CHUNK_CHARS
abelfx a0c8d0d
Index text before the first heading
abelfx 52ae97b
Merge branch 'main' into OMEGA-369-bound-markdown-chunks
abelfx aee8301
Merge branch 'main' into OMEGA-369-bound-markdown-chunks
abelfx 43543b1
test preamble over the cap is split without losing text tests
abelfx a5da89d
Merge branch 'main' into OMEGA-369-bound-markdown-chunks
timur-ashkenov 505484b
Merge branch 'main' into OMEGA-369-bound-markdown-chunks
abelfx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| import importlib.util | ||
| import logging | ||
| import sys | ||
| import types | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| REPO_ROOT = Path(__file__).resolve().parents[1] | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def rag(monkeypatch): | ||
| """Load src/rag.py with embedding and storage deps stubbed.""" | ||
| logger_mod = types.ModuleType("src.logger") | ||
| logger_mod.get_logger = lambda name: logging.getLogger(name) | ||
| monkeypatch.setitem(sys.modules, "src.logger", logger_mod) | ||
|
|
||
| chromadb_mod = types.ModuleType("chromadb") | ||
| chromadb_mod.PersistentClient = lambda **kwargs: None | ||
| monkeypatch.setitem(sys.modules, "chromadb", chromadb_mod) | ||
|
|
||
| openai_mod = types.ModuleType("openai") | ||
| openai_mod.OpenAI = object | ||
| monkeypatch.setitem(sys.modules, "openai", openai_mod) | ||
|
|
||
| llm_mod = types.ModuleType("lib_llm_ext") | ||
| llm_mod.initLocalEmbedding = lambda: None | ||
| llm_mod.useLocalEmbedding = lambda text: [] | ||
| monkeypatch.setitem(sys.modules, "lib_llm_ext", llm_mod) | ||
|
|
||
| config_mod = types.ModuleType("config") | ||
| config_mod.config_get_by_key = lambda key, default=None: default | ||
| monkeypatch.setitem(sys.modules, "config", config_mod) | ||
|
|
||
| embedding_mod = types.ModuleType("embedding_models") | ||
| embedding_mod.embedding_model = lambda provider, model: model | ||
| monkeypatch.setitem(sys.modules, "embedding_models", embedding_mod) | ||
|
|
||
| spec = importlib.util.spec_from_file_location( | ||
| "rag_under_test", REPO_ROOT / "src" / "rag.py" | ||
| ) | ||
| module = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(module) | ||
| return module | ||
|
|
||
|
|
||
| def largest(chunks): | ||
| return max(len(c["text"]) for c in chunks) | ||
|
|
||
|
|
||
| def test_headingless_file_is_not_returned_whole(rag): | ||
| text = ("word " * 3000 + "\n\n") * 8 | ||
| chunks = rag._chunk_markdown(text, "plain.md") | ||
|
|
||
| assert len(chunks) > 1 | ||
| assert largest(chunks) <= rag.MAX_CHUNK_CHARS | ||
|
|
||
|
|
||
| def test_single_heading_does_not_change_the_bound(rag): | ||
| text = ("word " * 3000 + "\n\n") * 8 | ||
|
|
||
| without = rag._chunk_markdown(text, "plain.md") | ||
| with_heading = rag._chunk_markdown("# Chapter\n\n" + text, "with.md") | ||
|
|
||
| assert largest(without) <= rag.MAX_CHUNK_CHARS | ||
| assert largest(with_heading) <= rag.MAX_CHUNK_CHARS | ||
|
|
||
|
|
||
| def test_paragraph_longer_than_the_cap_is_cut(rag): | ||
| # One paragraph, no blank lines. | ||
| chunks = rag._chunk_markdown("# H\n\n" + "word " * 30000, "nl.md") | ||
|
|
||
| assert largest(chunks) <= rag.MAX_CHUNK_CHARS | ||
|
|
||
|
|
||
| def test_text_with_no_whitespace_is_cut_mid_token(rag): | ||
| chunks = rag._chunk_markdown("x" * (rag.MAX_CHUNK_CHARS * 3), "blob.md") | ||
|
|
||
| assert largest(chunks) <= rag.MAX_CHUNK_CHARS | ||
| assert "".join(c["text"] for c in chunks) == "x" * (rag.MAX_CHUNK_CHARS * 3) | ||
|
|
||
|
|
||
| def test_cuts_prefer_whitespace_boundaries(rag): | ||
| chunks = rag._chunk_markdown("word " * 30000, "words.md") | ||
|
|
||
| assert largest(chunks) <= rag.MAX_CHUNK_CHARS | ||
| for chunk in chunks: | ||
| assert "wor d" not in chunk["text"] | ||
| assert chunk["text"].startswith("word") | ||
| assert chunk["text"].endswith("word") | ||
|
|
||
|
|
||
| def test_headingless_chunks_keep_the_filename_breadcrumb(rag): | ||
| chunks = rag._chunk_markdown(("word " * 3000 + "\n\n") * 8, "plain.md") | ||
|
|
||
| assert {c["breadcrumb"] for c in chunks} == {"plain.md"} | ||
|
|
||
|
|
||
| def test_short_headingless_file_stays_one_chunk(rag): | ||
| chunks = rag._chunk_markdown("just a short note", "note.md") | ||
|
|
||
| assert chunks == [{"text": "just a short note", "breadcrumb": "note.md"}] | ||
|
|
||
|
|
||
| def test_heading_structure_and_breadcrumbs_are_preserved(rag): | ||
| text = ( | ||
| "# Top\n\n" + "a" * 200 + "\n\n" | ||
| "## Nested\n\n" + "b" * 200 + "\n\n" | ||
| "# Second\n\n" + "c" * 200 + "\n" | ||
| ) | ||
| chunks = rag._chunk_markdown(text, "doc.md") | ||
|
|
||
| assert [c["breadcrumb"] for c in chunks] == [ | ||
| "doc.md > Top", | ||
| "doc.md > Top > Nested", | ||
| "doc.md > Second", | ||
| ] | ||
|
|
||
|
|
||
| def test_paragraph_join_does_not_leave_a_fragment(rag): | ||
| # Two paragraphs landing within the "\n\n" join of the cap. | ||
| a = " ".join(["word"] * 600) | ||
| b = " ".join(["word"] * 600) + "s" | ||
| chunks = rag._chunk_markdown("# H\n\n" + a + "\n\n" + b, "t.md") | ||
|
|
||
| assert [len(c["text"]) for c in chunks] == [2999, 3000] | ||
|
|
||
|
|
||
| def test_text_before_the_first_heading_is_kept(rag): | ||
| doc = "Text before the first heading.\n\n# Heading\n\nText after the heading.\n" | ||
| chunks = rag._chunk_markdown(doc, "t.md") | ||
|
|
||
| assert "Text before the first heading." in " ".join(c["text"] for c in chunks) | ||
|
|
||
|
|
||
| def test_long_preamble_becomes_its_own_section(rag): | ||
| preamble = "a" * 500 | ||
| chunks = rag._chunk_markdown(preamble + "\n\n# Heading\n\n" + "b" * 500, "t.md") | ||
|
|
||
| assert chunks[0] == {"text": preamble, "breadcrumb": "t.md"} | ||
| assert chunks[1]["breadcrumb"] == "t.md > Heading" | ||
|
|
||
|
|
||
| def test_preamble_over_the_cap_is_split_without_losing_text(rag): | ||
| preamble = "a" * 12001 | ||
| after = "b" * 200 | ||
| chunks = rag._chunk_markdown(preamble + "\n\n# Heading\n\n" + after, "t.md") | ||
|
|
||
| preamble_chunks = [c for c in chunks if c["breadcrumb"] == "t.md"] | ||
| assert len(preamble_chunks) > 1 | ||
| assert all(len(c["text"]) <= rag.MAX_CHUNK_CHARS for c in chunks) | ||
| assert "".join(c["text"] for c in preamble_chunks) == preamble | ||
| assert after in "".join(c["text"] for c in chunks) | ||
|
|
||
|
|
||
| def test_bare_hash_line_is_not_a_heading(rag): | ||
| chunks = rag._chunk_markdown("Body text.\n\n#\n35\n", "t.md") | ||
|
|
||
| assert {c["breadcrumb"] for c in chunks} == {"t.md"} | ||
| assert "35" in " ".join(c["text"] for c in chunks) | ||
|
|
||
|
|
||
| def test_warns_when_a_file_has_no_headings(rag, caplog): | ||
| with caplog.at_level(logging.WARNING): | ||
| rag._chunk_markdown(("word " * 3000 + "\n\n") * 8, "plain.md") | ||
|
|
||
| assert "no headings, chunking on paragraphs" in caplog.text | ||
|
|
||
|
|
||
| def test_warns_when_a_character_level_cut_is_needed(rag, caplog): | ||
| with caplog.at_level(logging.WARNING): | ||
| rag._chunk_markdown("# H\n\n" + "word " * 30000, "nl.md") | ||
|
|
||
| assert "cut to MAX_CHUNK_CHARS" in caplog.text | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@abelfx Сould we also test a preamble exceeding MAX_CHUNK_CHARS (e.g. 12001 characters), checking that every chunk stays within the limit and no text is lost? The current 500-character case does not exercise splitting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@timur-ashkenov Done!