Class: DSPy::Memory::MemoryManager

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dspy/memory/memory_manager.rb

Overview

High-level memory management interface implementing MemoryTools API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store: nil, embedding_engine: nil, compactor: nil) ⇒ MemoryManager

Returns a new instance of MemoryManager.



27
28
29
30
31
# File 'lib/dspy/memory/memory_manager.rb', line 27

def initialize(store: nil, embedding_engine: nil, compactor: nil)
  @store = store || InMemoryStore.new
  @embedding_engine = embedding_engine || create_default_embedding_engine
  @compactor = compactor || MemoryCompactor.new
end

Instance Attribute Details

#compactorObject (readonly)

Returns the value of attribute compactor.



24
25
26
# File 'lib/dspy/memory/memory_manager.rb', line 24

def compactor
  @compactor
end

#embedding_engineObject (readonly)

Returns the value of attribute embedding_engine.



21
22
23
# File 'lib/dspy/memory/memory_manager.rb', line 21

def embedding_engine
  @embedding_engine
end

#storeObject (readonly)

Returns the value of attribute store.



18
19
20
# File 'lib/dspy/memory/memory_manager.rb', line 18

def store
  @store
end

Instance Method Details

#clear_memories(user_id: nil) ⇒ Object



147
148
149
# File 'lib/dspy/memory/memory_manager.rb', line 147

def clear_memories(user_id: nil)
  @store.clear(user_id: user_id)
end

#compact_if_needed!(user_id = nil) ⇒ Object



229
230
231
# File 'lib/dspy/memory/memory_manager.rb', line 229

def compact_if_needed!(user_id = nil)
  @compactor.compact_if_needed!(@store, @embedding_engine, user_id: user_id)
end

#count_memories(user_id: nil) ⇒ Object



141
142
143
# File 'lib/dspy/memory/memory_manager.rb', line 141

def count_memories(user_id: nil)
  @store.count(user_id: user_id)
end

#delete_memory(memory_id) ⇒ Object



85
86
87
# File 'lib/dspy/memory/memory_manager.rb', line 85

def delete_memory(memory_id)
  @store.delete(memory_id)
end

#export_memories(user_id: nil) ⇒ Object



209
210
211
212
# File 'lib/dspy/memory/memory_manager.rb', line 209

def export_memories(user_id: nil)
  memories = get_all_memories(user_id: user_id)
  memories.map(&:to_h)
end

#find_similar(memory_id, limit: 5, threshold: 0.7) ⇒ Object



153
154
155
156
157
158
159
160
161
# File 'lib/dspy/memory/memory_manager.rb', line 153

def find_similar(memory_id, limit: 5, threshold: 0.7)
  record = @store.retrieve(memory_id)
  return [] unless record&.embedding
  
  results = @store.vector_search(record.embedding, user_id: record.user_id, limit: limit + 1, threshold: threshold)
  
  # Remove the original record from results
  results.reject { |r| r.id == memory_id }
end

#force_compact!(user_id = nil) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/dspy/memory/memory_manager.rb', line 235

def force_compact!(user_id = nil)
  DSPy::Context.with_span(
    operation: 'memory.compaction_complete',
    'memory.user_id' => user_id,
    'memory.forced' => true
  ) do
    results = {}
    
    # Run all compaction strategies regardless of thresholds
    results[:size_compaction] = @compactor.send(:perform_size_compaction!, @store, user_id)
    results[:age_compaction] = @compactor.send(:perform_age_compaction!, @store, user_id)
    results[:deduplication] = @compactor.send(:perform_deduplication!, @store, @embedding_engine, user_id)
    results[:relevance_pruning] = @compactor.send(:perform_relevance_pruning!, @store, user_id)
    
    results[:total_compacted] = results.values.sum { |r| r.is_a?(Hash) ? r[:removed_count] || 0 : 0 }
    results
  end
end

#get_all_memories(user_id: nil, limit: nil, offset: nil) ⇒ Object



91
92
93
# File 'lib/dspy/memory/memory_manager.rb', line 91

def get_all_memories(user_id: nil, limit: nil, offset: nil)
  @store.list(user_id: user_id, limit: limit, offset: offset)
end

#get_memory(memory_id) ⇒ Object



60
61
62
# File 'lib/dspy/memory/memory_manager.rb', line 60

def get_memory(memory_id)
  @store.retrieve(memory_id)
end

#healthy?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/dspy/memory/memory_manager.rb', line 203

