Skip to main content
Code chunking splits code based on its structure, leveraging Abstract Syntax Trees (ASTs) to create contextually relevant segments. It uses the Chonkie library to identify natural code boundaries like functions, classes, and blocks. Learn more about code chunking. This preserves code semantics better than fixed-size chunking by ensuring related code stays together in the same chunk, while splitting occurs at meaningful structural boundaries. Code chunking supports several built-in tokenizers or a custom Tokenizer instance.
1

Create a Python file

2

Set up your virtual environment

3

Install dependencies

4
Set OpenAI Key
5
Set your OPENAI_API_KEY as an environment variable. You can get one from OpenAI.
6
Mac
Windows
7

Run PgVector

8

Run the script

Code Chunking Params