Class: Intelligent::SequentialThinking

Inherits:
Object
  • Object
show all
Defined in:
lib/intelligent/sequential_thinking.rb

Instance Method Summary collapse

Constructor Details

#initialize(model: "claude-sonnet-4-20250514") ⇒ SequentialThinking

Returns a new instance of SequentialThinking.



3
4
5
6
# File 'lib/intelligent/sequential_thinking.rb', line 3

def initialize(model: "claude-sonnet-4-20250514")
  @model = model
  @anthropic_service = Llm::Anthropic.new(model: model)
end

Instance Method Details

#think_through_problem(problem_description, files = [], max_thoughts = 5) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/intelligent/sequential_thinking.rb', line 8

def think_through_problem(problem_description, files = [], max_thoughts = 5)
  thoughts = []
  current_thought = 1
  total_thoughts = max_thoughts

  loop do
    # Build the thinking prompt
    thinking_prompt = build_thinking_prompt(problem_description, thoughts, current_thought, total_thoughts)

    # Generate the next thought with files
    result = @anthropic_service.generate(thinking_prompt, files)

    if result[:success]
      thought_content = result[:text]
      thoughts << {
        number: current_thought,
        content: thought_content,
        timestamp: Time.now
      }

      # Check if we need more thoughts
      needs_more = analyze_thought_completeness(thought_content, current_thought, total_thoughts)

      break if !needs_more || current_thought >= total_thoughts
      current_thought += 1
    else
      return { success: false, error: result[:error] }
    end
  end

  # Generate final answer directly from thinking process
  final_answer = generate_final_answer(problem_description, thoughts, files)

  if final_answer[:success]
    {
      success: true,
      thoughts: thoughts,
      final_text: final_answer[:text]
    }
  else
    { success: false, error: final_answer[:error] }
  end
end