Class: DSPy::Memory::MemoryRecord
- Inherits:
-
Object
- Object
- DSPy::Memory::MemoryRecord
- 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
-
#access_count ⇒ Object
Returns the value of attribute access_count.
-
#content ⇒ Object
Returns the value of attribute content.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#embedding ⇒ Object
Returns the value of attribute embedding.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#last_accessed_at ⇒ Object
Returns the value of attribute last_accessed_at.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#tags ⇒ Object
Returns the value of attribute tags.
-
#updated_at ⇒ Object
Returns the value of attribute updated_at.
-
#user_id ⇒ Object
Returns the value of attribute user_id.
Class Method Summary collapse
Instance Method Summary collapse
- #accessed_recently?(seconds = 3600) ⇒ Boolean
- #add_tag(tag) ⇒ Object
- #age_in_days ⇒ Object
- #age_in_seconds ⇒ Object
- #has_tag?(tag) ⇒ Boolean
-
#initialize(content:, user_id: nil, tags: [], embedding: nil, id: nil, metadata: {}) ⇒ MemoryRecord
constructor
A new instance of MemoryRecord.
- #inspect ⇒ Object
- #record_access! ⇒ Object
- #remove_tag(tag) ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
- #update_content!(new_content) ⇒ Object
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 = @embedding = @created_at = Time.now @updated_at = Time.now @access_count = 0 @last_accessed_at = nil @metadata = end |
Instance Attribute Details
#access_count ⇒ Object
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 |
#content ⇒ Object
Returns the value of attribute content.
16 17 18 |
# File 'lib/dspy/memory/memory_record.rb', line 16 def content @content end |
#created_at ⇒ Object (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 |
#embedding ⇒ Object
Returns the value of attribute embedding.
25 26 27 |
# File 'lib/dspy/memory/memory_record.rb', line 25 def @embedding end |
#id ⇒ Object (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_at ⇒ Object
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 |
#metadata ⇒ Object
Returns the value of attribute metadata.
40 41 42 |
# File 'lib/dspy/memory/memory_record.rb', line 40 def @metadata end |
#tags ⇒ Object
Returns the value of attribute tags.
22 23 24 |
# File 'lib/dspy/memory/memory_record.rb', line 22 def @tags end |
#updated_at ⇒ Object
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_id ⇒ Object
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
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_days ⇒ Object
87 88 89 |
# File 'lib/dspy/memory/memory_record.rb', line 87 def age_in_days age_in_seconds / 86400.0 end |
#age_in_seconds ⇒ Object
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
100 101 102 |
# File 'lib/dspy/memory/memory_record.rb', line 100 def has_tag?(tag) @tags.include?(tag) end |
#inspect ⇒ Object
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_h ⇒ Object
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_s ⇒ Object
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 |