Add AST-aware code chunking for better code understanding#58
Conversation
There was a problem hiding this comment.
Pull Request Overview
This pull request introduces AST-aware code chunking to LEANN using the astchunk library, enhancing code understanding in RAG applications by preserving semantic boundaries like functions and classes instead of arbitrary text splits.
Key changes include:
- AST-aware chunking utilities that detect code files and apply semantic chunking
- Enhanced document RAG with
--enable-code-chunkingflag for mixed content - New specialized code RAG application optimized for code repositories
Reviewed Changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
tests/test_document_rag.py |
Adds test for document RAG with AST chunking enabled |
tests/test_astchunk_integration.py |
Comprehensive test suite for AST chunking functionality |
pyproject.toml |
Adds astchunk and tree-sitter dependencies |
packages/leann-core/src/leann/cli.py |
Integrates AST chunking flags and logic into main CLI |
docs/features.md |
Documents new AST-aware code chunking feature |
apps/document_rag.py |
Adds code chunking support to document RAG |
apps/code_rag.py |
New specialized application for code repository RAG |
apps/chunking/utils.py |
Core AST chunking utilities and fallback mechanisms |
apps/chunking/__init__.py |
Package initialization for chunking utilities |
apps/base_rag_example.py |
Adds AST chunking parameters to base RAG class |
README.md |
Updates documentation with code chunking examples |
ASTCHUNK_INTEGRATION.md |
Comprehensive integration documentation |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
Thanks for your contribution!! It is a very important feature, we will review and merge asap. |
andylizf
left a comment
There was a problem hiding this comment.
Mostly LGTM! Thanks for your hard work @gabriel-dehan ! Left some discussions.
|
|
||
| from base_rag_example import BaseRAGExample, create_text_chunks | ||
| from llama_index.core import SimpleDirectoryReader | ||
|
|
There was a problem hiding this comment.
| from .utils import CODE_EXTENSIONS |
There was a problem hiding this comment.
This wouldn't work as we are not in the same directory no? We've got an init.py in chunking/ so chunking should be correct (or chunking.utils, but not .utils)
I've left it as is
| print(f"⚠️ AST chunking not available ({e}), falling back to traditional chunking") | ||
| use_ast = False | ||
|
|
||
| if not use_ast: |
There was a problem hiding this comment.
| if not use_ast: | |
| else: |
There was a problem hiding this comment.
Actually no because we want to fallback if there is an error (see line right above, we set use_ast = False) so the next if needs to be separated and not an alternative branch of the previous condition!
|
Also, in the future, we may want to refactor our |
|
@gabriel-dehan it is better to have pre-commit, thanks! |
I've been asking myself the same thing. I've got no idea on how to measure that properly so I used Claude Code to generate a quick and dirty evaluation framework on small and big codebases, it does a lot of things and isn't very clean but it clones big repositories like VSCode / Node / Jango and performs complex queries on it amongst other things. The results are here: gabriel-dehan#2 I'll check your other comments and implement the fixes. |
|
Yeah, thanks for the benchmark. I briefly looked at that, and I am confused about the efficiency and memory-saving gain. Maybe I can investigate that later, but at least we can first merge this PR and make it an option, thanks a lot!! Both this PR and the benchmark are great. Let's fix this PR and merge it first |
|
Rebased on main, made the requested changes (for the most part) and installed the pre-commit hook and fixed a bunch of violations. Also commited the uv.lock so the PR seems like a lot more changes than what it actually has 🤭 |
This PR introduces intelligent code chunking that preserves semantic boundaries (functions, classes, methods) for better code understanding in RAG applications. Key Features: - AST-aware chunking for Python, Java, C#, TypeScript files - Graceful fallback to traditional chunking for unsupported languages - New specialized code RAG application for repositories - Enhanced CLI with --use-ast-chunking flag - Comprehensive test suite with integration tests Technical Implementation: - New chunking_utils.py module with enhanced chunking logic - Extended base RAG framework with AST chunking arguments - Updated document RAG with --enable-code-chunking flag - CLI integration with proper error handling and fallback Benefits: - Better semantic understanding of code structure - Improved search quality for code-related queries - Maintains backward compatibility with existing workflows - Supports mixed content (code + documentation) seamlessly Dependencies: - Added astchunk and tree-sitter parsers to pyproject.toml - All dependencies are optional - fallback works without them Testing: - Comprehensive test suite in test_astchunk_integration.py - Integration tests with document RAG - Error handling and edge case coverage Documentation: - Updated README.md with AST chunking highlights - Added ASTCHUNK_INTEGRATION.md with complete guide - Updated features.md with new capabilities
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
9b316c2 to
cc41033
Compare
|
I guess still some lint error? @gabriel-dehan and thanks for fixing! |
|
Yeah I'll try to look at it tonight! |
- Updated packages to v0.3.2 - Added astchunk dependency and tree-sitter dependencies - Resolved uv.lock conflicts by regenerating
|
LGTM, let's wait for CI and we will add you to our ack! |
|
Looks good, we can merge first and make it default false, if it works well, we can make it default true! |
Introduces intelligent AST-aware code chunking to LEANN using astchunk, improving code understanding in RAG applications. Instead of arbitrarily splitting code at character boundaries, we now have the option to preserve semantic structure by chunking at function, class, and method boundaries.
This enhancement should make LEANN significantly better for code-related RAG applications while maintaining full backward compatibility.
I am also planning on adding
rubysupport to astchunk and have opened a pull request there.What's new?
AST-Aware Code Chunking
📱 New application
apps/code_rag.py): Specialized for code repositories with optimized parameters--enable-code-chunkingflag for mixed content🛠️ CLI Integration
--use-ast-chunkingflag for the main CLIHow this helps
Before: Code was chunked arbitrarily, often splitting functions mid-way
After: AST chunking preserves semantic boundaries
This should result in a much better code understanding and search quality in RAG applications.
Examples
Files Changed
apps/chunking_utils.py- New AST-aware chunking utilitiesapps/code_rag.py- Specialized code RAG applicationapps/base_rag_example.py- Enhanced with AST chunking argumentsapps/document_rag.py- Added code chunking flagpackages/leann-core/src/leann/cli.py- CLI integrationpyproject.toml- Added astchunk dependenciestests/test_astchunk_integration.py- Comprehensive test suiteREADME.md&docs/features.md- Updated documentationDependencies
Added optional dependencies for AST parsing:
astchunk>=0.1.0- Core AST chunking librarytree-sitter>=0.20.0+ language parsers - AST parsing backendPS: Hopefully I didn't break anything during the rebase with
main, I haven't tested everything after rebasing but the basic things are still working as expected.