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.



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

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



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

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.



19
20
21
# File 'lib/dspy/prediction.rb', line 19

def _schema
  @_schema
end

#_structObject (readonly)

Returns the value of attribute _struct.



15
16
17
# File 'lib/dspy/prediction.rb', line 15

def _struct
  @_struct
end

Instance Method Details

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

Returns:

  • (Boolean)


53
54
55
# File 'lib/dspy/prediction.rb', line 53

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

#to_hObject



58
59
60
61
62
# File 'lib/dspy/prediction.rb', line 58

def to_h
  hash = DSPy::Utils::Serialization.deep_serialize(@_struct.serialize)
  hash.delete('_prediction_marker')
  hash
end

#to_json(*args) ⇒ Object



65
66
67
# File 'lib/dspy/prediction.rb', line 65

def to_json(*args)
  to_h.to_json(*args)
end