Class: RCrewAI::Knowledge::Chunker
- Inherits:
-
Object
- Object
- RCrewAI::Knowledge::Chunker
- Defined in:
- lib/rcrewai/knowledge/chunker.rb
Overview
Splits text into fixed-size, overlapping character windows. Overlap keeps context from spilling across chunk boundaries during retrieval.
Instance Method Summary collapse
- #chunk(text) ⇒ Object
-
#initialize(chunk_size: 1000, overlap: 100) ⇒ Chunker
constructor
A new instance of Chunker.
Constructor Details
#initialize(chunk_size: 1000, overlap: 100) ⇒ Chunker
Returns a new instance of Chunker.
8 9 10 11 12 13 |
# File 'lib/rcrewai/knowledge/chunker.rb', line 8 def initialize(chunk_size: 1000, overlap: 100) raise ArgumentError, 'overlap must be smaller than chunk_size' if overlap >= chunk_size @chunk_size = chunk_size @overlap = overlap end |
Instance Method Details
#chunk(text) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rcrewai/knowledge/chunker.rb', line 15 def chunk(text) text = text.to_s return [] if text.empty? return [text] if text.length <= @chunk_size chunks = [] start = 0 step = @chunk_size - @overlap while start < text.length chunks << text[start, @chunk_size] start += step end chunks end |