Class: Phronomy::VectorStore::InMemory
- 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.
Instance Method Summary collapse
- #add(id:, embedding:, metadata: {}) ⇒ Object
- #clear ⇒ Object
-
#initialize ⇒ InMemory
constructor
A new instance of InMemory.
- #remove(id:) ⇒ Object
-
#search(query_embedding:, k: 5) ⇒ Array<Hash>
Sorted by descending score.
-
#size ⇒ Integer
Number of documents stored.
Constructor Details
#initialize ⇒ InMemory
Returns a new instance of InMemory.
15 16 17 |
# File 'lib/phronomy/vector_store/in_memory.rb', line 15 def initialize @documents = {} end |
Instance Method Details
#add(id:, embedding:, metadata: {}) ⇒ Object
22 23 24 25 |
# File 'lib/phronomy/vector_store/in_memory.rb', line 22 def add(id:, embedding:, metadata: {}) @documents[id] = {embedding: , metadata: } self end |
#clear ⇒ Object
49 50 51 52 |
# File 'lib/phronomy/vector_store/in_memory.rb', line 49 def clear @documents.clear self end |
#remove(id:) ⇒ Object
44 45 46 47 |
# File 'lib/phronomy/vector_store/in_memory.rb', line 44 def remove(id:) @documents.delete(id) self end |
#search(query_embedding:, k: 5) ⇒ Array<Hash>
Returns sorted by descending score.
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/phronomy/vector_store/in_memory.rb', line 30 def search(query_embedding:, k: 5) # 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(, doc[:embedding]) {id: id, score: score, metadata: doc[:metadata]} end results.sort_by { |r| -r[:score] }.first(k) end |
#size ⇒ Integer
Returns number of documents stored.
55 56 57 |
# File 'lib/phronomy/vector_store/in_memory.rb', line 55 def size @documents.size end |