Class: Legion::Extensions::Agentic::Defense::Confabulation::Helpers::ConfabulationEngine
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Defense::Confabulation::Helpers::ConfabulationEngine
- Defined in:
- lib/legion/extensions/agentic/defense/confabulation/helpers/confabulation_engine.rb
Instance Attribute Summary collapse
-
#claims ⇒ Object
readonly
Returns the value of attribute claims.
Instance Method Summary collapse
- #average_calibration ⇒ Object
- #confabulation_rate ⇒ Object
- #confabulation_report ⇒ Object
- #flag_confabulation(claim_id:) ⇒ Object
- #high_risk_claims ⇒ Object
-
#initialize ⇒ ConfabulationEngine
constructor
A new instance of ConfabulationEngine.
- #prune_if_needed ⇒ Object
- #register_claim(content:, claim_type: :factual, confidence: 0.5, evidence_strength: 0.5) ⇒ Object
- #to_h ⇒ Object
- #verified_claims ⇒ Object
- #verify_claim(claim_id:) ⇒ Object
Constructor Details
#initialize ⇒ ConfabulationEngine
Returns a new instance of ConfabulationEngine.
12 13 14 |
# File 'lib/legion/extensions/agentic/defense/confabulation/helpers/confabulation_engine.rb', line 12 def initialize @claims = {} end |
Instance Attribute Details
#claims ⇒ Object (readonly)
Returns the value of attribute claims.
10 11 12 |
# File 'lib/legion/extensions/agentic/defense/confabulation/helpers/confabulation_engine.rb', line 10 def claims @claims end |
Instance Method Details
#average_calibration ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/legion/extensions/agentic/defense/confabulation/helpers/confabulation_engine.rb', line 63 def average_calibration return 0.0 if @claims.empty? total_gap = @claims.values.sum { |c| (c.confidence - c.evidence_strength).abs } gap = total_gap / @claims.size.to_f (1.0 - gap).clamp(0.0, 1.0).round(10) end |
#confabulation_rate ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/legion/extensions/agentic/defense/confabulation/helpers/confabulation_engine.rb', line 55 def confabulation_rate total = @claims.size return 0.0 if total.zero? flagged = @claims.values.count(&:confabulated) (flagged.to_f / total).round(10) end |
#confabulation_report ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/legion/extensions/agentic/defense/confabulation/helpers/confabulation_engine.rb', line 71 def confabulation_report total = @claims.size high_risk = high_risk_claims.size verified = verified_claims.size confabulated = @claims.values.count(&:confabulated) overall_risk = total.zero? ? 0.0 : high_risk.to_f / total risk_label = risk_label_for(overall_risk) { total_claims: total, high_risk_claims: high_risk, verified_claims: verified, confabulated_claims: confabulated, confabulation_rate: confabulation_rate, average_calibration: average_calibration, overall_risk: overall_risk.round(10), risk_label: risk_label } end |
#flag_confabulation(claim_id:) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/legion/extensions/agentic/defense/confabulation/helpers/confabulation_engine.rb', line 39 def flag_confabulation(claim_id:) claim = @claims[claim_id] return { found: false, claim_id: claim_id } unless claim claim.mark_confabulated! { found: true, claim_id: claim_id, confabulated: true } end |
#high_risk_claims ⇒ Object
47 48 49 |
# File 'lib/legion/extensions/agentic/defense/confabulation/helpers/confabulation_engine.rb', line 47 def high_risk_claims @claims.values.select { |c| c.confabulation_risk >= Constants::CONFABULATION_THRESHOLD } end |
#prune_if_needed ⇒ Object
91 92 93 94 95 96 |
# File 'lib/legion/extensions/agentic/defense/confabulation/helpers/confabulation_engine.rb', line 91 def prune_if_needed return unless @claims.size >= Constants::MAX_CLAIMS oldest_key = @claims.min_by { |_, c| c.created_at }&.first @claims.delete(oldest_key) end |
#register_claim(content:, claim_type: :factual, confidence: 0.5, evidence_strength: 0.5) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/legion/extensions/agentic/defense/confabulation/helpers/confabulation_engine.rb', line 16 def register_claim(content:, claim_type: :factual, confidence: 0.5, evidence_strength: 0.5) claim_type = claim_type.to_sym claim_type = :factual unless Constants::CLAIM_TYPES.include?(claim_type) claim = Claim.new( content: content, claim_type: claim_type, confidence: confidence, evidence_strength: evidence_strength ) prune_if_needed @claims[claim.id] = claim claim end |
#to_h ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/legion/extensions/agentic/defense/confabulation/helpers/confabulation_engine.rb', line 98 def to_h { claim_count: @claims.size, confabulation_rate: confabulation_rate, average_calibration: average_calibration } end |
#verified_claims ⇒ Object
51 52 53 |
# File 'lib/legion/extensions/agentic/defense/confabulation/helpers/confabulation_engine.rb', line 51 def verified_claims @claims.values.select(&:verified) end |
#verify_claim(claim_id:) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/legion/extensions/agentic/defense/confabulation/helpers/confabulation_engine.rb', line 31 def verify_claim(claim_id:) claim = @claims[claim_id] return { found: false, claim_id: claim_id } unless claim claim.verify! { found: true, claim_id: claim_id, verified: true } end |