Class: Maglev::Indexer

Inherits:
Object
  • Object
show all
Defined in:
lib/maglev/indexer.rb

Constant Summary collapse

SOURCE =
"snapshot"

Instance Method Summary collapse

Constructor Details

#initialize(record, chunk_model: Chunk, embedding_adapter: Maglev.configuration.embedding_adapter, embedding_dimensions: Maglev.configuration.embedding_dimensions, chunk_size: Maglev.configuration.chunk_size, vector_store: Maglev.configuration.vector_store) ⇒ Indexer

Returns a new instance of Indexer.



16
17
18
19
20
21
22
23
# File 'lib/maglev/indexer.rb', line 16

def initialize(record, chunk_model: Chunk, embedding_adapter: Maglev.configuration.embedding_adapter, embedding_dimensions: Maglev.configuration.embedding_dimensions, chunk_size: Maglev.configuration.chunk_size, vector_store: Maglev.configuration.vector_store)
  @record = record
  @chunk_model = chunk_model
  @embedding_adapter = embedding_adapter || Adapters::RubyLLMEmbedding.new
  @embedding_dimensions = embedding_dimensions
  @chunk_size = chunk_size
  @vector_store = vector_store
end

Instance Method Details

#indexObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/maglev/indexer.rb', line 25

def index
  validate_owner_id!
  validate_storage_dimensions!
  ActiveSupport::Notifications.instrument("maglev.index.start", owner_type: @record.class.name, owner_id: @record.id)

  chunk_count = loop do
    snapshot = @record.maglev_snapshot
    chunks = Chunker.new(max_characters: @chunk_size).call(snapshot)
    prepared = @vector_store ? prepare_documents(chunks) : prepare_chunks(chunks)
    break chunks.length if persist_if_current(snapshot, prepared)
  end

  ActiveSupport::Notifications.instrument("maglev.index.success", owner_type: @record.class.name, owner_id: @record.id, chunk_count: chunk_count)
rescue => error
  ActiveSupport::Notifications.instrument("maglev.index.failure", owner_type: @record.class.name, owner_id: @record.id, error_class: error.class.name)
  raise
end

#unindexObject



43
44
45
# File 'lib/maglev/indexer.rb', line 43

def unindex
  identity_scope.delete_all
end