Class: DSPy::Memory::MemoryRecord

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

Overview

Represents a single memory entry with metadata and embeddings

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content:, user_id: nil, tags: [], embedding: nil, id: nil, metadata: {}) ⇒ MemoryRecord

Returns a new instance of MemoryRecord.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dspy/memory/memory_record.rb', line 52

def initialize(content:, user_id: nil, tags: [], embedding: nil, id: nil, metadata: {})
  @id = id || SecureRandom.uuid
  @content = content
  @user_id = user_id
  @tags = tags
  @embedding = embedding
  @created_at = Time.now
  @updated_at = Time.now
  @access_count = 0
  @last_accessed_at = nil
  @metadata = 
end

Instance Attribute Details

#access_countObject

Returns the value of attribute access_count.



34
35
36
# File 'lib/dspy/memory/memory_record.rb', line 34

def access_count
  @access_count
end

#contentObject

Returns the value of attribute content.



16
17
18
# File 'lib/dspy/memory/memory_record.rb', line 16

def content
  @content
end

#created_atObject (readonly)

Returns the value of attribute created_at.



28
29
30
# File 'lib/dspy/memory/memory_record.rb', line 28

def created_at
  @created_at
end

#embeddingObject

Returns the value of attribute embedding.



25
26
27
# File 'lib/dspy/memory/memory_record.rb', line 25

def embedding
  @embedding
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#last_accessed_atObject

Returns the value of attribute last_accessed_at.



37
38
39
# File 'lib/dspy/memory/memory_record.rb', line 37

def last_accessed_at
  @last_accessed_at
end

#metadataObject

Returns the value of attribute metadata.



40
41
42
# File 'lib/dspy/memory/memory_record.rb', line 40

def 
  @metadata
end

#tagsObject

Returns the value of attribute tags.



22
23
24
# File 'lib/dspy/memory/memory_record.rb', line 22

def tags
  @tags
end

#updated_atObject

Returns the value of attribute updated_at.



31
32
33
# File 'lib/dspy/memory/memory_record.rb', line 31

def updated_at
  @updated_at
end

#user_idObject

Returns the value of attribute user_id.



19
20
21
# File 'lib/dspy/memory/memory_record.rb', line 19

def user_id
  @user_id
end

Class Method Details

.from_h(hash) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/dspy/memory/memory_record.rb', line 135

def self.from_h(hash)
  record = allocate
  record.instance_variable_set(:@id, hash['id'])
  record.instance_variable_set(:@content, hash['content'])
  record.instance_variable_set(:@user_id, hash['user_id'])
  record.instance_variable_set(:@tags, hash['tags'] || [])
  record.instance_variable_set(:@embedding, hash['embedding'])
  record.instance_variable_set(:@created_at, Time.parse(hash['created_at']))
  record.instance_variable_set(:@updated_at, Time.parse(hash['updated_at']))
  record.instance_variable_set(:@access_count, hash['access_count'] || 0)
  record.instance_variable_set(:@last_accessed_at, 
    hash['last_accessed_at'] ? Time.parse(hash['last_accessed_at']) : nil)
  record.instance_variable_set(:@metadata, hash['metadata'] || {})
  record
end

Instance Method Details

#accessed_recently?(seconds = 3600) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/dspy/memory/memory_record.rb', line 93

def accessed_recently?(seconds = 3600)
  return false if @last_accessed_at.nil?
  (Time.now - @last_accessed_at) <= seconds
end

#add_tag(tag) ⇒ Object



106
107
108
# File 'lib/dspy/memory/memory_record.rb', line 106

def add_tag(tag)
  @tags << tag unless @tags.include?(tag)
end

#age_in_daysObject



87
88
89
# File 'lib/dspy/memory/memory_record.rb', line 87

def age_in_days
  age_in_seconds / 86400.0
end

#age_in_secondsObject



81
82
83
# File 'lib/dspy/memory/memory_record.rb', line 81

def age_in_seconds
  Time.now - @created_at
end

#has_tag?(tag) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/dspy/memory/memory_record.rb', line 100

def has_tag?(tag)
  @tags.include?(tag)
end

#inspectObject



158
159
160
# File 'lib/dspy/memory/memory_record.rb', line 158

def inspect
  to_s
end

#record_access!Object



67
68
69
70
# File 'lib/dspy/memory/memory_record.rb', line 67

def record_access!
  @access_count += 1
  @last_accessed_at = Time.now
end

#remove_tag(tag) ⇒ Object



112
113
114
# File 'lib/dspy/memory/memory_record.rb', line 112

def remove_tag(tag)
  @tags.delete(tag)
end

#to_hObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/dspy/memory/memory_record.rb', line 118

def to_h
  {
    'id' => @id,
    'content' => @content,
    'user_id' => @user_id,
    'tags' => @tags,
    'embedding' => @embedding,
    'created_at' => @created_at.iso8601,
    'updated_at' => @updated_at.iso8601,
    'access_count' => @access_count,
    'last_accessed_at' => @last_accessed_at&.iso8601,
    'metadata' => @metadata
  }
end

#to_sObject



153
154
155
# File 'lib/dspy/memory/memory_record.rb', line 153

def to_s
  "#<MemoryRecord id=#{@id[0..7]}... content=\"#{@content[0..50]}...\" tags=#{@tags}>"
end

#update_content!(new_content) ⇒ Object



74
75
76
77
# File 'lib/dspy/memory/memory_record.rb', line 74

def update_content!(new_content)
  @content = new_content
  @updated_at = Time.now
end