Class: Phronomy::VectorStore::Base

Inherits:
Object
  • Object
show all
Includes:
AsyncBackend
Defined in:
lib/phronomy/vector_store/base.rb

Overview

Abstract interface for vector stores.

Implementations manage a collection of (embedding, metadata) pairs and support similarity search.

Async methods (search_async, add_async, remove_async, clear_async) are provided by the AsyncBackend mixin which defaults to routing calls through BlockingAdapterPool. Backends with native async drivers may override individual async methods without touching the pool at all.

Direct Known Subclasses

InMemory, Pgvector, RedisSearch

Instance Method Summary collapse

Methods included from AsyncBackend

#add_async, #clear_async, #remove_async, #search_async

Instance Method Details

#add(id:, embedding:, metadata: {}, cancellation_token: nil) ⇒ Object

Add a document with its vector embedding.

Parameters:

  • id (String)

    unique document identifier

  • embedding (Array<Float>)

    vector embedding

  • metadata (Hash) (defaults to: {})

    arbitrary metadata (e.g. the original message object)

  • cancellation_token (Phronomy::CancellationToken, nil) (defaults to: nil)

    optional; raises CancellationError when cancelled

Raises:

  • (NotImplementedError)


24
25
26
27
# File 'lib/phronomy/vector_store/base.rb', line 24

def add(id:, embedding:, metadata: {}, cancellation_token: nil)
  cancellation_token&.raise_if_cancelled!
  raise NotImplementedError, "#{self.class}#add is not implemented"
end

#clearObject

Remove all documents.

Raises:

  • (NotImplementedError)


50
51
52
# File 'lib/phronomy/vector_store/base.rb', line 50

def clear
  raise NotImplementedError, "#{self.class}#clear is not implemented"
end

#remove(id:) ⇒ Object

Remove a single document by id.

Parameters:

  • id (String)

    document identifier

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/phronomy/vector_store/base.rb', line 45

def remove(id:)
  raise NotImplementedError, "#{self.class}#remove is not implemented"
end

#search(query_embedding:, k: 5, cancellation_token: nil) ⇒ Array<Hash>

Return the k most similar documents to the query embedding.

Parameters:

  • query_embedding (Array<Float>)
  • k (Integer) (defaults to: 5)

    number of results

  • cancellation_token (Phronomy::CancellationToken, nil) (defaults to: nil)

    optional; raises CancellationError when cancelled

Returns:

  • (Array<Hash>)

    each element: { id:, score:, metadata: }

Raises:

  • (NotImplementedError)


36
37
38
39
# File 'lib/phronomy/vector_store/base.rb', line 36

def search(query_embedding:, k: 5, cancellation_token: nil)
  cancellation_token&.raise_if_cancelled!
  raise NotImplementedError, "#{self.class}#search is not implemented"
end

#sizeInteger

Return the number of documents stored.

Returns:

  • (Integer)

Raises:

  • (NotImplementedError)


58
59
60
# File 'lib/phronomy/vector_store/base.rb', line 58

def size
  raise NotImplementedError, "#{self.class}#size is not implemented"
end