Class: Agentic::Learning::CapabilityOptimizer

Inherits:
Object
  • Object
show all
Defined in:
lib/agentic/learning/capability_optimizer.rb

Overview

CapabilityOptimizer improves capability implementations and composition based on execution history and performance metrics.

Examples:

Optimizing a capability implementation

history_store = Agentic::Learning::ExecutionHistoryStore.new
recognizer = Agentic::Learning::PatternRecognizer.new(history_store: history_store)
optimizer = Agentic::Learning::CapabilityOptimizer.new(
  pattern_recognizer: recognizer,
  history_store: history_store,
  registry: Agentic.agent_capability_registry
)

# Get optimization suggestions for a capability
suggestions = optimizer.get_optimization_suggestions("text_generation")

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CapabilityOptimizer

Initialize a new CapabilityOptimizer

Parameters:

  • options (Hash) (defaults to: {})

    Configuration options

Options Hash (options):

  • :logger (Logger)

    Custom logger (defaults to Agentic.logger)

  • :pattern_recognizer (PatternRecognizer)

    Pattern recognizer for insights

  • :history_store (ExecutionHistoryStore)

    History store for performance data

  • :registry (AgentCapabilityRegistry)

    The capability registry

  • :llm_client (LlmClient)

    LLM client for generating optimizations (optional)

  • :auto_apply_optimizations (Boolean)

    Whether to automatically apply optimizations



30
31
32
33
34
35
36
37
38
39
# File 'lib/agentic/learning/capability_optimizer.rb', line 30

def initialize(options = {})
  @logger = options[:logger] || Agentic.logger
  @pattern_recognizer = options[:pattern_recognizer] || raise(ArgumentError, "pattern_recognizer is required")
  @history_store = options[:history_store] || raise(ArgumentError, "history_store is required")
  @registry = options[:registry] || Agentic.agent_capability_registry
  @llm_client = options[:llm_client]
  @auto_apply_optimizations = options.fetch(:auto_apply_optimizations, false)
  @optimization_cache = {}
  @last_optimization = {}
end

Instance Method Details

#apply_optimization(capability_name, optimization, options = {}) ⇒ Boolean

Apply optimization to a capability

Parameters:

  • capability_name (String)

    The name of the capability

  • optimization (Hash)

    The optimization to apply

  • options (Hash) (defaults to: {})

    Additional options

Returns:

  • (Boolean)

    Whether the optimization was applied successfully



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/agentic/learning/capability_optimizer.rb', line 167

def apply_optimization(capability_name, optimization, options = {})
  # Get the capability and provider
  capability = @registry.get(capability_name)
  return false unless capability

  provider = @registry.get_provider(capability_name)
  return false unless provider

  # Check optimization type
  case optimization[:type]
  when :implementation
    # For implementation optimizations, we would need to create a new provider
    # This is a simplified example - in a real implementation, we would need to
    # handle different implementation types (Proc, Class, etc.)
    if provider.implementation.is_a?(Proc) && optimization[:new_implementation].is_a?(Proc)
      # Create a new provider with the optimized implementation
      new_provider = Agentic::CapabilityProvider.new(
        capability: capability,
        implementation: optimization[:new_implementation]
      )

      # Register the new provider
      @registry.register(capability, new_provider)

      return true
    end
  when :parameters
    # For parameter optimizations, we would update the capability's parameters
    # This is a simplified example - in a real implementation, we would need to
    # handle different parameter types
    if optimization[:parameters].is_a?(Hash)
      # Update parameters (this would depend on the actual capability implementation)
      # For this example, we just log that we would update the parameters
      @logger.info("Would update parameters for #{capability_name}: #{optimization[:parameters]}")

      return true
    end
  end

  false
end

#get_optimization_suggestions(capability_name, version = nil, options = {}) ⇒ Hash

Get optimization suggestions for a capability

Parameters:

  • capability_name (String)

    The name of the capability

  • version (String, nil) (defaults to: nil)

    The version of the capability (latest if nil)

  • options (Hash) (defaults to: {})

    Additional options

