Class: Phronomy::Memory::Retrieval::Semantic
- Defined in:
- lib/phronomy/memory/retrieval/semantic.rb
Overview
Retrieval strategy that returns the k semantically closest messages to the query.
Messages are indexed in a VectorStore on save. On retrieval, the query is embedded and the k nearest messages are returned. Falls back to the k most recent messages when no query is provided.
Instance Method Summary collapse
-
#clear_index(thread_id:) ⇒ Object
Clear indexed messages for a thread.
-
#index(thread_id:, messages:) ⇒ Object
Index a new batch of messages so they are searchable on future #select calls.
-
#initialize(embeddings:, store: nil, k: 10, max_index_size: nil) ⇒ Semantic
constructor
A new instance of Semantic.
-
#select(messages, query: nil, thread_id: nil) ⇒ Array
Return semantically relevant messages, or recent messages when query is nil.
Constructor Details
#initialize(embeddings:, store: nil, k: 10, max_index_size: nil) ⇒ Semantic
Returns a new instance of Semantic.
24 25 26 27 28 29 30 31 32 |
# File 'lib/phronomy/memory/retrieval/semantic.rb', line 24 def initialize(embeddings:, store: nil, k: 10, max_index_size: nil) @store = store || Phronomy::VectorStore::InMemory.new @embeddings = @k = k @index = {} # id => message (insertion-ordered via Ruby Hash) @counter = 0 @max_index_size = max_index_size @mutex = Mutex.new end |
Instance Method Details
#clear_index(thread_id:) ⇒ Object
Clear indexed messages for a thread.
55 56 57 58 59 60 61 62 63 |
# File 'lib/phronomy/memory/retrieval/semantic.rb', line 55 def clear_index(thread_id:) @mutex.synchronize do ids = @index.keys.select { |id| id.start_with?("#{thread_id}:") } ids.each do |id| @index.delete(id) @store.remove(id: id) end end end |
#index(thread_id:, messages:) ⇒ Object
Index a new batch of messages so they are searchable on future #select calls. Called by ConversationManager#save.
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/phronomy/memory/retrieval/semantic.rb', line 39 def index(thread_id:, messages:) .each do |msg| = @embeddings.(msg.content.to_s) @mutex.synchronize do id = "#{thread_id}:#{@counter}" @counter += 1 @store.add(id: id, embedding: , metadata: {thread_id: thread_id, message: msg}) @index[id] = msg evict_oldest! if @max_index_size && @index.size > @max_index_size end end end |
#select(messages, query: nil, thread_id: nil) ⇒ Array
Return semantically relevant messages, or recent messages when query is nil.
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/phronomy/memory/retrieval/semantic.rb', line 71 def select(, query: nil, thread_id: nil) if query && !query.strip.empty? = @embeddings.(query) results = @store.search(query_embedding: , k: @k * 3) results .select { |r| thread_id.nil? || r[:metadata][:thread_id] == thread_id } .first(@k) .map { |r| r[:metadata][:message] } else .last(@k) end end |