By Albert Mao
Jan 29, 2024
This article outlines the goals of chunking and available chunking strategies for LLM vector databases.
When someone starts building LLM-based applications, they will face the need to break long data into segments known as chunks to address model data processing limitations. Pre-processing long data for vector embedding in LLM is a prerequisite for context preservation, system efficiency and cost control.
There are several chunking methods that can be used depending on the language model and the tasks at hand. Below, we discuss in more detail the benefits of chunking and various chunking strategies for vector databases.
Why Chunk Data?
As it was already mentioned, chunking allows to segment data to fit the limitations of large language models. While the developers constantly increase the number of tokens an LLM can process as prompts, aka context window, the size of the context window with language models is still finite. The data fed to the LLM needs to be split into chunks to enable the model to effectively process data while enabling the model to understand the broader context.
When generative AI is used in conjunction with Retrieval Augmented Generation (RAG), chunking enables the model to identify relevant context in external databases. Chunking data for RAG impacts both retrieval and generation processes, providing for system efficiency in processing information for various tasks and maintaining the quality of LLM output.
In addition, chunking data contributes to LLMs' computational efficiency. While large chunks allow the model to capture more context, they incur higher computation costs and require more time for processing. Meanwhile, effective data chunking helps to keep both the computation costs and processing time under control.
Chunk Size Selection
There are several chunking strategies that can be used depending on the nature of the content embedded, the model for embedding, the expectations for user queries, context window limitations and other parameters of your LLM applications. Below, we compare the benefits and disadvantages of smaller and larger chunks and discuss fixed vs. context-aware chunking and chunk overlap.
Small vs. large chunks
Small chunks, for example, sentences embedded as vectors, help the model to focus on each sentence's specific meaning. Meanwhile, too short chunks can lead to insufficient context fed to LLM, guesswork and inconsistent output.
Larger chunks, for example, full paragraphs or documents embedded as vectors, allow the model to capture the broader meaning and context of the text. At the same time, such vector embedding can become more general, create noise and provide for lower focus on individual sentences' meaning.
Fixed-size vs. context-aware chunking
As it follows from the name of this method, fixed-size chunking presumes splitting text into segments with a fixed number of characters, words or tokens. For example, you can segment a document into chunks of 200 characters or 50 words each and leverage chunk overlap (more on this below) to keep context.
In context-aware chunking, the models split the text into segments using context separators, for example, breaking text into sentences by periods. The systems can utilize other frameworks, such as Markdown, by recognizing the syntax (blocks, headers, etc.) to divide content into meaningful chunks based on content structure.
When using fixed-size chunking, overlapping allows to preserve context between segments of text. When chunks are overlapped, each next chunk starts with the last few words of the preceding chunk to mitigate context loss, maintain LLM understanding of the whole text and retain critical context to enable coherent and accurate output.
Implement Effective Chunking Strategies with VectorShift
Chunking data for large language models can be done in several ways following available chunking strategies. While there is no one-size-fits-all chunking solution that suits all LLMs and tasks, determining the best chunking strategy can be done through an iterative process involving the evaluation of LLM performance on each chunking strategy or chunk size.
Meanwhile, implementing chunking in AI generative applications can be facilitated through no-code functionality and SDK interfaces available with VectorShift. For any questions related to implementing chunking into your AI-based systems, please don't hesitate to get in touch with our team or request a free demo.