Class: Legion::Extensions::Agentic::Inference::PredictiveProcessing::Helpers::GenerativeModel
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Inference::PredictiveProcessing::Helpers::GenerativeModel
- Includes:
- Constants
- Defined in:
- lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb
Constant Summary
Constants included from Constants
Constants::ACTIVE_INFERENCE_THRESHOLD, Constants::DEFAULT_PRECISION, Constants::FREE_ENERGY_THRESHOLD, Constants::INFERENCE_MODES, Constants::LEARNING_RATE, Constants::MAX_HISTORY, Constants::MAX_MODELS, Constants::MAX_PREDICTIONS_PER_MODEL, Constants::MODEL_CONFIDENCE_FLOOR, Constants::MODEL_STATES, Constants::PRECISION_DECAY, Constants::PRECISION_FLOOR, Constants::PRECISION_LABELS
Instance Attribute Summary collapse
-
#confidence ⇒ Object
readonly
Returns the value of attribute confidence.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#domain ⇒ Object
readonly
Returns the value of attribute domain.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#last_prediction ⇒ Object
readonly
Returns the value of attribute last_prediction.
-
#precision ⇒ Object
readonly
Returns the value of attribute precision.
-
#prediction_error ⇒ Object
readonly
Returns the value of attribute prediction_error.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#updated_at ⇒ Object
readonly
Returns the value of attribute updated_at.
Instance Method Summary collapse
- #decay ⇒ Object
-
#free_energy ⇒ Object
Free energy (surprise): high when errors are large relative to precision.
-
#initialize(domain:) ⇒ GenerativeModel
constructor
A new instance of GenerativeModel.
- #observe(actual:, predicted:) ⇒ Object
- #precision_label ⇒ Object
- #predict(context: {}) ⇒ Object
- #stable? ⇒ Boolean
- #surprised? ⇒ Boolean
- #to_h ⇒ Object
- #update_model(error:) ⇒ Object
Constructor Details
#initialize(domain:) ⇒ GenerativeModel
Returns a new instance of GenerativeModel.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb', line 17 def initialize(domain:) @id = SecureRandom.uuid @domain = domain @confidence = DEFAULT_PRECISION @precision = DEFAULT_PRECISION @prediction_error = 0.0 @state = :stable @history = [] @last_prediction = nil @created_at = Time.now.utc @updated_at = Time.now.utc end |
Instance Attribute Details
#confidence ⇒ Object (readonly)
Returns the value of attribute confidence.
14 15 16 |
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb', line 14 def confidence @confidence end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
14 15 16 |
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb', line 14 def created_at @created_at end |
#domain ⇒ Object (readonly)
Returns the value of attribute domain.
14 15 16 |
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb', line 14 def domain @domain end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
14 15 16 |
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb', line 14 def id @id end |
#last_prediction ⇒ Object (readonly)
Returns the value of attribute last_prediction.
14 15 16 |
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb', line 14 def last_prediction @last_prediction end |
#precision ⇒ Object (readonly)
Returns the value of attribute precision.
14 15 16 |
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb', line 14 def precision @precision end |
#prediction_error ⇒ Object (readonly)
Returns the value of attribute prediction_error.
14 15 16 |
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb', line 14 def prediction_error @prediction_error end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
14 15 16 |
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb', line 14 def state @state end |
#updated_at ⇒ Object (readonly)
Returns the value of attribute updated_at.
14 15 16 |
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb', line 14 def updated_at @updated_at end |
Instance Method Details
#decay ⇒ Object
78 79 80 81 |
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb', line 78 def decay @precision = [@precision - PRECISION_DECAY, PRECISION_FLOOR].max @updated_at = Time.now.utc end |
#free_energy ⇒ Object
Free energy (surprise): high when errors are large relative to precision. Uses a formula that can exceed FREE_ENERGY_THRESHOLD (0.7) with sustained errors.
54 55 56 57 58 59 60 61 |
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb', line 54 def free_energy return 0.0 if @history.empty? recent = @history.last(10) avg_error = recent.sum.to_f / recent.size raw = avg_error * (1.5 - (precision * 0.5)) raw.clamp(0.0, 1.0) end |
#observe(actual:, predicted:) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb', line 43 def observe(actual:, predicted:) @prediction_error = compute_error(actual, predicted) update_history(@prediction_error) update_confidence update_state @updated_at = Time.now.utc @prediction_error end |
#precision_label ⇒ Object
83 84 85 |
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb', line 83 def precision_label PRECISION_LABELS.find { |range, _label| range.cover?(precision) }&.last || :noise end |
#predict(context: {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb', line 30 def predict(context: {}) richness = context_richness(context) expected_val = (confidence + richness).clamp(0.0, 1.0) @last_prediction = { expected_value: expected_val, confidence: confidence, precision: precision, domain: @domain, context_size: context.size, predicted_at: Time.now.utc } end |
#stable? ⇒ Boolean
70 71 72 |
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb', line 70 def stable? free_energy <= FREE_ENERGY_THRESHOLD end |
#surprised? ⇒ Boolean
74 75 76 |
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb', line 74 def surprised? free_energy > FREE_ENERGY_THRESHOLD end |
#to_h ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb', line 87 def to_h { id: @id, domain: @domain, confidence: confidence, precision: precision, prediction_error: @prediction_error, free_energy: free_energy, state: @state, precision_label: precision_label, stable: stable?, surprised: surprised?, history_size: @history.size, created_at: @created_at, updated_at: @updated_at } end |
#update_model(error:) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/generative_model.rb', line 63 def update_model(error:) adjustment = error * LEARNING_RATE * precision @confidence = (@confidence - adjustment).clamp(MODEL_CONFIDENCE_FLOOR, 1.0) @state = :updating @updated_at = Time.now.utc end |