Class: Soka::ThoughtsMemory
- Inherits:
-
Object
- Object
- Soka::ThoughtsMemory
- Defined in:
- lib/soka/thoughts_memory.rb
Overview
Stores and manages the thinking process history
Instance Attribute Summary collapse
-
#sessions ⇒ Object
readonly
Returns the value of attribute sessions.
Instance Method Summary collapse
- #add(input, result) ⇒ Object
- #all_sessions ⇒ Object
- #average_iterations ⇒ Object
- #build_stats ⇒ Object
- #clear ⇒ Object
- #empty? ⇒ Boolean
- #failed_sessions ⇒ Object
- #format_stats_string(stats) ⇒ Object
-
#initialize ⇒ ThoughtsMemory
constructor
A new instance of ThoughtsMemory.
- #inspect ⇒ Object
- #last_session ⇒ Object
- #size ⇒ Object
- #successful_sessions ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ ThoughtsMemory
Returns a new instance of ThoughtsMemory.
8 9 10 |
# File 'lib/soka/thoughts_memory.rb', line 8 def initialize @sessions = [] end |
Instance Attribute Details
#sessions ⇒ Object (readonly)
Returns the value of attribute sessions.
6 7 8 |
# File 'lib/soka/thoughts_memory.rb', line 6 def sessions @sessions end |
Instance Method Details
#add(input, result) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/soka/thoughts_memory.rb', line 12 def add(input, result) session = { input: input, thoughts: result.thoughts || [], final_answer: result.final_answer, status: result.status, timestamp: Time.now } session[:error] = result.error if result.error @sessions << session end |
#all_sessions ⇒ Object
30 31 32 |
# File 'lib/soka/thoughts_memory.rb', line 30 def all_sessions @sessions end |
#average_iterations ⇒ Object
54 55 56 57 58 59 |
# File 'lib/soka/thoughts_memory.rb', line 54 def average_iterations return 0 if @sessions.empty? total = @sessions.sum { |s| s[:thoughts].size } total.to_f / @sessions.size end |
#build_stats ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/soka/thoughts_memory.rb', line 69 def build_stats { total: size, successful: successful_sessions.size, failed: failed_sessions.size, avg_iterations: format('%.1f', average_iterations) } end |
#clear ⇒ Object
34 35 36 |
# File 'lib/soka/thoughts_memory.rb', line 34 def clear @sessions.clear end |
#empty? ⇒ Boolean
42 43 44 |
# File 'lib/soka/thoughts_memory.rb', line 42 def empty? @sessions.empty? end |
#failed_sessions ⇒ Object
50 51 52 |
# File 'lib/soka/thoughts_memory.rb', line 50 def failed_sessions @sessions.select { |s| s[:status] == :failed } end |
#format_stats_string(stats) ⇒ Object
78 79 80 81 82 |
# File 'lib/soka/thoughts_memory.rb', line 78 def format_stats_string(stats) "<Soka::ThoughtsMemory> (#{stats[:total]} sessions, " \ "#{stats[:successful]} successful, #{stats[:failed]} failed, " \ "avg iterations: #{stats[:avg_iterations]})" end |
#inspect ⇒ Object
84 85 86 |
# File 'lib/soka/thoughts_memory.rb', line 84 def inspect to_s end |
#last_session ⇒ Object
26 27 28 |
# File 'lib/soka/thoughts_memory.rb', line 26 def last_session @sessions.last end |
#size ⇒ Object
38 39 40 |
# File 'lib/soka/thoughts_memory.rb', line 38 def size @sessions.size end |
#successful_sessions ⇒ Object
46 47 48 |
# File 'lib/soka/thoughts_memory.rb', line 46 def successful_sessions @sessions.select { |s| s[:status] == :success } end |
#to_h ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/soka/thoughts_memory.rb', line 88 def to_h { sessions: @sessions, stats: { total_sessions: size, successful_sessions: successful_sessions.size, failed_sessions: failed_sessions.size, average_iterations: average_iterations } } end |
#to_s ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/soka/thoughts_memory.rb', line 61 def to_s return '<Soka::ThoughtsMemory> (0 sessions)' if empty? stats = build_stats format_stats_string(stats) end |