Class: Aidp::PromptOptimization::ContextComposer
- Inherits:
-
Object
- Object
- Aidp::PromptOptimization::ContextComposer
- Defined in:
- lib/aidp/prompt_optimization/context_composer.rb
Overview
Composes optimal context from scored fragments within token budget
Selects the best combination of fragments that fits within the token budget while maximizing relevance and coverage.
Algorithm:
-
Sort fragments by relevance score
-
Always include critical fragments (score > 0.9)
-
Fill remaining budget with highest-scoring fragments
-
Deduplicate overlapping content
-
Return selected fragments with statistics
Constant Summary collapse
- CRITICAL_SCORE_THRESHOLD =
Thresholds for different fragment types
0.9- MINIMUM_SCORE_THRESHOLD =
0.3
Instance Attribute Summary collapse
-
#max_tokens ⇒ Object
readonly
Returns the value of attribute max_tokens.
Instance Method Summary collapse
-
#compose(scored_fragments, thresholds: {}, reserved_tokens: 2000) ⇒ CompositionResult
Compose optimal context from scored fragments.
-
#initialize(max_tokens: 16000) ⇒ ContextComposer
constructor
A new instance of ContextComposer.
Constructor Details
#initialize(max_tokens: 16000) ⇒ ContextComposer
Returns a new instance of ContextComposer.
27 28 29 |
# File 'lib/aidp/prompt_optimization/context_composer.rb', line 27 def initialize(max_tokens: 16000) @max_tokens = max_tokens end |
Instance Attribute Details
#max_tokens ⇒ Object (readonly)
Returns the value of attribute max_tokens.
21 22 23 |
# File 'lib/aidp/prompt_optimization/context_composer.rb', line 21 def max_tokens @max_tokens end |
Instance Method Details
#compose(scored_fragments, thresholds: {}, reserved_tokens: 2000) ⇒ CompositionResult
Compose optimal context from scored fragments
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/aidp/prompt_optimization/context_composer.rb', line 37 def compose(scored_fragments, thresholds: {}, reserved_tokens: 2000) available_budget = @max_tokens - reserved_tokens # Separate fragments by type for threshold checking categorized = categorize_fragments(scored_fragments, thresholds) # Start with critical fragments (always included) selected = select_critical_fragments(categorized[:critical]) used_tokens = calculate_total_tokens(selected) # Add high-priority fragments within budget selected, used_tokens = add_fragments_within_budget( selected, categorized[:high_priority], available_budget - used_tokens ) # Fill remaining budget with other fragments if space allows selected, used_tokens = add_fragments_within_budget( selected, categorized[:medium_priority], available_budget - used_tokens ) # Deduplicate if requested selected = deduplicate_fragments(selected) if categorized[:needs_dedup] CompositionResult.new( selected_fragments: selected, total_tokens: used_tokens, budget: available_budget, excluded_count: scored_fragments.length - selected.length, average_score: calculate_average_score(selected) ) end |