Class: Aidp::PromptOptimization::Optimizer
- Inherits:
-
Object
- Object
- Aidp::PromptOptimization::Optimizer
- Defined in:
- lib/aidp/prompt_optimization/optimizer.rb
Overview
Main coordinator for prompt optimization
Orchestrates all components to produce an optimized prompt:
- Index style guide, templates, and source code
- Score fragments based on task context
- Select optimal fragments within token budget
- Build final prompt markdown
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#project_dir ⇒ Object
readonly
Returns the value of attribute project_dir.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
Instance Method Summary collapse
-
#clear_cache ⇒ Object
Clear cached indexes (useful for testing or when files change).
-
#initialize(project_dir:, config: nil) ⇒ Optimizer
constructor
A new instance of Optimizer.
-
#optimize_prompt(task_type: nil, description: nil, affected_files: [], step_name: nil, tags: [], options: {}) ⇒ PromptOutput
Optimize prompt for given task context.
-
#statistics ⇒ Hash
Get optimization statistics.
Constructor Details
#initialize(project_dir:, config: nil) ⇒ Optimizer
Returns a new instance of Optimizer.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/aidp/prompt_optimization/optimizer.rb', line 33 def initialize(project_dir:, config: nil) @project_dir = project_dir @config = config || default_config @stats = OptimizerStats.new # Initialize indexers (will cache results) @style_guide_indexer = nil @template_indexer = nil @fragmenter = nil @project_knowledge_indexer = nil @scorer = nil @composer = nil @builder = nil end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
31 32 33 |
# File 'lib/aidp/prompt_optimization/optimizer.rb', line 31 def config @config end |
#project_dir ⇒ Object (readonly)
Returns the value of attribute project_dir.
31 32 33 |
# File 'lib/aidp/prompt_optimization/optimizer.rb', line 31 def project_dir @project_dir end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
31 32 33 |
# File 'lib/aidp/prompt_optimization/optimizer.rb', line 31 def stats @stats end |
Instance Method Details
#clear_cache ⇒ Object
Clear cached indexes (useful for testing or when files change)
101 102 103 104 105 106 107 |
# File 'lib/aidp/prompt_optimization/optimizer.rb', line 101 def clear_cache @style_guide_indexer = nil @template_indexer = nil @fragmenter = nil @project_knowledge_indexer = nil @stats.reset! end |
#optimize_prompt(task_type: nil, description: nil, affected_files: [], step_name: nil, tags: [], options: {}) ⇒ PromptOutput
Optimize prompt for given task context
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/aidp/prompt_optimization/optimizer.rb', line 59 def optimize_prompt(task_type: nil, description: nil, affected_files: [], step_name: nil, tags: [], options: {}) start_time = Time.now # Build task context task_context = TaskContext.new( task_type: task_type, description: description, affected_files: affected_files, step_name: step_name, tags: ) # Index all fragments all_fragments = index_all_fragments(affected_files) @stats.record_fragments_indexed(all_fragments.count) # Score fragments scored_fragments = score_fragments(all_fragments, task_context) @stats.record_fragments_scored(scored_fragments.count) # Select fragments within budget max_tokens = [:max_tokens] || @config[:max_tokens] thresholds = @config[:include_threshold] composition_result = compose_context(scored_fragments, max_tokens, thresholds) @stats.record_fragments_selected(composition_result.selected_count) @stats.record_fragments_excluded(composition_result.excluded_count) @stats.record_tokens_used(composition_result.total_tokens) @stats.record_budget_utilization(composition_result.budget_utilization) # Build final prompt prompt_output = build_prompt(task_context, composition_result, ) elapsed = Time.now - start_time @stats.record_optimization_time(elapsed) log_optimization_result(prompt_output) if @config[:log_selected_fragments] prompt_output end |
#statistics ⇒ Hash
Get optimization statistics
112 113 114 |
# File 'lib/aidp/prompt_optimization/optimizer.rb', line 112 def statistics @stats.summary end |