Class: DSPy::ChainOfThought
Overview
Enhances prediction by encouraging step-by-step reasoning before providing a final answer using Sorbet signatures.
Constant Summary collapse
- FieldDescriptor =
DSPy::Signature::FieldDescriptor
Constants inherited from Module
Module::DEFAULT_MODULE_SUBSCRIPTION_SCOPE
Instance Attribute Summary collapse
-
#original_signature ⇒ Object
readonly
Returns the value of attribute original_signature.
Attributes inherited from Predict
#demos, #prompt, #signature_class
Instance Method Summary collapse
- #forward_untyped(**input_values) ⇒ Object
-
#initialize(signature_class) ⇒ ChainOfThought
constructor
A new instance of ChainOfThought.
- #with_examples(examples) ⇒ Object
- #with_instruction(instruction) ⇒ Object
- #with_prompt(new_prompt) ⇒ Object
Methods inherited from Predict
#add_examples, #configure, from_h, #named_predictors, #predictors, #system_signature, #user_signature
Methods included from Mixins::TypeCoercion
deserialize_enum, enum_type?, extract_enum_class
Methods inherited from Module
#call, #call_untyped, #configure, #configure_predictor, #dup_for_thread, #forward, inherited, #lm, #module_scope_id, #module_scope_label, #module_scope_label=, module_subscription_specs, #named_predictors, #predictors, #registered_module_subscriptions, #save, subscribe, #to_h, #unsubscribe_module_events
Methods included from Callbacks
Constructor Details
#initialize(signature_class) ⇒ ChainOfThought
Returns a new instance of ChainOfThought.
20 21 22 23 24 25 26 27 |
# File 'lib/dspy/chain_of_thought.rb', line 20 def initialize(signature_class) @original_signature = signature_class enhanced_signature = build_enhanced_signature(signature_class) # Call parent constructor with enhanced signature super(enhanced_signature) @signature_class = enhanced_signature end |
Instance Attribute Details
#original_signature ⇒ Object (readonly)
Returns the value of attribute original_signature.
86 87 88 |
# File 'lib/dspy/chain_of_thought.rb', line 86 def original_signature @original_signature end |
Instance Method Details
#forward_untyped(**input_values) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/dspy/chain_of_thought.rb', line 91 def forward_untyped(**input_values) # Create a Predict instance and call its forward method (which will create Predict span via Module#forward) # We can't call super.forward because that would go to Module#forward_untyped, not Module#forward # Create a temporary Predict instance with our enhanced signature to get the prediction predict_instance = DSPy::Predict.new(@signature_class) predict_instance.configure { |c| c.lm = self.lm } # Use the same LM configuration # Call predict's forward method, which will create the Predict span prediction_result = predict_instance.forward(**input_values) # Add ChainOfThought-specific analysis and events if DSPy::Observability.enabled? && prediction_result # Add reasoning metrics via events if prediction_result.respond_to?(:reasoning) && prediction_result.reasoning DSPy.event('chain_of_thought.reasoning_metrics', { 'cot.reasoning_length' => prediction_result.reasoning.length, 'cot.has_reasoning' => true, 'cot.reasoning_steps' => count_reasoning_steps(prediction_result.reasoning), 'dspy.module_type' => 'chain_of_thought', 'dspy.signature' => @original_signature.name }) end end # Analyze reasoning (emits events for backwards compatibility) analyze_reasoning(prediction_result) prediction_result end |
#with_examples(examples) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/dspy/chain_of_thought.rb', line 65 def with_examples(examples) # Convert examples to include reasoning if they don't have it enhanced_examples = examples.map do |example| if example.reasoning.nil? || example.reasoning.empty? # Try to extract reasoning from the output if it contains a reasoning field reasoning = example.output[:reasoning] || "Step by step reasoning for this example." DSPy::FewShotExample.new( input: example.input, output: example.output, reasoning: reasoning ) else example end end super(enhanced_examples) end |
#with_instruction(instruction) ⇒ Object
59 60 61 62 |
# File 'lib/dspy/chain_of_thought.rb', line 59 def with_instruction(instruction) enhanced_instruction = ensure_chain_of_thought_instruction(instruction) super(enhanced_instruction) end |
#with_prompt(new_prompt) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/dspy/chain_of_thought.rb', line 32 def with_prompt(new_prompt) # Create a new ChainOfThought with the same original signature instance = self.class.new(@original_signature) # Ensure the instruction includes "Think step by step" if not already present enhanced_instruction = if new_prompt.instruction.include?("Think step by step") new_prompt.instruction else "#{new_prompt.instruction} Think step by step." end # Create enhanced prompt with ChainOfThought-specific schemas enhanced_prompt = Prompt.new( instruction: enhanced_instruction, input_schema: @signature_class.input_json_schema, output_schema: @signature_class.output_json_schema, few_shot_examples: new_prompt.few_shot_examples, signature_class_name: @signature_class.name, schema_format: new_prompt.schema_format, data_format: new_prompt.data_format ) instance.instance_variable_set(:@prompt, enhanced_prompt) instance end |