Sha256: 4078e0a9a287a913489655f51e2b32da8990df8df29463510615fddf0dd7e146

Contents?: true

Size: 817 Bytes

Versions: 1

Compression:

Stored size: 817 Bytes

Contents

module LlmOrchestrator
  class Memory
    attr_reader :messages

    def initialize
      @messages = []
      @max_tokens = 2000  # Adjust based on your needs
    end

    def add_message(role, content)
      @messages << { role: role, content: content }
      trim_messages if exceeds_token_limit?
    end

    def clear
      @messages.clear
    end

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

    private

    def exceeds_token_limit?
      # Simple approximation: 4 chars ~= 1 token
      total_chars = @messages.sum { |msg| msg[:content].length }
      (total_chars / 4) > @max_tokens
    end

    def trim_messages
      while exceeds_token_limit? && @messages.size > 1
        @messages.shift  # Remove oldest message
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
llm_orchestrator-0.1.0 lib/llm_orchestrator/memory.rb