Class: Soka::ThoughtsMemory

Inherits:
Object
  • Object
show all
Defined in:
lib/soka/thoughts_memory.rb

Overview

Stores and manages the thinking process history

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeThoughtsMemory

Returns a new instance of ThoughtsMemory.



8
9
10
# File 'lib/soka/thoughts_memory.rb', line 8

def initialize
  @sessions = []
end

Instance Attribute Details

#sessionsObject (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_sessionsObject



30
31
32
# File 'lib/soka/thoughts_memory.rb', line 30

def all_sessions
  @sessions
end

#average_iterationsObject



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_statsObject



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

#clearObject



34
35
36
# File 'lib/soka/thoughts_memory.rb', line 34

def clear
  @sessions.clear
end

#empty?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/soka/thoughts_memory.rb', line 42

def empty?
  @sessions.empty?
end

#failed_sessionsObject



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

#inspectObject



84
85
86
# File 'lib/soka/thoughts_memory.rb', line 84

def inspect
  to_s
end

#last_sessionObject



26
27
28
# File 'lib/soka/thoughts_memory.rb', line 26

def last_session
  @sessions.last
end

#sizeObject



38
39
40
# File 'lib/soka/thoughts_memory.rb', line 38

def size
  @sessions.size
end

#successful_sessionsObject



46
47
48
# File 'lib/soka/thoughts_memory.rb', line 46

def successful_sessions
  @sessions.select { |s| s[:status] == :success }
end

#to_hObject



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_sObject



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