Class: Desiru::Modules::BestOfN
- Inherits:
-
Desiru::Module
- Object
- Desiru::Module
- Desiru::Modules::BestOfN
- Defined in:
- lib/desiru/modules/best_of_n.rb
Overview
BestOfN module that samples N outputs from a predictor and selects the best one based on configurable criteria (confidence, consistency, or external validation)
Constant Summary collapse
- SELECTION_CRITERIA =
i[confidence consistency llm_judge custom].freeze
- DEFAULT_SIGNATURE =
'question: string -> answer: string'
Instance Attribute Summary
Attributes inherited from Desiru::Module
#config, #demos, #metadata, #model, #signature
Instance Method Summary collapse
- #forward(**inputs) ⇒ Object
-
#initialize(signature = nil, model: nil, **kwargs) ⇒ BestOfN
constructor
A new instance of BestOfN.
Methods inherited from Desiru::Module
#call, #reset, #to_h, #with_demos
Methods included from AsyncCapable
#call_async, #call_batch_async
Methods included from ErrorHandling
#safe_execute, #with_error_context, #with_retry
Methods included from Core::Traceable
#call, #disable_trace!, #enable_trace!, #trace_enabled?
Constructor Details
#initialize(signature = nil, model: nil, **kwargs) ⇒ BestOfN
Returns a new instance of BestOfN.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/desiru/modules/best_of_n.rb', line 12 def initialize(signature = nil, model: nil, **kwargs) # Extract our specific options before passing to parent @n_samples = kwargs.delete(:n_samples) || 5 @selection_criterion = validate_criterion(kwargs.delete(:selection_criterion) || :consistency) @temperature = kwargs.delete(:temperature) || 0.7 @custom_selector = kwargs.delete(:custom_selector) # Proc that takes array of results @base_module = kwargs.delete(:base_module) || Modules::Predict = kwargs.delete(:include_metadata) || false # Use default signature if none provided signature ||= DEFAULT_SIGNATURE # Pass remaining kwargs to parent (config, demos, metadata) super end |
Instance Method Details
#forward(**inputs) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/desiru/modules/best_of_n.rb', line 28 def forward(**inputs) # Generate N samples samples = generate_samples(inputs) # Select the best sample based on criterion best_sample = select_best(samples, inputs) # Include metadata if requested if || signature.output_fields.key?(:selection_metadata) best_sample[:selection_metadata] = (samples, best_sample) end # Clean up internal fields best_sample.delete(:_confidence_score) best_sample rescue ArgumentError => e # Re-raise ArgumentError for missing custom selector raise e rescue StandardError => e Desiru.logger.error("BestOfN error: #{e.message}") # Fallback to single sample fallback_sample(inputs) end |