Class: DSPy::Tools::MemoryToolset

Inherits:
Toolset
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dspy/tools/memory_toolset.rb

Overview

Example implementation of a memory toolset for agents Provides tools for storing, retrieving, and managing memory

Instance Method Summary collapse

Methods inherited from Toolset

schema_for_method, to_tools, tool, toolset_name

Constructor Details

#initializeMemoryToolset

Returns a new instance of MemoryToolset.



27
28
29
# File 'lib/dspy/tools/memory_toolset.rb', line 27

def initialize
  @memory = T.let({}, T::Hash[String, T::Hash[Symbol, T.untyped]])
end

Instance Method Details

#clearObject



90
91
92
93
94
# File 'lib/dspy/tools/memory_toolset.rb', line 90

def clear
  count = @memory.size
  @memory.clear
  "Cleared #{count} memories"
end

#countObject



97
98
99
# File 'lib/dspy/tools/memory_toolset.rb', line 97

def count
  @memory.size
end

#delete(key:) ⇒ Object



82
83
84
85
86
87
# File 'lib/dspy/tools/memory_toolset.rb', line 82

def delete(key:)
  return "Memory '#{key}' not found" unless @memory.key?(key)

  @memory.delete(key)
  "Deleted memory '#{key}' successfully"
end

#get_metadata(key:) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/dspy/tools/memory_toolset.rb', line 102

def (key:)
  entry = @memory[key]
  return nil unless entry

  {
    created_at: entry[:created_at],
    updated_at: entry[:updated_at],
    access_count: entry[:access_count],
    last_accessed_at: entry[:last_accessed_at],
    tags: entry[:tags],
    value_length: entry[:value].length
  }
end

#list_keysObject



68
69
70
# File 'lib/dspy/tools/memory_toolset.rb', line 68

def list_keys
  @memory.keys.sort
end

#retrieve(key:) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/dspy/tools/memory_toolset.rb', line 44

def retrieve(key:)
  entry = @memory[key]
  return nil unless entry

  # Track access
  entry[:access_count] += 1
  entry[:last_accessed_at] = Time.now
  entry[:value]
end

#search(pattern:, in_keys: true, in_values: true) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dspy/tools/memory_toolset.rb', line 55

def search(pattern:, in_keys: true, in_values: true)
  results = []
  regex = Regexp.new(pattern, Regexp::IGNORECASE)

  @memory.each do |key, entry|
    match = (in_keys && key.match?(regex)) || (in_values && entry[:value].match?(regex))
    results << { key: key, value: entry[:value] } if match
  end

  results
end

#store(key:, value:, tags: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/dspy/tools/memory_toolset.rb', line 32

def store(key:, value:, tags: nil)
  @memory[key] = {
    value: value,
    tags: tags || [],
    created_at: Time.now,
    updated_at: Time.now,
    access_count: 0
  }
  "Stored memory '#{key}' successfully"
end

#update(key:, value:) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/dspy/tools/memory_toolset.rb', line 73

def update(key:, value:)
  return "Memory '#{key}' not found" unless @memory.key?(key)

  @memory[key][:value] = value
  @memory[key][:updated_at] = Time.now
  "Updated memory '#{key}' successfully"
end