Sha256: 7badc30ac0f0ffa4f754118bc55dd87d686e6b2068b7207ad0902106571b8be1

Contents?: true

Size: 894 Bytes

Versions: 3

Compression:

Stored size: 894 Bytes

Contents

# frozen_string_literal: true

module Luo

  ##
  # 用来保存回话历史的简单内存队列
  class MemoryHistory
    include Configurable

    setting :max_size, default: 12

    ##
    # 初始化一个队列
    # @param [Integer] max_size 队列的最大长度
    def initialize(max_size = config.max_size)
      @queue = []
      @max_size = max_size
    end

    ##
    # 入队
    def enqueue(element)
      if @queue.size == @max_size
        @queue.shift
      end
      @queue << element
    end

    def user(content)
      enqueue({role: "user", content: content})
    end

    def assistant(content)
      enqueue({role: "assistant", content: content})
    end

    alias push enqueue

    def dequeue
      @queue.shift
    end

    def size
      @queue.size
    end

    def to_a
      @queue
    end

    def to_json
      JSON.pretty_generate @queue
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
luo-0.1.3 lib/luo/memory_history.rb
luo-0.1.2 lib/luo/memory_history.rb
luo-0.1.1 lib/luo/memory_history.rb