Options Hash (options):

  • :force (Boolean)

    Force new suggestions even if recently generated

Returns:

  • (Hash)

    Optimization suggestions



48
49
50
51
52
53
54
55
56
57
58
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
99
100
101
# File 'lib/agentic/learning/capability_optimizer.rb', line 48

def get_optimization_suggestions(capability_name, version = nil, options = {})
  # Get the capability from the registry
  capability = @registry.get(capability_name, version)
  return {success: false, reason: "Capability not found"} unless capability

  version = capability.version
  cache_key = "#{capability_name}:#{version}"

  # Check cache
  unless options[:force]
    if @optimization_cache[cache_key] && @last_optimization[cache_key] &&
        @last_optimization[cache_key] > Time.now - 24 * 60 * 60
      return @optimization_cache[cache_key]
    end
  end

  # Get capability execution history
  history = get_capability_history(capability_name, version)

  # Check if we have enough data
  if history.size < 10
    result = {
      success: false,
      reason: "Insufficient execution data",
      capability: capability_name,
      version: version,
      suggestions: []
    }

    @optimization_cache[cache_key] = result
    @last_optimization[cache_key] = Time.now

    return result
  end

  # Analyze performance
  performance = analyze_capability_performance(history)

  # Generate suggestions
  suggestions = generate_optimization_suggestions(capability, performance)

  result = {
    success: true,
    capability: capability_name,
    version: version,
    performance: performance,
    suggestions: suggestions
  }

  @optimization_cache[cache_key] = result
  @last_optimization[cache_key] = Time.now

  result
end

#optimize_capability_composition(task, options = {}) ⇒ Hash

Optimize capability composition for a task

Parameters:

  • task (Task)

    The task to optimize capabilities for

  • options (Hash) (defaults to: {})

    Additional options

Returns:

  • (Hash)

    Optimized capability composition



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/agentic/learning/capability_optimizer.rb', line 108

def optimize_capability_composition(task, options = {})
  # Get task description
  description = task.description

  # Get currently used capabilities
  if task.input && task.input[:capabilities]
    current_capabilities = task.input[:capabilities]
  else
    # If no capabilities specified, use default inference
    requirements = {}
    if task.agent_spec
      engine = Agentic::AgentAssemblyEngine.new(@registry)
      requirements = engine.analyze_requirements(task)
    end

    current_capabilities = requirements.keys
  end

  # No need to optimize if no capabilities
  if current_capabilities.empty?
    return {
      success: false,
      reason: "No capabilities to optimize",
      original_capabilities: [],
      optimized_capabilities: []
    }
  end

  # Get capability performance data
  capability_performance = {}
  current_capabilities.each do |capability|
    suggestions = get_optimization_suggestions(capability.to_s)
    capability_performance[capability.to_s] = suggestions[:performance] if suggestions[:success]
  end

  # Generate optimized composition
  if @llm_client
    generate_optimized_composition_with_llm(
      description,
      current_capabilities,
      capability_performance,
      options
    )
  else
    generate_optimized_composition_heuristic(
      description,
      current_capabilities,
      capability_performance,
      options
    )
  end
end

#record_execution(capability_name, version, metrics, result) ⇒ Boolean

Record capability execution metrics

Parameters:

  • capability_name (String)

    The name of the capability

  • version (String)

    The version of the capability

  • metrics (Hash)

    The execution metrics

  • result (Hash)

    The execution result

Returns:

  • (Boolean)

    Whether the metrics were recorded successfully



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/agentic/learning/capability_optimizer.rb', line 216

def record_execution(capability_name, version, metrics, result)
  success = result && !result.key?(:error)

  @history_store.record_execution(
    capability_name: capability_name,
    capability_version: version,
    duration_ms: metrics[:duration_ms],
    success: success,
    metrics: metrics,
    context: {
      result_summary: summarize_result(result),
      error: (result && result[:error]) ? result[:error] : nil
    }
  )

  true
end