Class: Desiru::Modules::ReAct

Inherits:
Desiru::Module show all
Defined in:
lib/desiru/modules/react.rb

Overview

ReAct (Reasoning and Acting) module for tool-using AI agents This module allows the language model to iteratively reason about a task and use tools to gather information before producing a final answer

Instance Attribute Summary collapse

Attributes inherited from Desiru::Module

#config, #demos, #metadata, #model, #signature

Instance Method Summary collapse

Methods inherited from Desiru::Module

#call, #reset, #to_h, #with_demos

Methods included from AsyncCapable

#call_async, #call_batch_async

Methods included from ErrorHandling

#safe_execute, #with_error_context, #with_retry

Methods included from Core::Traceable

#call, #disable_trace!, #enable_trace!, #trace_enabled?

Constructor Details

#initialize(signature, tools: [], max_iterations: 5, model: nil) ⇒ ReAct

Returns a new instance of ReAct.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/desiru/modules/react.rb', line 14

def initialize(signature, tools: [], max_iterations: 5, model: nil)
  super(signature, model: model)
  @tools = normalize_tools(tools)
  @max_iterations = max_iterations

  # Build the ReAct signature for reasoning and tool selection
  react_signature = build_react_signature
  @react_module = ChainOfThought.new(react_signature, model: @model)

  # Build extraction signature for final output
  extract_signature = build_extract_signature
  @extract_module = ChainOfThought.new(extract_signature, model: @model)
end

Instance Attribute Details

#extract_moduleObject (readonly)

Returns the value of attribute extract_module.



12
13
14
# File 'lib/desiru/modules/react.rb', line 12

def extract_module
  @extract_module
end

#max_iterationsObject (readonly)

Returns the value of attribute max_iterations.



12
13
14
# File 'lib/desiru/modules/react.rb', line 12

def max_iterations
  @max_iterations
end

#react_moduleObject (readonly)

Returns the value of attribute react_module.



12
13
14
# File 'lib/desiru/modules/react.rb', line 12

def react_module
  @react_module
end

#toolsObject (readonly)

Returns the value of attribute tools.



12
13
14
# File 'lib/desiru/modules/react.rb', line 12

def tools
  @tools
end

Instance Method Details

#forward(inputs) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/desiru/modules/react.rb', line 28

def forward(inputs)
  trajectory = []

  max_iterations.times do |_iteration|
    # Get the next action from the model
    react_inputs = prepare_react_inputs(inputs, trajectory)
    react_output = react_module.call(react_inputs)

    # Extract the tool name and arguments
    tool_name = react_output[:next_tool_name]
    tool_args = parse_tool_args(react_output[:next_tool_args])

    # Add reasoning to trajectory
    trajectory << {
      thought: react_output[:next_thought],
      tool: tool_name,
      args: tool_args
    }

    # Check if we're done
    break if tool_name == "finish"

    # Execute the tool
    begin
      tool_result = execute_tool(tool_name, tool_args)
      trajectory.last[:observation] = tool_result
    rescue StandardError => e
      trajectory.last[:observation] = "Error: #{e.message}"
    end
  end

  # Extract final outputs from trajectory
  extract_inputs = prepare_extract_inputs(inputs, trajectory)
  extract_module.call(extract_inputs)
end