feat(chunking_utils): token-aware chunking — prevent silent data loss#375
Open
luojiyin1987 wants to merge 3 commits into
Open
feat(chunking_utils): token-aware chunking — prevent silent data loss#375luojiyin1987 wants to merge 3 commits into
luojiyin1987 wants to merge 3 commits into
Conversation
- Add max_tokens_per_chunk param to create_traditional_chunks() and create_text_chunks() — auto-scales chunk_size using existing calculate_safe_chunk_size() and validates with validate_chunk_token_limits() - Also scales AST chunk_size when token limit is given - Revalidate chunk_overlap after scaling to avoid SentenceSplitter errors - Add BaseRAGExample._resolve_chunk_token_limit() helper to resolve the embedding model's token limit from CLI args - build_index() now warns if existing chunks exceed the model's token limit Backward compat: default max_tokens_per_chunk=None (no-op, identical behavior)
- Delete _traditional_chunks_as_dicts() — it was a pure alias for create_traditional_chunks(). Replace all 6 call sites with direct calls. - Extract _parse_ast_chunk_output() — normalizes the 4 different chunk output shapes (object, str, dict with content, dict with text) into a uniform (text, metadata) tuple. Removes 20 lines of inline dispatch from create_ast_chunks(). - Net: +28/−35 lines, no behaviour change
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #374
Problem
chunk_sizeis in characters, but embedding models have token limits (512/2048/8192). Chars-to-tokens varies by language — code can be 1.2 tokens/char. Oversized chunks get silently truncated at embedding time.Solution
Opt-in
max_tokens_per_chunkparam uses existing infrastructure (all 3 helpers were already present but never wired together):Changes (2 files, +113/-14)
chunking_utils.py: auto-scale chunk_size, auto-scale AST chunk_size, revalidate overlap, post-validate tokensbase_rag_example.py:_resolve_chunk_token_limit()helper + build-time chunk validationBackward compat:
max_tokens_per_chunk=None(default) = identical behavior.