Skip to main content

AST Snowball Splitter

AST Snowball Splitter is a Python package designed for intelligent code chunking using the nodes of Abstract Syntax Trees (AST). This approach allows for more relevant chunks compared to treating code as natural language. It is ideal for use in Retrieval-Augmented Generation (RAG) systems employed in coding assistants.

Features

  • Intelligent Code Chunking: Generates chunks based on the structure of the code, resulting in more meaningful segments.
  • AST-Based: Utilizes Abstract Syntax Trees to split the code, ensuring that chunks respect the logical boundaries of the code.
  • RAG System Integration: Perfect for use in Retrieval-Augmented Generation systems, enhancing the capabilities of coding assistants.
  • Langchain Integration: Outputs are compatible with Langchain, using Document types for seamless integration.

Installation

To install the package, use pip:

pip install astsnowballsplitter

Usage

from astsnowballsplitter.code_splitter import ASTSnowballSplitter, ASTLanguageConfig
from transformers import AutoTokenizer
from langchain.schema import Document

# Define the configuration for the languages
languages_config = [
    ASTLanguageConfig(language='python', library_path='build/my-languages.so', grammar_path='tree-sitter-python'),
    ASTLanguageConfig(language='javascript', library_path='build/my-languages.so', grammar_path='tree-sitter-javascript')
]

# Initialize the tokenizer and splitter
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
splitter = ASTSnowballSplitter(
    tokenizer=tokenizer,
    chunk_size=100,
    chunk_overlap=10,
    languages_config=languages_config
)

# Define source code to split
python_code = """
def example_function():
    print("Hello, world!")
    for i in range(10):
        print(i)
"""

javascript_code = """
function exampleFunction() {
    console.log("Hello, world!");
    for (let i = 0; i < 10; i++) {
        console.log(i);
    }
}
"""

# Split the code into chunks
texts = [python_code, javascript_code]
file_extensions = ['py', 'js']
documents = splitter.split_text(texts, file_extensions)

# Display the results
for doc in documents:
    print(doc.page_content)
    print(doc.metadata)

Metadata

Release files for astsnowballsplitter 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for astsnowballsplitter 0.2.0
File Size Uploaded Provenance
astsnowballsplitter-0.2.0.tar.gz 7.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for astsnowballsplitter 0.2.0
File Interpreter ABI Platform
astsnowballsplitter-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.0 kB

Release files / astsnowballsplitter-0.2.0.tar.gz

Download URL astsnowballsplitter-0.2.0.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
2280f92396852e748e34ffd488b782081f35ab4941caca386d480ef444e06adf
BLAKE2b-256 checksum
How to use checksums
8eac2c50f700118ea4cd853ec42390a0002d504f025b976bce4106ff02cbe446
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.5

Release files / astsnowballsplitter-0.2.0-py3-none-any.whl

Download URL astsnowballsplitter-0.2.0-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
52dde79fcf6b26036994bc6c94c3d35ef542296e37343bcce7da4bd35ee1dee7
BLAKE2b-256 checksum
How to use checksums
2c59a77fb4995b78b21a7531b09ae0e1f7e109e4a2c46062050354ebdb635a72
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.5

Security for astsnowballsplitter 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page