Class: DSPy::FewShotExample

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dspy/few_shot_example.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input:, output:, reasoning: nil) ⇒ FewShotExample

Returns a new instance of FewShotExample.



25
26
27
28
29
# File 'lib/dspy/few_shot_example.rb', line 25

def initialize(input:, output:, reasoning: nil)
  @input = input.freeze
  @output = output.freeze
  @reasoning = reasoning
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



10
11
12
# File 'lib/dspy/few_shot_example.rb', line 10

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



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

def output
  @output
end

#reasoningObject (readonly)

Returns the value of attribute reasoning.



16
17
18
# File 'lib/dspy/few_shot_example.rb', line 16

def reasoning
  @reasoning
end

Class Method Details

.from_h(hash) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/dspy/few_shot_example.rb', line 64

def self.from_h(hash)
  new(
    input: hash[:input] || {},
    output: hash[:output] || {},
    reasoning: hash[:reasoning]
  )
end

Instance Method Details

#==(other) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/dspy/few_shot_example.rb', line 73

def ==(other)
  return false unless other.is_a?(FewShotExample)
  
  @input == other.input &&
    @output == other.output &&
    @reasoning == other.reasoning
end

#to_hObject



54
55
56
57
58
59
60
61
# File 'lib/dspy/few_shot_example.rb', line 54

def to_h
  result = {
    input: @input,
    output: @output
  }
  result[:reasoning] = @reasoning if @reasoning
  result
end

#to_prompt_sectionObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dspy/few_shot_example.rb', line 32

def to_prompt_section
  sections = []
  
  sections << "## Input"
  sections << "```json"
  sections << JSON.pretty_generate(@input)
  sections << "```"
  
  if @reasoning
    sections << "## Reasoning"
    sections << @reasoning
  end
  
  sections << "## Output"
  sections << "```json"
  sections << JSON.pretty_generate(@output)
  sections << "```"
  
  sections.join("\n")
end