Class: Maglev::VectorStores::Pgvector
- Defined in:
- lib/maglev/vector_stores/pgvector.rb
Instance Method Summary collapse
- #capabilities ⇒ Object
- #delete(ids:) ⇒ Object
- #delete_by_owner(owner_type:, owner_id:) ⇒ Object
- #healthcheck ⇒ Object
-
#initialize(chunk_model: Chunk) ⇒ Pgvector
constructor
A new instance of Pgvector.
- #search(vector:, filters:, limit:) ⇒ Object
- #upsert(documents:) ⇒ Object
Constructor Details
Instance Method Details
#capabilities ⇒ Object
62 63 64 |
# File 'lib/maglev/vector_stores/pgvector.rb', line 62 def capabilities {metadata_filtering: true, pgvector: true} end |
#delete(ids:) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/maglev/vector_stores/pgvector.rb', line 47 def delete(ids:) ids.each do |id| owner_type, owner_id, source, chunk_index = id.split(":", 4) @chunk_model.where(owner_type: owner_type, owner_id: owner_id, source: source, chunk_index: chunk_index).delete_all end end |
#delete_by_owner(owner_type:, owner_id:) ⇒ Object
54 55 56 |
# File 'lib/maglev/vector_stores/pgvector.rb', line 54 def delete_by_owner(owner_type:, owner_id:) @chunk_model.where(owner_type: owner_type, owner_id: owner_id).delete_all end |
#healthcheck ⇒ Object
58 59 60 |
# File 'lib/maglev/vector_stores/pgvector.rb', line 58 def healthcheck @chunk_model.connection.active? ? :ok : :unavailable end |
#search(vector:, filters:, limit:) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/maglev/vector_stores/pgvector.rb', line 38 def search(vector:, filters:, limit:) scope = filters.reduce(@chunk_model.all) do |current_scope, (key, value)| current_scope.where(key => value) end scope.nearest_neighbors(:embedding, vector, distance: "cosine") .first(limit) .map { |row| document_for(row) } end |
#upsert(documents:) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/maglev/vector_stores/pgvector.rb', line 14 def upsert(documents:) @chunk_model.transaction do documents.each do |document| scope = identity_scope(document) existing = scope.find_by(chunk_index: document.chunk_index, content_checksum: document.content_checksum) next if existing scope.where(chunk_index: document.chunk_index).delete_all @chunk_model.create!( owner_type: document.owner_type, owner_id: document.owner_id, owner_model_name: document.owner_model_name, owner: document.owner, source: document.source, chunk_index: document.chunk_index, content: document.content, content_checksum: document.content_checksum, embedding_model: document., embedding: document. ) end end end |