Class: Anthemic::Memory::Simple

Inherits:
Base
  • Object
show all
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

Constructor Details

#initializeSimple

Returns a new instance of Simple.



9
10
11
# File 'lib/anthemic/memory/simple.rb', line 9

def initialize
  @messages = []
end

Instance Method Details

#add(role:, content:) ⇒ void

This method returns an undefined value.

Add a message to memory

Parameters:

  • role (String)

    the role of the message sender (e.g., “user”, “assistant”)

  • content (String)

    the content of the message



18
19
20
21
22
23
24
25
26
# File 'lib/anthemic/memory/simple.rb', line 18

def add(role:, content:)
  message = { role: role, content: content, timestamp: Time.now.to_i }
  @messages << message
  
  # Trim messages if they exceed the maximum
  if @messages.size > MAX_MESSAGES
    @messages = @messages.last(MAX_MESSAGES)
  end
end

#get(_query = nil) ⇒ Array<Hash>

Get all messages (simple implementation doesn’t filter by query)

Parameters:

  • query (String)

    the query (ignored in Simple memory)

Returns:

  • (Array<Hash>)

    all messages in memory



32
33
34
# File 'lib/anthemic/memory/simple.rb', line 32

def get(_query = nil)
  @messages
end

#summarizeString

Provide a basic summary of the conversation

Returns:

  • (String)

    a summary of the memory contents



48
49
50
# File 'lib/anthemic/memory/simple.rb', line 48

def summarize
  "Conversation with #{@messages.size} messages."
end

#to_contextString

Convert memory to a context string for inclusion in prompts

Returns:

  • (String)

    a formatted context string



39
40
41
42
43
# File 'lib/anthemic/memory/simple.rb', line 39

def to_context
  @messages.map do |msg|
    "#{msg[:role].capitalize}: #{msg[:content]}"
  end.join("\n\n")
end