Class: Boxcars::VectorStore::Pgvector::BuildFromArray

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

Instance Method Summary collapse

Methods included from Boxcars::VectorStore

included

Constructor Details

#initialize(params) ⇒ BuildFromArray

initialize the vector store with the following parameters:

each hash item should have content and metadata [

{ content: "hello", metadata: { a: 1 } },
{ content: "hi", metadata: { a: 1 } },
{ content: "bye", metadata: { a: 1 } },
{ content: "what's this", metadata: { a: 1 } }

]

Parameters:

  • params (Hash)

    A Hash containing the initial configuration.

Options Hash (params):

  • :embedding_tool (Symbol)

    The embedding tool to use. Must be provided.

  • :input_array (Array)

    The array of inputs to use for the embedding tool. Must be provided.

  • :database_url (String)

    The URL of the database where embeddings are stored. Must be provided.

  • :table_name (String)

    The name of the database table where embeddings are stored. Must be provided.

  • :embedding_column_name (String)

    The name of the database column where embeddings are stored. required.

  • :content_column_name (String)

    The name of the database column where content is stored. Must be provided.

  • :metadata_column_name (String)

    The name of the database column where metadata is stored. required.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/boxcars/vector_store/pgvector/build_from_array.rb', line 28

def initialize(params)
  @embedding_tool = params[:embedding_tool] || :openai

  validate_params(embedding_tool, params[:input_array])

  @database_url = params[:database_url]
  @table_name = params[:table_name]
  @embedding_column_name = params[:embedding_column_name]
  @content_column_name = params[:content_column_name]
  @metadata_column_name = params[:metadata_column_name]

  @input_array = params[:input_array]
  @pg_vectors = []
end

Instance Method Details

#callHash

Returns vector_store: array of hashes with :content, :metadata, and :embedding keys.

Returns:

  • (Hash)

    vector_store: array of hashes with :content, :metadata, and :embedding keys



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/boxcars/vector_store/pgvector/build_from_array.rb', line 44

def call
  texts = input_array.map { |doc| doc[:content] }
  vectors = generate_vectors(texts)
  add_vectors(vectors, input_array)
  documents = save_vector_store

  {
    type: :pgvector,
    vector_store: documents
  }
end