Sha256: 24b41ff65fffae957416866e0bb9e834836fdff5d5aaec5874defb5f24079bc5

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module Boxcars
  module VectorStore
    module InMemory
      class BuildFromDocumentArray
        include VectorStore

        def initialize(embedding_tool: :openai, documents: nil)
          validate_params(embedding_tool, documents)
          @embedding_tool = embedding_tool
          @documents = documents
          @memory_vectors = []
        end

        def call
          texts = documents
          vectors = generate_vectors(texts)
          add_vectors(vectors, documents)
          {
            type: :in_memory,
            vector_store: memory_vectors
          }
        end

        private

        attr_reader :documents, :memory_vectors

        def validate_params(embedding_tool, documents)
          raise_argument_error('documents is nil') unless documents
          return if %i[openai tensorflow].include?(embedding_tool)

          raise_argument_error('embedding_tool is invalid')
        end

        # returns array of documents with vectors
        def add_vectors(vectors, documents)
          vectors.zip(documents).each do |vector, doc|
            memory_vector = Document.new(
              content: doc[:content],
              embedding: vector[:embedding],
              metadata: doc[:metadata].merge(dim: vector[:dim])
            )
            @memory_vectors << memory_vector
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
boxcars-0.2.13 lib/boxcars/vector_store/in_memory/build_from_document_array.rb
boxcars-0.2.12 lib/boxcars/vector_store/in_memory/build_from_document_array.rb