Class: Anthemic::Memory::Simple
- Defined in:
- lib/anthemic/memory/simple.rb
Constant Summary collapse
- MAX_MESSAGES =
Maximum number of messages to keep in memory
100
Instance Method Summary collapse
-
#add(role:, content:) ⇒ void
Add a message to memory.
-
#get(_query = nil) ⇒ Array<Hash>
Get all messages (simple implementation doesn’t filter by query).
-
#initialize ⇒ Simple
constructor
A new instance of Simple.
-
#summarize ⇒ String
Provide a basic summary of the conversation.
-
#to_context ⇒ String
Convert memory to a context string for inclusion in prompts.
Constructor Details
#initialize ⇒ Simple
Returns a new instance of Simple.
9 10 11 |
# File 'lib/anthemic/memory/simple.rb', line 9 def initialize = [] end |
Instance Method Details
#add(role:, content:) ⇒ void
This method returns an undefined value.
Add a message to memory
18 19 20 21 22 23 24 25 26 |
# File 'lib/anthemic/memory/simple.rb', line 18 def add(role:, content:) = { role: role, content: content, timestamp: Time.now.to_i } << # Trim messages if they exceed the maximum if .size > MAX_MESSAGES = .last(MAX_MESSAGES) end end |
#get(_query = nil) ⇒ Array<Hash>
Get all messages (simple implementation doesn’t filter by query)
32 33 34 |
# File 'lib/anthemic/memory/simple.rb', line 32 def get(_query = nil) end |
#summarize ⇒ String
Provide a basic summary of the conversation
48 49 50 |
# File 'lib/anthemic/memory/simple.rb', line 48 def summarize "Conversation with #{@messages.size} messages." end |
#to_context ⇒ String
Convert memory to a context string for inclusion in prompts
39 40 41 42 43 |
# File 'lib/anthemic/memory/simple.rb', line 39 def to_context .map do |msg| "#{msg[:role].capitalize}: #{msg[:content]}" end.join("\n\n") end |