Class: DSPy::Prediction

Inherits:
Object
  • Object
show all
Extended by:
T::Generic, T::Sig
Includes:
T::Props, T::Props::Serializable
Defined in:
lib/dspy/prediction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema = nil, **attributes) ⇒ Prediction

Returns a new instance of Prediction.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dspy/prediction.rb', line 25

def initialize(schema = nil, **attributes)
  @_schema = extract_struct_class(schema)
  
  # Convert attributes based on schema if provided
  converted_attributes = if @_schema
    convert_attributes_with_schema(attributes)
  else
    attributes
  end

  # Create a dynamic struct to hold the data
  struct_class = create_dynamic_struct(converted_attributes)
  @_struct = struct_class.new(**converted_attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/dspy/prediction.rb', line 42

def method_missing(method, *args, &block)
  if @_struct.respond_to?(method)
    @_struct.send(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#_schemaObject (readonly)

Returns the value of attribute _schema.



17
18
19
# File 'lib/dspy/prediction.rb', line 17

def _schema
  @_schema
end

#_structObject (readonly)

Returns the value of attribute _struct.



13
14
15
# File 'lib/dspy/prediction.rb', line 13

def _struct
  @_struct
end

Instance Method Details

#respond_to_missing?(method, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/dspy/prediction.rb', line 51

def respond_to_missing?(method, include_all = false)
  @_struct.respond_to?(method, include_all) || super
end

#to_hObject



56
57
58
# File 'lib/dspy/prediction.rb', line 56

def to_h
  @_struct.serialize
end