Class: Luo::MemoryHistory
- Inherits:
-
Object
- Object
- Luo::MemoryHistory
- Includes:
- Configurable
- Defined in:
- lib/luo/memory_history.rb
Overview
用来保存回话历史的简单内存队列
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Instance Method Summary collapse
- #assistant(content) ⇒ Object
- #clone ⇒ Object
- #context_model ⇒ Object
- #dequeue ⇒ Object
-
#enqueue(element) ⇒ Object
(also: #push)
入队.
-
#initialize(context = nil, max_size: config.max_size) ⇒ MemoryHistory
constructor
初始化一个队列.
- #save(input, output) ⇒ Object
- #search(_input) ⇒ Object
- #size ⇒ Object
- #to_a ⇒ Object
- #to_json ⇒ Object
- #user(content) ⇒ Object
Methods included from Configurable
Constructor Details
#initialize(context = nil, max_size: config.max_size) ⇒ MemoryHistory
初始化一个队列
16 17 18 19 20 |
# File 'lib/luo/memory_history.rb', line 16 def initialize(context = nil, max_size: config.max_size) @context = context @queue = [] @max_size = max_size end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
12 13 14 |
# File 'lib/luo/memory_history.rb', line 12 def context @context end |
Instance Method Details
#assistant(content) ⇒ Object
48 49 50 |
# File 'lib/luo/memory_history.rb', line 48 def assistant(content) enqueue({role: "assistant", content: content}) end |
#clone ⇒ Object
31 32 33 |
# File 'lib/luo/memory_history.rb', line 31 def clone Marshal.load(Marshal.dump(self)) end |
#context_model ⇒ Object
40 41 42 |
# File 'lib/luo/memory_history.rb', line 40 def context_model @context_model end |
#dequeue ⇒ Object
54 55 56 |
# File 'lib/luo/memory_history.rb', line 54 def dequeue @queue.shift end |
#enqueue(element) ⇒ Object Also known as: push
入队
24 25 26 27 28 29 |
# File 'lib/luo/memory_history.rb', line 24 def enqueue(element) if @queue.size == @max_size @queue.shift end @queue << element end |
#save(input, output) ⇒ Object
35 36 37 38 |
# File 'lib/luo/memory_history.rb', line 35 def save(input, output) @context_model ||= true enqueue({input: input, output: output}) end |
#search(_input) ⇒ Object
72 73 74 |
# File 'lib/luo/memory_history.rb', line 72 def search(_input) to_a end |
#size ⇒ Object
58 59 60 |
# File 'lib/luo/memory_history.rb', line 58 def size @queue.size end |
#to_a ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/luo/memory_history.rb', line 62 def to_a return @queue unless context_model @queue.reduce([]) do |rt, node| rt << {role: "user", content: node[:input]} rt << {role: "assistant", content: node[:output]} rt end end |
#to_json ⇒ Object
76 77 78 |
# File 'lib/luo/memory_history.rb', line 76 def to_json JSON.pretty_generate @queue end |
#user(content) ⇒ Object
44 45 46 |
# File 'lib/luo/memory_history.rb', line 44 def user(content) enqueue({role: "user", content: content}) end |