Class: DSPy::ReAct
- Extended by:
- T::Sig
- Defined in:
- lib/dspy/re_act.rb
Overview
ReAct Agent using Sorbet signatures
Constant Summary collapse
- FINISH_ACTION =
"finish"
Instance Attribute Summary collapse
-
#enhanced_output_struct ⇒ Object
readonly
Returns the value of attribute enhanced_output_struct.
-
#max_iterations ⇒ Object
readonly
Returns the value of attribute max_iterations.
-
#original_signature_class ⇒ Object
readonly
Returns the value of attribute original_signature_class.
-
#tools ⇒ Object
readonly
Returns the value of attribute tools.
Attributes inherited from Predict
Instance Method Summary collapse
- #forward(**kwargs) ⇒ Object
-
#initialize(signature_class, tools: [], max_iterations: 5) ⇒ ReAct
constructor
A new instance of ReAct.
Methods inherited from Predict
#add_examples, #forward_untyped, #system_signature, #user_signature, #with_examples, #with_instruction, #with_prompt
Methods inherited from Module
#call, #call_untyped, #forward_untyped, #lm
Constructor Details
#initialize(signature_class, tools: [], max_iterations: 5) ⇒ ReAct
Returns a new instance of ReAct.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/dspy/re_act.rb', line 87 def initialize(signature_class, tools: [], max_iterations: 5) @original_signature_class = signature_class @tools = T.let({}, T::Hash[String, T.untyped]) tools.each { |tool| @tools[tool.name.downcase] = tool } @max_iterations = max_iterations # Create dynamic signature classes that include the original input fields thought_signature = create_thought_signature(signature_class) observation_signature = create_observation_signature(signature_class) # Create thought generator using Predict to preserve field descriptions @thought_generator = T.let(DSPy::Predict.new(thought_signature), DSPy::Predict) # Create observation processor using Predict to preserve field descriptions @observation_processor = T.let(DSPy::Predict.new(observation_signature), DSPy::Predict) # Create enhanced output struct with ReAct fields @enhanced_output_struct = create_enhanced_output_struct(signature_class) enhanced_output_struct = @enhanced_output_struct # Create enhanced signature class enhanced_signature = Class.new(DSPy::Signature) do # Set the description description signature_class.description # Use the same input struct @input_struct_class = signature_class.input_struct_class # Use the enhanced output struct with ReAct fields @output_struct_class = enhanced_output_struct class << self attr_reader :input_struct_class, :output_struct_class end end # Call parent constructor with enhanced signature super(enhanced_signature) end |
Instance Attribute Details
#enhanced_output_struct ⇒ Object (readonly)
Returns the value of attribute enhanced_output_struct.
77 78 79 |
# File 'lib/dspy/re_act.rb', line 77 def enhanced_output_struct @enhanced_output_struct end |
#max_iterations ⇒ Object (readonly)
Returns the value of attribute max_iterations.
83 84 85 |
# File 'lib/dspy/re_act.rb', line 83 def max_iterations @max_iterations end |
#original_signature_class ⇒ Object (readonly)
Returns the value of attribute original_signature_class.
74 75 76 |
# File 'lib/dspy/re_act.rb', line 74 def original_signature_class @original_signature_class end |
#tools ⇒ Object (readonly)
Returns the value of attribute tools.
80 81 82 |
# File 'lib/dspy/re_act.rb', line 80 def tools @tools end |
Instance Method Details
#forward(**kwargs) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/dspy/re_act.rb', line 128 def forward(**kwargs) lm = config.lm || DSPy.config.lm available_tools = @tools.keys # Instrument the entire ReAct agent lifecycle result = instrument_prediction('dspy.react', @original_signature_class, kwargs, { max_iterations: @max_iterations, available_tools: available_tools }) do # Validate input input_struct = @original_signature_class.input_struct_class.new(**kwargs) # Execute ReAct reasoning loop reasoning_result = execute_react_reasoning_loop(input_struct) # Create enhanced output with all ReAct data create_enhanced_result(kwargs, reasoning_result) end result end |