def healthy?
  @embedding_engine.ready? && @store.respond_to?(:count)
end

#import_memories(memories_data) ⇒ Object



216
217
218
219
220
221
222
223
224
225
# File 'lib/dspy/memory/memory_manager.rb', line 216

def import_memories(memories_data)
  records = memories_data.map { |data| MemoryRecord.from_h(data) }
  results = @store.store_batch(records)
  
  # Compact after batch import
  user_ids = records.map(&:user_id).compact.uniq
  user_ids.each { |user_id| compact_if_needed!(user_id) }
  
  results.count(true)
end

#search_by_tags(tags, user_id: nil, limit: nil) ⇒ Object



129
130
131
# File 'lib/dspy/memory/memory_manager.rb', line 129

def search_by_tags(tags, user_id: nil, limit: nil)
  @store.search_by_tags(tags, user_id: user_id, limit: limit)
end

#search_memories(query, user_id: nil, limit: 10, threshold: 0.5) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/dspy/memory/memory_manager.rb', line 97

def search_memories(query, user_id: nil, limit: 10, threshold: 0.5)
  DSPy::Context.with_span(
    operation: 'memory.search',
    **DSPy::ObservationType::Retriever.langfuse_attributes,
    'retriever.query' => query,
    'retriever.user_id' => user_id,
    'retriever.limit' => limit,
    'retriever.threshold' => threshold
  ) do |span|
    # Generate embedding for the query
    query_embedding = @embedding_engine.embed(query)
    
    # Perform vector search if supported
    results = if @store.supports_vector_search?
      @store.vector_search(query_embedding, user_id: user_id, limit: limit, threshold: threshold)
    else
      # Fallback to text search
      @store.search(query, user_id: user_id, limit: limit)
    end
    
    # Add retrieval results to span
    if span
      span.set_attribute('retriever.results_count', results.length)
      span.set_attribute('retriever.results', results.map { |r| { id: r.id, content: r.content[0..100] } }.to_json)
    end
    
    results
  end
end

#search_text(query, user_id: nil, limit: nil) ⇒ Object



135
136
137
# File 'lib/dspy/memory/memory_manager.rb', line 135

def search_text(query, user_id: nil, limit: nil)
  @store.search(query, user_id: user_id, limit: limit)
end

#statsObject



190
191
192
193
194
195
196
197
198
199
# File 'lib/dspy/memory/memory_manager.rb', line 190

def stats
  store_stats = @store.stats
  engine_stats = @embedding_engine.stats
  
  {
    store: store_stats,
    embedding_engine: engine_stats,
    total_memories: store_stats[:total_memories] || 0
  }
end

#store_memories_batch(contents, user_id: nil, tags: []) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/dspy/memory/memory_manager.rb', line 165

def store_memories_batch(contents, user_id: nil, tags: [])
  # Generate embeddings in batch for efficiency
  embeddings = @embedding_engine.embed_batch(contents)
  
  records = contents.zip(embeddings).map do |content, embedding|
    MemoryRecord.new(
      content: content,
      user_id: user_id,
      tags: tags,
      embedding: embedding
    )
  end
  
  # Store all records
  results = @store.store_batch(records)
  
  # Compact after batch operation
  compact_if_needed!(user_id)
  
  # Return only successfully stored records
  records.select.with_index { |_, idx| results[idx] }
end

#store_memory(content, user_id: nil, tags: [], metadata: {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dspy/memory/memory_manager.rb', line 35

def store_memory(content, user_id: nil, tags: [], metadata: {})
  # Generate embedding for the content
  embedding = @embedding_engine.embed(content)
  
  # Create memory record
  record = MemoryRecord.new(
    content: content,
    user_id: user_id,
    tags: tags,
    embedding: embedding,
    metadata: 
  )
  
  # Store in backend
  success = @store.store(record)
  raise "Failed to store memory" unless success
  
  # Check if compaction is needed after storing
  compact_if_needed!(user_id)
  
  record
end

#update_memory(memory_id, new_content, tags: nil, metadata: nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/dspy/memory/memory_manager.rb', line 66

def update_memory(memory_id, new_content, tags: nil, metadata: nil)
  record = @store.retrieve(memory_id)
  return false unless record
  
  # Update content and regenerate embedding
  record.update_content!(new_content)
  record.embedding = @embedding_engine.embed(new_content)
  
  # Update tags if provided
  record.tags = tags if tags
  
  # Update metadata if provided
  record..merge!() if 
  
  @store.update(record)
end