Class: DSPy::Module

Inherits:
Object
  • Object
show all
Extended by:
T::Generic, T::Sig
Includes:
Dry::Configurable
Defined in:
lib/dspy/module.rb

Direct Known Subclasses

Predict

Instance Method Summary collapse

Instance Method Details

#call(**input_values) ⇒ Object



60
61
62
# File 'lib/dspy/module.rb', line 60

def call(**input_values)
  forward(**input_values)
end

#call_untyped(**input_values) ⇒ Object



66
67
68
# File 'lib/dspy/module.rb', line 66

def call_untyped(**input_values)
  forward_untyped(**input_values)
end

#forward(**input_values) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dspy/module.rb', line 24

def forward(**input_values)
  # Create span for this module's execution
  observation_type = DSPy::ObservationType.for_module_class(self.class)
  DSPy::Context.with_span(
    operation: "#{self.class.name}.forward",
    **observation_type.langfuse_attributes,
    'langfuse.observation.input' => input_values.to_json,
    'dspy.module' => self.class.name
  ) do |span|
    result = forward_untyped(**input_values)
    
    # Add output to span
    if span && result
      output_json = result.respond_to?(:to_h) ? result.to_h.to_json : result.to_json rescue result.to_s
      span.set_attribute('langfuse.observation.output', output_json)
    end
    
    # Cast the result of forward_untyped to the expected output type
    T.cast(result, T.type_parameter(:O))
  end
end

#forward_untyped(**input_values) ⇒ Object

Raises:

  • (NotImplementedError)


48
49
50
# File 'lib/dspy/module.rb', line 48

def forward_untyped(**input_values)
  raise NotImplementedError, "Subclasses must implement forward_untyped method"
end

#lmObject



72
73
74
# File 'lib/dspy/module.rb', line 72

def lm
  config.lm || DSPy.current_lm
end