Class: DSPy::CodeAct

Inherits:
Predict
  • Object
show all
Extended by:
T::Sig
Includes:
Mixins::StructBuilder
Defined in:
lib/dspy/code_act.rb,
lib/dspy/code_act.rb

Overview

CodeAct Agent using Think-Code-Observe pattern

Constant Summary collapse

VERSION =
DSPy::CodeActVersion::VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(signature_class, max_iterations: 10) ⇒ CodeAct

Returns a new instance of CodeAct.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/dspy/code_act.rb', line 107

def initialize(signature_class, max_iterations: 10)
  @original_signature_class = signature_class
  @max_iterations = max_iterations
  @execution_context = T.let({}, T::Hash[Symbol, T.untyped])

  # Create code generator using Predict to preserve field descriptions
  @code_generator = T.let(DSPy::Predict.new(RubyCodeGeneration), DSPy::Predict)

  # Create observation processor using Predict to preserve field descriptions
  @observation_processor = T.let(DSPy::Predict.new(RubyCodeObservation), DSPy::Predict)

  # Create enhanced output struct with CodeAct 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 CodeAct fields
    @output_struct_class = enhanced_output_struct

    # Store original signature name
    @original_signature_name = signature_class.name

    class << self
      attr_reader :input_struct_class, :output_struct_class, :original_signature_name
      
      # Override name to return the original signature name
      def name
        @original_signature_name || super
      end
    end
  end

  # Call parent constructor with enhanced signature
  super(enhanced_signature)
end

Instance Attribute Details

#enhanced_output_structObject (readonly)

Returns the value of attribute enhanced_output_struct.



98
99
100
# File 'lib/dspy/code_act.rb', line 98

def enhanced_output_struct
  @enhanced_output_struct
end

#execution_contextObject (readonly)

Returns the value of attribute execution_context.



104
105
106
# File 'lib/dspy/code_act.rb', line 104

def execution_context
  @execution_context
end

#max_iterationsObject (readonly)

Returns the value of attribute max_iterations.



101
102
103
# File 'lib/dspy/code_act.rb', line 101

def max_iterations
  @max_iterations
end

#original_signature_classObject (readonly)

Returns the value of attribute original_signature_class.



95
96
97
# File 'lib/dspy/code_act.rb', line 95

def original_signature_class
  @original_signature_class
end

Instance Method Details

#forward(**kwargs) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/dspy/code_act.rb', line 164

def forward(**kwargs)
  # Validate input and serialize all fields as task context
  input_struct = @original_signature_class.input_struct_class.new(**kwargs)
  task = DSPy::TypeSerializer.serialize(input_struct).to_json

  # Execute CodeAct reasoning loop
  reasoning_result = execute_codeact_reasoning_loop(task)

  # Create enhanced output with all CodeAct data
  create_enhanced_result(kwargs, reasoning_result)
end

#named_predictorsObject



151
152
153
154
155
156
# File 'lib/dspy/code_act.rb', line 151

def named_predictors
  pairs = T.let([], T::Array[[String, DSPy::Module]])
  pairs << ["code_generator", @code_generator]
  pairs << ["observation_processor", @observation_processor]
  pairs
end

#predictorsObject



159
160
161
# File 'lib/dspy/code_act.rb', line 159

def predictors
  named_predictors.map { |(_, predictor)| predictor }
end