Class: Kreuzberg::Config::Chunking

Inherits:
Object
  • Object
show all
Defined in:
lib/kreuzberg/config.rb

Overview

Chunking configuration

Examples:

chunking = Chunking.new(max_chars: 1000, max_overlap: 200)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_chars: nil, max_overlap: nil, preset: nil, embedding: nil, chunk_size: nil, chunk_overlap: nil, enabled: true) ⇒ Chunking

Returns a new instance of Chunking.

Raises:

  • (ArgumentError)


198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/kreuzberg/config.rb', line 198

def initialize(
  max_chars: nil,
  max_overlap: nil,
  preset: nil,
  embedding: nil,
  chunk_size: nil,
  chunk_overlap: nil,
  enabled: true
)
  resolved_size = chunk_size || max_chars || 1000
  resolved_overlap = chunk_overlap || max_overlap || 200

  @max_chars = resolved_size.to_i
  @max_overlap = resolved_overlap.to_i

  # Validate positive values
  raise ArgumentError, "max_chars must be a positive integer, got #{@max_chars}" if @max_chars.negative?
  raise ArgumentError, "max_overlap must be a positive integer, got #{@max_overlap}" if @max_overlap.negative?

  @preset = preset&.to_s
  @embedding = normalize_embedding(embedding)
  @enabled = boolean_or_nil(enabled)
end

Instance Attribute Details

#embeddingObject (readonly)

Returns the value of attribute embedding.



196
197
198
# File 'lib/kreuzberg/config.rb', line 196

def embedding
  @embedding
end

#enabledObject (readonly)

Returns the value of attribute enabled.



196
197
198
# File 'lib/kreuzberg/config.rb', line 196

def enabled
  @enabled
end

#max_charsObject (readonly)

Returns the value of attribute max_chars.



196
197
198
# File 'lib/kreuzberg/config.rb', line 196

def max_chars
  @max_chars
end

#max_overlapObject (readonly)

Returns the value of attribute max_overlap.



196
197
198
# File 'lib/kreuzberg/config.rb', line 196

def max_overlap
  @max_overlap
end

#presetObject (readonly)

Returns the value of attribute preset.



196
197
198
# File 'lib/kreuzberg/config.rb', line 196

def preset
  @preset
end

Instance Method Details

#to_hObject



222
223
224
225
226
227
228
229
230
231
232
# File 'lib/kreuzberg/config.rb', line 222

def to_h
  config = {
    max_chars: @max_chars,
    max_overlap: @max_overlap,
    preset: @preset,
    embedding: @embedding&.to_h
  }.compact
  # @type var config: Hash[Symbol, untyped]
  config[:enabled] = @enabled unless @enabled.nil?
  config
end