Class: Soka::Engines::React

Inherits:
Base
  • Object
show all
Includes:
Concerns::ResponseParser, Concerns::ResponseProcessor, Concerns::ResultBuilder, Prompts
Defined in:
lib/soka/engines/react.rb

Overview

ReAct (Reasoning and Acting) engine implementation

Defined Under Namespace

Classes: ReasonResult

Instance Attribute Summary

Attributes inherited from Base

#agent, #custom_instructions, #llm, #max_iterations, #think_in, #tools

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Soka::Engines::Base

Instance Method Details

#iterate_reasoning(context) ⇒ ReasonResult?

Iterate through reasoning cycles

Parameters:

Returns:

  • (ReasonResult, nil)

    The result if found, nil otherwise



41
42
43
44
45
46
47
48
49
50
# File 'lib/soka/engines/react.rb', line 41

def iterate_reasoning(context)
  max_iterations.times do |index|
    context.iteration = index # Set current iteration number
    result = process_iteration(context)
    return result if result
  end
  # When max iterations reached, ensure iteration count is correct
  context.iteration = max_iterations
  nil
end

#max_iterations_result(context) ⇒ ReasonResult

Build result when max iterations reached

Parameters:

Returns:



55
56
57
58
59
60
61
62
63
# File 'lib/soka/engines/react.rb', line 55

def max_iterations_result(context)
  context.emit_event(:error, "Maximum iterations (#{context.max_iterations}) reached")
  build_result(
    input: context.task,
    thoughts: context.thoughts,
    final_answer: "I couldn't complete the task within the maximum number of iterations.",
    status: :max_iterations_reached
  )
end

#reason(task) {|event| ... } ⇒ ReasonResult

Main reasoning entry point

Parameters:

  • task (String)

    The task to process

Yields:

  • (event)

    Optional block to handle events during execution

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/soka/engines/react.rb', line 23

def reason(task, &block)
  start_time = Time.now
  context = ReasoningContext.new(task: task, event_handler: block, max_iterations: max_iterations,
                                 think_in: think_in)
  context.messages = build_messages(task)

  result = iterate_reasoning(context)
  result ||= max_iterations_result(context)

  # Add execution time to the result
  execution_time = Time.now - start_time
  result.execution_time = execution_time if result.respond_to?(:execution_time=)
  result
end