Custom Chunking
Shows four chunking strategies for RAG documents and a combined approach using the trueno_rag library.
What this file does
Shows four chunking strategies for RAG documents and a combined approach using the trueno_rag library.
When to use it
- When you need to split Markdown docs by structure
- When you want semantic grouping of sentences
- When you need fixed-size paragraph chunks for essays
- When you require fine-grained sentence-level retrieval
Assumes this stack
Custom Chunking
Examples of using different chunking strategies.
Structural Chunking for Markdown
Best for documentation and structured content:
use trueno_rag::chunk::StructuralChunker;
let chunker = StructuralChunker::new(true, 500);
let doc = Document::new(r#"
# Introduction
Welcome to our guide.
## Getting Started
Follow these steps to begin.
## Advanced Usage
For power users, consider these options.
"#);
let chunks = chunker.chunk(&doc)?;
for chunk in &chunks {
println!("Headers: {:?}", chunk.metadata.headers);
println!("Content: {}...\n", &chunk.content[..50.min(chunk.content.len())]);
}
Semantic Chunking for Topic Detection
Groups content by meaning:
use trueno_rag::chunk::SemanticChunker;
use trueno_rag::embed::MockEmbedder;
let embedder = MockEmbedder::new(384);
let chunker = SemanticChunker::new(embedder, 0.5, 1000);
let doc = Document::new(
"Rust is a systems programming language. \
It focuses on safety and performance. \
Python is great for data science. \
It has many libraries for machine learning."
);
// Sentences about Rust should cluster together,
// sentences about Python should cluster together
let chunks = chunker.chunk(&doc)?;
Paragraph Chunking for Essays
Groups paragraphs together:
use trueno_rag::chunk::ParagraphChunker;
let chunker = ParagraphChunker::new(2); // 2 paragraphs per chunk
let doc = Document::new(
"First paragraph here.\n\n\
Second paragraph follows.\n\n\
Third paragraph continues.\n\n\
Fourth paragraph ends."
);
let chunks = chunker.chunk(&doc)?;
assert_eq!(chunks.len(), 2);
Sentence Chunking for Fine-Grained Retrieval
Groups sentences:
use trueno_rag::chunk::SentenceChunker;
let chunker = SentenceChunker::new(3, 1); // 3 sentences, 1 overlap
let doc = Document::new(
"First. Second. Third. Fourth. Fifth. Sixth."
);
let chunks = chunker.chunk(&doc)?;
Combining Strategies
Use structural chunking first, then refine:
let structural = StructuralChunker::new(true, 2000);
let sentence = SentenceChunker::new(5, 1);
// First pass: structural
let sections = structural.chunk(&doc)?;
// Second pass: sentence-level for large sections
let mut final_chunks = Vec::new();
for section in sections {
if section.content.len() > 500 {
let sub_doc = Document::new(§ion.content);
let sub_chunks = sentence.chunk(&sub_doc)?;
final_chunks.extend(sub_chunks);
} else {
final_chunks.push(section);
}
}
What's inside
5 code examples covering structural, semantic, paragraph, sentence, and combined chunking strategies
Change this for your project
- Replace
trueno_ragwith your own RAG library name - Replace
MockEmbedder::new(384)with your actual embedder - Replace
0.5similarity threshold with your own value
Where it goes
Keep it in your repository where the agent or team that needs it will read it.
Worth borrowing
- Combining chunkers in two passes: structural first, then sentence-level for large sections
Related Documents
Example Workflows and Use Cases
Presents 16 real-world automation workflows that combine n8n and Make, each with a trigger, processing steps, and output.
Example Conversations
Shows 15 realistic terminal conversations for using work-os with Claude Code across daily workflows, projects, and career tasks.
GitHub MCP Server - Code Execution Examples
Shows TypeScript code snippets for calling GitHub MCP tools like get repo info, list issues, create issues, and search code.
Example - computing context-aware co-occurrence scores with CoCoScore
Walks through using CoCoScore to compute context-aware co-occurrence scores for disease-gene pairs from sentence-level co-mentions.