Class: Soka::Memory
- Inherits:
-
Object
- Object
- Soka::Memory
- Defined in:
- lib/soka/memory.rb
Overview
Manages conversation history for agents
Instance Attribute Summary collapse
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
Instance Method Summary collapse
- #add(role:, content:) ⇒ Object
- #clear ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(initial_messages = []) ⇒ Memory
constructor
A new instance of Memory.
- #inspect ⇒ Object
- #last ⇒ Object
- #size ⇒ Object
- #to_messages ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(initial_messages = []) ⇒ Memory
Returns a new instance of Memory.
8 9 10 11 12 13 14 15 |
# File 'lib/soka/memory.rb', line 8 def initialize( = []) = [] # Add initial messages if provided return unless .is_a?(Array) .each { |msg| add(**msg) } end |
Instance Attribute Details
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
6 7 8 |
# File 'lib/soka/memory.rb', line 6 def end |
Instance Method Details
#add(role:, content:) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/soka/memory.rb', line 17 def add(role:, content:) validate_role!(role) validate_content!(content) << { role: role.to_s, content: content, timestamp: Time.now } end |
#clear ⇒ Object
32 33 34 |
# File 'lib/soka/memory.rb', line 32 def clear .clear end |
#empty? ⇒ Boolean
40 41 42 |
# File 'lib/soka/memory.rb', line 40 def empty? .empty? end |
#inspect ⇒ Object
58 59 60 |
# File 'lib/soka/memory.rb', line 58 def inspect to_s end |
#last ⇒ Object
44 45 46 |
# File 'lib/soka/memory.rb', line 44 def last .last end |
#size ⇒ Object
36 37 38 |
# File 'lib/soka/memory.rb', line 36 def size .size end |
#to_messages ⇒ Object
28 29 30 |
# File 'lib/soka/memory.rb', line 28 def .map { |msg| { role: msg[:role], content: msg[:content] } } end |
#to_s ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/soka/memory.rb', line 48 def to_s return '<Soka::Memory> []' if empty? = .map do |msg| " { role: '#{msg[:role]}', content: '#{truncate(msg[:content])}' }" end.join(",\n") "<Soka::Memory> [\n#{formatted_messages}\n]" end |