Class: Legion::Extensions::Agentic::Memory::Compression::Helpers::InformationChunk
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Memory::Compression::Helpers::InformationChunk
- Includes:
- Constants
- Defined in:
- lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb
Constant Summary
Constants included from Constants
Constants::CHUNK_TYPES, Constants::COMPRESSION_LABELS, Constants::COMPRESSION_RATE, Constants::DEFAULT_COMPRESSION_RATIO, Constants::FIDELITY_LABELS, Constants::FIDELITY_LOSS_RATE, Constants::MAX_ABSTRACTIONS, Constants::MAX_CHUNKS, Constants::MIN_FIDELITY
Instance Attribute Summary collapse
-
#chunk_type ⇒ Object
readonly
Returns the value of attribute chunk_type.
-
#compressed_size ⇒ Object
readonly
Returns the value of attribute compressed_size.
-
#compression_count ⇒ Object
readonly
Returns the value of attribute compression_count.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#fidelity ⇒ Object
readonly
Returns the value of attribute fidelity.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#original_size ⇒ Object
readonly
Returns the value of attribute original_size.
Instance Method Summary collapse
- #compress!(amount: COMPRESSION_RATE) ⇒ Object
- #compression_label ⇒ Object
- #compression_ratio ⇒ Object
- #decompress!(amount: COMPRESSION_RATE) ⇒ Object
- #fidelity_label ⇒ Object
- #highly_compressed? ⇒ Boolean
-
#initialize(label:, chunk_type: :semantic, original_size: 1.0) ⇒ InformationChunk
constructor
A new instance of InformationChunk.
- #to_h ⇒ Object
Constructor Details
#initialize(label:, chunk_type: :semantic, original_size: 1.0) ⇒ InformationChunk
Returns a new instance of InformationChunk.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 17 def initialize(label:, chunk_type: :semantic, original_size: 1.0) @id = SecureRandom.uuid @label = label @chunk_type = chunk_type.to_sym @original_size = [original_size.to_f, 0.01].max @compressed_size = @original_size @fidelity = 1.0 @compression_count = 0 @created_at = Time.now.utc end |
Instance Attribute Details
#chunk_type ⇒ Object (readonly)
Returns the value of attribute chunk_type.
14 15 16 |
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 14 def chunk_type @chunk_type end |
#compressed_size ⇒ Object (readonly)
Returns the value of attribute compressed_size.
14 15 16 |
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 14 def compressed_size @compressed_size end |
#compression_count ⇒ Object (readonly)
Returns the value of attribute compression_count.
14 15 16 |
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 14 def compression_count @compression_count end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
14 15 16 |
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 14 def created_at @created_at end |
#fidelity ⇒ Object (readonly)
Returns the value of attribute fidelity.
14 15 16 |
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 14 def fidelity @fidelity end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
14 15 16 |
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 14 def id @id end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
14 15 16 |
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 14 def label @label end |
#original_size ⇒ Object (readonly)
Returns the value of attribute original_size.
14 15 16 |
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 14 def original_size @original_size end |
Instance Method Details
#compress!(amount: COMPRESSION_RATE) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 34 def compress!(amount: COMPRESSION_RATE) @compressed_size = (@compressed_size * (1.0 - amount)).clamp(0.01, @original_size).round(10) @fidelity = (@fidelity - FIDELITY_LOSS_RATE).clamp(MIN_FIDELITY, 1.0).round(10) @compression_count += 1 self end |
#compression_label ⇒ Object
50 51 52 53 |
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 50 def compression_label match = COMPRESSION_LABELS.find { |range, _| range.cover?(compression_ratio) } match ? match.last : :raw end |
#compression_ratio ⇒ Object
28 29 30 31 32 |
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 28 def compression_ratio return 0.0 if @original_size.zero? (1.0 - (@compressed_size / @original_size)).clamp(0.0, 1.0).round(10) end |
#decompress!(amount: COMPRESSION_RATE) ⇒ Object
41 42 43 44 |
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 41 def decompress!(amount: COMPRESSION_RATE) @compressed_size = (@compressed_size / (1.0 - amount)).clamp(0.01, @original_size).round(10) self end |
#fidelity_label ⇒ Object
55 56 57 58 |
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 55 def fidelity_label match = FIDELITY_LABELS.find { |range, _| range.cover?(@fidelity) } match ? match.last : :degraded end |
#highly_compressed? ⇒ Boolean
46 47 48 |
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 46 def highly_compressed? compression_ratio >= 0.8 end |
#to_h ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/legion/extensions/agentic/memory/compression/helpers/information_chunk.rb', line 60 def to_h { id: @id, label: @label, chunk_type: @chunk_type, original_size: @original_size, compressed_size: @compressed_size, compression_ratio: compression_ratio, compression_label: compression_label, fidelity: @fidelity, fidelity_label: fidelity_label, compression_count: @compression_count, created_at: @created_at } end |