Semantic chunking for RAG: embedding-similarity-based document splitting with a pluggable Embedder contract.
moon add yuzhiblue/moon-semchunk///|
fn embed(text : String) -> Array[Double] {
// Replace with a call to your embedding provider.
[1.0, 0.0]
}
///|
fn main {
let text = "The cat sleeps. The cat plays. The ocean is deep. Ships cross it."
let chunks = @moon_semchunk.chunk(
text,
embed,
@moon_semchunk.Percentile(95.0),
)
for chunk in chunks {
println(chunk.text)
}
}pub(all) struct Chunk {
text : String
index : Int
}pub(all) struct ChunkOptions {
strategy : Threshold
buffer_size : Int
max_chunk_size : Int
overlap : Int
}pub(all) struct ChunkStats {
count : Int
min_length : Int
max_length : Int
mean_length : Double
sentence_count : Int
}pub(all) enum Threshold {
Percentile(Double)
StandardDeviation(Double)
Interquartile(Double)
}fn chunk_with_options(text : String, embedder : (String) -> Array[Double], options : ChunkOptions) -> Array[Chunk]fn make_options(strategy : Threshold, buffer_size : Int, max_chunk_size : Int, overlap : Int) -> ChunkOptionsInstall
Download zipSemantic chunking for RAG: embedding-similarity-based document splitting with a pluggable Embedder contract.