


























For a RAG system, indexing is not just about putting chunks into a vector database. It usually includes three major parts:
This step focuses on how to split raw documents into smaller units that can be embedded and retrieved.
Common strategies:
This step focuses on making each chunk more self-contained and easier to retrieve.
Common techniques:
This step focuses on how to maintain the quality, structure, and lifecycle of the index.
Common mechanisms:
This section mainly covers how to split data into chunks before indexing or retrieval.
Semantic chunking splits content based on meaning, instead of only using token size.
The core idea is to preserve semantic completeness and avoid breaking a sentence, paragraph, or logical section in the middle.
Common approaches:
Example:
Bad:
'The elevator maintenance report shows that the door sensor failed because'
'of repeated signal loss during peak operating hours.'
Why it is bad: the sentence is split in the middle, so each chunk loses context.
Good:
The elevator maintenance report shows that the door sensor failed because of repeated signal loss during peak operating hours.
Why it is good: the sentence keeps its complete meaning.
Recursive chunking uses multiple splitting strategies in priority order.
A common flow is:
The benefit is that it tries to keep the original structure and meaning as much as possible, while still ensuring each chunk fits within the target size limit.
How to Design the Indexing API in a RAG SDK?
For a RAG SDK, the indexing API should be flexible enough for common use cases, while still allowing advanced users to customize the pipeline.
A good design can include three layers:
1. Provide Built-in Splitters
The SDK should provide several built-in splitters for common scenarios, for example:
TokenTextSplitterRecursiveTextSplitterMarkdownHeaderSplitterSemanticTextSplitterCodeSplitterconst index = await rag.index({
documents,
splitter: new RecursiveTextSplitter({
chunkSize: 800,
chunkOverlap: 100,
}),
})
2. Allow Third-party Splitters Through Adapters
Users may already use splitters from LangChain, LlamaIndex, or internal company libraries.
Instead of forcing users to rewrite everything, the SDK can provide adapter support.
const splitter = fromLangChainSplitter(langChainSplitter)
await rag.index({
documents,
splitter,
})
3. Provide a Splitter Interface for Custom Implementations
The SDK should expose a clear interface, so users can create their own splitters.
interface DocumentSplitter {
split(document: Document): Promise<Chunk[]>
}
class CustomBusinessSplitter implements DocumentSplitter {
async split(document: Document): Promise<Chunk[]> {
// Custom splitting logic
return chunks
}
}
After we already have chunks, we can further improve them for better search, retrieval, and indexing.
The goal is not just to split the document, but to make each chunk more self-contained and easier to retrieve correctly.
Contextual headers add metadata or structural information to each chunk, so that even after chunking, the chunk can still preserve its original context.
This is especially useful when a chunk is retrieved independently from the original document.
Common metadata includes:
Example:
Chunk without contextual header:
## Pricing
The Pro plan supports up to 100 users and includes advanced analytics.
This is understandable, but the context is weak. The retriever may not know this chunk is about pricing.
Chunk with contextual header:
Document: Product Plan Guide
Section: Pricing
Module: Pro Plan
The Pro plan supports up to 100 users and includes advanced analytics.
This is better because the chunk carries its context even when retrieved alone.
Document augmentation means adding extra information before indexing to improve retrieval quality.
The augmentation does not necessarily need to modify the original document. It can be added only to the indexed version, metadata, or embedding input.
The goal is to make the content easier to search, match, and understand.
Common approaches:
Example:
Original content:
You can cancel your subscription from the billing page.
Augmented content for indexing:
Standard questions:
- How can I cancel my subscription?
- Where do I cancel my plan?
- How do I stop auto-renewal?
Answer:
You can cancel your subscription from the billing page.
Metadata extraction means extracting structured information from documents during the indexing stage and storing it as metadata. This makes later search, filtering, sorting, and display easier.
This metadata is often not part of the main document content, but it is very important for the system.
Common metadata includes:
A interface for RAG application looks like this:
type IndexItem = {
id: string
content: string
embedding: number[]
metadata: {
source: string
title?: string
tags?: string[]
}
}
Basically it's a combination of chunk + embedding + metadata
Deduplication and cleaning are part of pre-indexing data processing.
This means that before indexing, we first remove duplicate content, noise, and dirty data, so they do not enter the vector database.
This is a very common and important step. If the raw data quality is poor, even good embeddings and retrieval methods will struggle to produce ideal results.
Common operations include:
Hierarchical indexing is an indexing strategy that builds knowledge representations at multiple levels.
Instead of creating only one flat index at a single granularity, hierarchical indexing creates indexes at different levels, such as document, section, and chunk.
The goal is to allow the retrieval system to first locate the relevant area at a coarse level, and then drill down into more specific content. It can also choose different levels of detail depending on the complexity of the user’s question.
Comparison between normal indexing and hierarchical indexing:
In normal indexing, all chunks are usually stored at the same level:
chunk1
chunk2
chunk3
The retriever directly searches across all chunks.
In hierarchical indexing, the content keeps its original structure:
Document A
├── Section 1
│ ├── chunk1
│ └── chunk2
└── Section 2
└── chunk3
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。