Class: RCrewAI::Memory::InMemoryStore
- Inherits:
-
Object
- Object
- RCrewAI::Memory::InMemoryStore
- Defined in:
- lib/rcrewai/memory/in_memory_store.rb
Overview
Default, volatile vector store: records live in a Hash keyed by scope. Records: { id:, text:, vector:, metadata: }. Cosine search in Ruby.
Instance Method Summary collapse
- #add(id:, text:, vector:, scope:, metadata: {}) ⇒ Object
- #all(scope:) ⇒ Object
- #delete(scope:) ⇒ Object
- #delete_record(id:, scope:) ⇒ Object
-
#initialize ⇒ InMemoryStore
constructor
A new instance of InMemoryStore.
- #search(vector, k:, scope:) ⇒ Object
Constructor Details
#initialize ⇒ InMemoryStore
Returns a new instance of InMemoryStore.
10 11 12 |
# File 'lib/rcrewai/memory/in_memory_store.rb', line 10 def initialize @scopes = Hash.new { |h, k| h[k] = {} } end |
Instance Method Details
#add(id:, text:, vector:, scope:, metadata: {}) ⇒ Object
14 15 16 |
# File 'lib/rcrewai/memory/in_memory_store.rb', line 14 def add(id:, text:, vector:, scope:, metadata: {}) @scopes[scope][id] = { id: id, text: text, vector: vector, metadata: || {} } end |
#all(scope:) ⇒ Object
18 19 20 |
# File 'lib/rcrewai/memory/in_memory_store.rb', line 18 def all(scope:) @scopes[scope].values end |
#delete(scope:) ⇒ Object
31 32 33 |
# File 'lib/rcrewai/memory/in_memory_store.rb', line 31 def delete(scope:) @scopes.delete(scope) end |
#delete_record(id:, scope:) ⇒ Object
35 36 37 |
# File 'lib/rcrewai/memory/in_memory_store.rb', line 35 def delete_record(id:, scope:) @scopes[scope].delete(id) end |
#search(vector, k:, scope:) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/rcrewai/memory/in_memory_store.rb', line 22 def search(vector, k:, scope:) @scopes[scope].values .reject { |r| r[:vector].nil? } .map { |r| [r, Similarity.cosine(vector, r[:vector])] } .sort_by { |(_r, score)| -score } .first(k) .map(&:first) end |