Class: DSPy::Predict

Inherits:
Module show all
Defined in:
lib/dspy/predict.rb

Direct Known Subclasses

ChainOfThought

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Module

#call

Constructor Details

#initialize(signature_class) ⇒ Predict

Returns a new instance of Predict.



14
15
16
# File 'lib/dspy/predict.rb', line 14

def initialize(signature_class)
  @signature_class = signature_class
end

Instance Attribute Details

#signature_classObject (readonly)

Returns the value of attribute signature_class.



12
13
14
# File 'lib/dspy/predict.rb', line 12

def signature_class
  @signature_class
end

Instance Method Details

#forward(**input_values) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/dspy/predict.rb', line 61

def forward(**input_values)
  DSPy.logger.info( module: self.class.to_s, **input_values)
  result = @signature_class.input_schema.call(input_values)
  if result.success?
    output_attributes = lm.chat(self, input_values)
    poro_class = Data.define(*output_attributes.keys)
    return poro_class.new(*output_attributes.values)
  end
  raise PredictionInvalidError.new(result.errors)
end

#lmObject



57
58
59
# File 'lib/dspy/predict.rb', line 57

def lm
  DSPy.config.lm
end

#system_signatureObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dspy/predict.rb', line 18

def system_signature
  <<-PROMPT
  Your input schema fields are:
    ```json
     #{JSON.generate(@signature_class.input_schema.json_schema)}
    ```
  Your output schema fields are:
    ```json
      #{JSON.generate(@signature_class.output_schema.json_schema)}
    ````
  All interactions will be structured in the following way, with the appropriate values filled in.

  ## Input values
    ```json
      {input_values}
    ```  
  ## Output values
  Respond exclusively with the output schema fields in the json block below.
    ```json
      {output_values}
    ```
  
  In adhering to this structure, your objective is: #{@signature_class.description}

  PROMPT
end

#user_signature(input_values) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dspy/predict.rb', line 45

def user_signature(input_values)
  <<-PROMPT
    ## Input Values
    ```json
    #{JSON.generate(input_values)}
    ```     
    
    Respond with the corresponding output schema fields wrapped in a ```json ``` block, 
     starting with the heading `## Output values`.
  PROMPT
end