Class: Geminize::Models::Memory
- Inherits:
-
Object
- Object
- Geminize::Models::Memory
- Defined in:
- lib/geminize/models/memory.rb
Overview
Represents a message in the Gemini format
Instance Attribute Summary collapse
-
#parts ⇒ Array<Hash>
readonly
The parts of the message (e.g., text content).
-
#role ⇒ String
readonly
The role of the sender (user, model, system).
Class Method Summary collapse
-
.from_hash(hash) ⇒ Memory
Create a Memory object from a hash.
-
.from_json(json) ⇒ Memory
Create a Memory object from a JSON string.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Equality comparison.
-
#initialize(role = "", parts = nil) ⇒ Memory
constructor
Initialize a new memory.
-
#to_h ⇒ Hash
Convert the memory to a hash suitable for API requests.
-
#to_json(*opts) ⇒ String
Convert the memory to a JSON string.
Constructor Details
#initialize(role = "", parts = nil) ⇒ Memory
Initialize a new memory
18 19 20 21 22 |
# File 'lib/geminize/models/memory.rb', line 18 def initialize(role = "", parts = nil) @role = role @parts = parts || [{text: ""}] @parts.freeze end |
Instance Attribute Details
#parts ⇒ Array<Hash> (readonly)
Returns The parts of the message (e.g., text content).
13 14 15 |
# File 'lib/geminize/models/memory.rb', line 13 def parts @parts end |
#role ⇒ String (readonly)
Returns The role of the sender (user, model, system).
10 11 12 |
# File 'lib/geminize/models/memory.rb', line 10 def role @role end |
Class Method Details
.from_hash(hash) ⇒ Memory
Create a Memory object from a hash
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/geminize/models/memory.rb', line 55 def self.from_hash(hash) # Handle both string and symbol keys role = hash[:role] || hash["role"] || "" # Handle parts parts_data = hash[:parts] || hash["parts"] parts = if parts_data # Convert string keys to symbols parts_data.map do |part| if part.is_a?(Hash) part_with_symbol_keys = {} part.each { |k, v| part_with_symbol_keys[k.to_sym] = v } part_with_symbol_keys else part end end else [{text: ""}] end new(role, parts) end |
.from_json(json) ⇒ Memory
Create a Memory object from a JSON string
82 83 84 85 |
# File 'lib/geminize/models/memory.rb', line 82 def self.from_json(json) hash = JSON.parse(json) from_hash(hash) end |
Instance Method Details
#==(other) ⇒ Boolean
Equality comparison
47 48 49 50 |
# File 'lib/geminize/models/memory.rb', line 47 def ==(other) return false unless other.is_a?(Memory) role == other.role && parts == other.parts end |
#to_h ⇒ Hash
Convert the memory to a hash suitable for API requests
26 27 28 29 30 31 |
# File 'lib/geminize/models/memory.rb', line 26 def to_h { role: @role, parts: @parts } end |
#to_json(*opts) ⇒ String
Convert the memory to a JSON string
36 37 38 39 40 41 42 |
# File 'lib/geminize/models/memory.rb', line 36 def to_json(*opts) if opts.first && opts.first[:pretty] JSON.pretty_generate(to_h) else to_h.to_json(*opts) end end |