Class: Boxcars::VectorStore::Hnswlib::SaveToHnswlib

Inherits:
Object
  • Object
show all
Includes:
Boxcars::VectorStore
Defined in:
lib/boxcars/vector_store/hnswlib/save_to_hnswlib.rb

Instance Method Summary collapse

Methods included from Boxcars::VectorStore

included

Constructor Details

#initialize(hnsw_vectors_array) ⇒ SaveToHnswlib

Returns a new instance of SaveToHnswlib.

Parameters:

  • document_embeddings (Array)

    An array of hashes containing the document id, document text, and embedding.

  • index_file_path (String)

    The path to the index file.

  • json_doc_file_path (Hash)

    a customizable set of options



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/boxcars/vector_store/hnswlib/save_to_hnswlib.rb', line 16

def initialize(hnsw_vectors_array)
  @metadata = hnsw_vectors_array&.first&.
  validate_params(hnsw_vectors_array, )

  @vectors = hnsw_vectors_array
  @index_file_path = [:index_file_path]
  @json_doc_file_path = [:json_doc_file_path] || @index_file_path.gsub(/\.bin$/, '.json')

  @metric = [:metric] || "l2"
  @dim = [:dim]
  @max_item = [:max_item] || 10000

  @index = ::Hnswlib::HierarchicalNSW.new(
    space: @metric,
    dim: @dim
  )
  @index.init_index(max_elements: @max_item)
end

Instance Method Details

#callObject



35
36
37
38
# File 'lib/boxcars/vector_store/hnswlib/save_to_hnswlib.rb', line 35

def call
  document_texts = add_vectors_to_index
  write_files(index, document_texts)
end