Class: Looped::Memory
- Inherits:
-
Object
- Object
- Looped::Memory
- Extended by:
- T::Sig
- Defined in:
- lib/looped/memory.rb
Constant Summary collapse
- DEFAULT_MAX_ENTRIES =
10- DEFAULT_MAX_RESULT_LENGTH =
500
Instance Method Summary collapse
- #add(action_type:, action_input:, action_output:, model_id: nil, tokens_used: nil, error: nil) ⇒ Object
- #clear ⇒ Object
- #entries ⇒ Object
-
#initialize(max_entries: DEFAULT_MAX_ENTRIES, max_result_length: DEFAULT_MAX_RESULT_LENGTH) ⇒ Memory
constructor
A new instance of Memory.
- #recent(n) ⇒ Object
- #size ⇒ Object
- #to_context ⇒ Object
Constructor Details
#initialize(max_entries: DEFAULT_MAX_ENTRIES, max_result_length: DEFAULT_MAX_RESULT_LENGTH) ⇒ Memory
Returns a new instance of Memory.
12 13 14 15 16 |
# File 'lib/looped/memory.rb', line 12 def initialize(max_entries: DEFAULT_MAX_ENTRIES, max_result_length: DEFAULT_MAX_RESULT_LENGTH) @entries = T.let([], T::Array[Types::MemoryEntry]) @max_entries = max_entries @max_result_length = max_result_length end |
Instance Method Details
#add(action_type:, action_input:, action_output:, model_id: nil, tokens_used: nil, error: nil) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/looped/memory.rb', line 28 def add(action_type:, action_input:, action_output:, model_id: nil, tokens_used: nil, error: nil) @entries << Types::MemoryEntry.new( action_type: action_type, action_input: stringify_keys(action_input), action_output: action_output, timestamp: Time.now.utc.iso8601, model_id: model_id, error: error, tokens_used: tokens_used ) # Enforce max_entries limit @entries.shift while @entries.size > @max_entries end |
#clear ⇒ Object
64 65 66 |
# File 'lib/looped/memory.rb', line 64 def clear @entries.clear end |
#entries ⇒ Object
54 55 56 |
# File 'lib/looped/memory.rb', line 54 def entries @entries.dup end |
#recent(n) ⇒ Object
69 70 71 |
# File 'lib/looped/memory.rb', line 69 def recent(n) @entries.last(n) end |
#size ⇒ Object
59 60 61 |
# File 'lib/looped/memory.rb', line 59 def size @entries.size end |
#to_context ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/looped/memory.rb', line 44 def to_context @entries.last(@max_entries).map do |entry| Types::ActionSummary.new( action: summarize_action(entry), result: truncate(entry.action_output) ) end end |