Class: Phronomy::VectorStore::InMemory

Inherits:
Base
  • Object
show all
Defined in:
lib/phronomy/vector_store/in_memory.rb

Overview

Pure-Ruby in-memory vector store using cosine similarity.

Intended for tests, short-lived agents, and Retrieval::Semantic scenarios where the message count is small enough that a linear scan is fast enough.

Examples:

store = Phronomy::VectorStore::InMemory.new
store.add(id: "1", embedding: [0.1, 0.9], metadata: { message: msg })
results = store.search(query_embedding: [0.1, 0.8], k: 3)

Instance Method Summary collapse

Constructor Details

#initialize(dimension: nil) ⇒ InMemory

Returns a new instance of InMemory.

Parameters:

  • dimension (Integer, nil) (defaults to: nil)

    expected embedding dimension. When nil, the dimension is inferred from the first call to #add. For multi-threaded use, pass dimension: explicitly; concurrent first adds are not guaranteed to be race-free.



19
20
21
22
# File 'lib/phronomy/vector_store/in_memory.rb', line 19

def initialize(dimension: nil)
  @documents = {}
  @expected_dimension = dimension
end

Instance Method Details

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

Parameters:

  • id (String)
  • embedding (Array<Float>)
  • metadata (Hash) (defaults to: {})


27
28
29
30
31
32
33
# File 'lib/phronomy/vector_store/in_memory.rb', line 27

def add(id:, embedding:, metadata: {})
  # Establish expected dimension on first add, then validate.
  @expected_dimension ||= embedding.size
  validate_embedding_dimension!(embedding, @expected_dimension)
  @documents[id] = {embedding: embedding, metadata: }
  self
end

#clearObject



59
60
61
62
# File 'lib/phronomy/vector_store/in_memory.rb', line 59

def clear
  @documents.clear
  self
end

#remove(id:) ⇒ Object



54
55
56
57
# File 'lib/phronomy/vector_store/in_memory.rb', line 54

def remove(id:)
  @documents.delete(id)
  self
end

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

Returns sorted by descending score.

Parameters:

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

Returns:

  • (Array<Hash>)

    sorted by descending score



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/phronomy/vector_store/in_memory.rb', line 38

def search(query_embedding:, k: 5)
  # search never establishes dimension; validate only when dimension is known.
  validate_embedding_dimension!(query_embedding, @expected_dimension)
  # Take an atomic snapshot before iterating.  Hash#dup is a C-level
  # call that completes without releasing the GVL, so it is atomic with
  # respect to any other Ruby thread.  Iterating the copy instead of
  # @documents directly prevents "can't add a new key into hash during
  # iteration" when a concurrent thread calls #add.
  snapshot = @documents.dup
  results = snapshot.map do |id, doc|
    score = cosine_similarity(query_embedding, doc[:embedding])
    {id: id, score: score, metadata: doc[:metadata]}
  end
  results.sort_by { |r| -r[:score] }.first(k)
end

#sizeInteger

Returns number of documents stored.

Returns:

  • (Integer)

    number of documents stored



65
66
67
# File 'lib/phronomy/vector_store/in_memory.rb', line 65

def size
  @documents.size
end