Class: SmartPrompt::HybridStrategy
- Inherits:
-
Object
- Object
- SmartPrompt::HybridStrategy
- Includes:
- ContextStrategy
- Defined in:
- lib/smart_prompt/hybrid_strategy.rb
Overview
HybridStrategy implements a flexible context selection strategy that combines multiple strategies for optimal results
This strategy supports two modes:
-
Adaptive mode: Automatically selects the best strategy based on message count
-
Combined mode: Merges results from multiple strategies
Adaptive mode selection logic:
-
< 20 messages: Use SlidingWindowStrategy (simple and efficient)
-
20-50 messages: Use RelevanceBasedStrategy (balance recency and relevance)
-
> 50 messages: Use SummaryBasedStrategy (compress older messages)
Combined mode:
-
Runs multiple strategies and merges their results
-
Removes duplicates and sorts by importance
-
Provides comprehensive context from different perspectives
Instance Method Summary collapse
-
#initialize(config = {}) ⇒ HybridStrategy
constructor
Initialize the hybrid strategy.
-
#select_messages(messages, max_tokens, current_message = nil) ⇒ Array<Message>
Select messages using hybrid approach.
-
#should_compress?(session) ⇒ Boolean
Determine if compression should be triggered Uses the most conservative threshold from all strategies.
Constructor Details
#initialize(config = {}) ⇒ HybridStrategy
Initialize the hybrid strategy
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/smart_prompt/hybrid_strategy.rb', line 34 def initialize(config = {}) @mode = config[:mode] || :adaptive @adaptive_threshold_low = config[:adaptive_threshold_low] || 20 @adaptive_threshold_high = config[:adaptive_threshold_high] || 50 # Initialize sub-strategies with their configurations @sliding_window = SlidingWindowStrategy.new(config[:sliding_window] || {}) @relevance_based = RelevanceBasedStrategy.new(config[:relevance_based] || {}) @summary_based = SummaryBasedStrategy.new(config[:summary_based] || {}) # Validate mode unless [:adaptive, :combined].include?(@mode) raise ArgumentError, "Invalid mode: #{@mode}. Must be :adaptive or :combined" end end |
Instance Method Details
#select_messages(messages, max_tokens, current_message = nil) ⇒ Array<Message>
Select messages using hybrid approach
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/smart_prompt/hybrid_strategy.rb', line 55 def (, max_tokens, = nil) return [] if .nil? || .empty? case @mode when :adaptive select_adaptive(, max_tokens, ) when :combined select_combined(, max_tokens, ) else # Fallback to sliding window if mode is somehow invalid @sliding_window.(, max_tokens, ) end end |
#should_compress?(session) ⇒ Boolean
Determine if compression should be triggered Uses the most conservative threshold from all strategies
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/smart_prompt/hybrid_strategy.rb', line 73 def should_compress?(session) return false if session.nil? # In adaptive mode, use the threshold of the currently selected strategy if @mode == :adaptive = session. if < @adaptive_threshold_low @sliding_window.should_compress?(session) elsif < @adaptive_threshold_high @relevance_based.should_compress?(session) else @summary_based.should_compress?(session) end else # In combined mode, compress if any strategy recommends it @sliding_window.should_compress?(session) || @relevance_based.should_compress?(session) || @summary_based.should_compress?(session) end end |