Class: DSPy::Memory::InMemoryStore

Inherits:
MemoryStore show all
Extended by:
T::Sig
Defined in:
lib/dspy/memory/in_memory_store.rb

Overview

In-memory implementation of MemoryStore for development and testing

Instance Method Summary collapse

Methods inherited from MemoryStore

#delete_batch, #retrieve_batch, #store_batch, #update_batch

Constructor Details

#initializeInMemoryStore

Returns a new instance of InMemoryStore.



13
14
15
16
# File 'lib/dspy/memory/in_memory_store.rb', line 13

def initialize
  @memories = T.let({}, T::Hash[String, MemoryRecord])
  @mutex = T.let(Mutex.new, Mutex)
end

Instance Method Details

#clear(user_id: nil) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/dspy/memory/in_memory_store.rb', line 163

def clear(user_id: nil)
  @mutex.synchronize do
    if user_id
      count = @memories.values.count { |record| record.user_id == user_id }
      @memories.reject! { |_, record| record.user_id == user_id }
      count
    else
      count = @memories.size
      @memories.clear
      count
    end
  end
end

#count(user_id: nil) ⇒ Object



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

def count(user_id: nil)
  @mutex.synchronize do
    if user_id
      @memories.values.count { |record| record.user_id == user_id }
    else
      @memories.size
    end
  end
end

#delete(id) ⇒ Object



48
49
50
51
52
# File 'lib/dspy/memory/in_memory_store.rb', line 48

def delete(id)
  @mutex.synchronize do
    @memories.delete(id) ? true : false
  end
end

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dspy/memory/in_memory_store.rb', line 55

def list(user_id: nil, limit: nil, offset: nil)
  @mutex.synchronize do
    records = @memories.values
    
    # Filter by user_id if provided
    records = records.select { |r| r.user_id == user_id } if user_id
    
    # Sort by created_at (newest first)
    records = records.sort_by(&:created_at).reverse
    
    # Apply offset and limit
    records = records.drop(offset) if offset
    records = records.take(limit) if limit
    
    records
  end
end

#retrieve(id) ⇒ Object



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

def retrieve(id)
  @mutex.synchronize do
    record = @memories[id]
    record&.record_access!
    record
  end
end

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dspy/memory/in_memory_store.rb', line 74

def search(query, user_id: nil, limit: nil)
  @mutex.synchronize do
    regex = Regexp.new(Regexp.escape(query), Regexp::IGNORECASE)
    
    records = @memories.values.select do |record|
      # Filter by user_id if provided
      next false if user_id && record.user_id != user_id
      
      # Search in content and tags
      record.content.match?(regex) || record.tags.any? { |tag| tag.match?(regex) }
    end
    
    # Sort by relevance (exact matches first, then by recency)
    records = records.sort_by do |record|
      exact_match = record.content.downcase.include?(query.downcase) ? 0 : 1
      [exact_match, -record.created_at.to_f]
    end
    
    records = records.take(limit) if limit
    records
  end
end

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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/dspy/memory/in_memory_store.rb', line 98

def search_by_tags(tags, user_id: nil, limit: nil)
  @mutex.synchronize do
    records = @memories.values.select do |record|
      # Filter by user_id if provided
      next false if user_id && record.user_id != user_id
      
      # Check if record has any of the specified tags
      tags.any? { |tag| record.has_tag?(tag) }
    end
    
    # Sort by number of matching tags, then by recency
    records = records.sort_by do |record|
      matching_tags = tags.count { |tag| record.has_tag?(tag) }
      [-matching_tags, -record.created_at.to_f]
    end
    
    records = records.take(limit) if limit
    records
  end
end

#statsObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/dspy/memory/in_memory_store.rb', line 183

def stats
  @mutex.synchronize do
    total = @memories.size
    with_embeddings = @memories.values.count(&:embedding)
    users = @memories.values.map(&:user_id).compact.uniq.size
    
    {
      total_memories: total,
      memories_with_embeddings: with_embeddings,
      unique_users: users,
      supports_vector_search: supports_vector_search?,
      avg_access_count: total > 0 ? @memories.values.sum(&:access_count) / total.to_f : 0
    }
  end
end

#store(record) ⇒ Object



19
20
21
22
23
24
# File 'lib/dspy/memory/in_memory_store.rb', line 19

def store(record)
  @mutex.synchronize do
    @memories[record.id] = record
    true
  end
end

#supports_vector_search?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/dspy/memory/in_memory_store.rb', line 178

def supports_vector_search?
  true
end

#update(record) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/dspy/memory/in_memory_store.rb', line 36

def update(record)
  @mutex.synchronize do
    if @memories.key?(record.id)
      @memories[record.id] = record
      true
    else
      false
    end
  end
end

#vector_search(embedding, user_id: nil, limit: nil, threshold: nil) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/dspy/memory/in_memory_store.rb', line 120

def vector_search(embedding, user_id: nil, limit: nil, threshold: nil)
  @mutex.synchronize do
    records_with_similarity = []
    
    @memories.values.each do |record|
      # Filter by user_id if provided
      next if user_id && record.user_id != user_id
      
      # Skip records without embeddings
      next unless record.embedding
      
      # Calculate cosine similarity
      similarity = cosine_similarity(embedding, record.embedding)
      
      # Apply threshold if provided
      next if threshold && similarity < threshold
      
      records_with_similarity << [record, similarity]
    end
    
    # Sort by similarity (highest first)
    records_with_similarity.sort_by! { |_, similarity| -similarity }
    
    # Apply limit
    records_with_similarity = records_with_similarity.take(limit) if limit
    
    # Return just the records
    records_with_similarity.map(&:first)
  end
end