Class: Geminize::Models::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/geminize/models/memory.rb

Overview

Represents a message in the Gemini format

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role = "", parts = nil) ⇒ Memory

Initialize a new memory

Parameters:

  • role (String) (defaults to: "")

    The role of the sender (user, model, system)

  • parts (Array<Hash>) (defaults to: nil)

    The parts of the message



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

#partsArray<Hash> (readonly)

Returns The parts of the message (e.g., text content).

Returns:

  • (Array<Hash>)

    The parts of the message (e.g., text content)



13
14
15
# File 'lib/geminize/models/memory.rb', line 13

def parts
  @parts
end

#roleString (readonly)

Returns The role of the sender (user, model, system).

Returns:

  • (String)

    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

Parameters:

  • hash (Hash)

    The hash to create from

Returns:

  • (Memory)

    A new Memory object



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

Parameters:

  • json (String)

    The JSON string

Returns:

  • (Memory)

    A new Memory object



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

Parameters:

  • other (Object)

    The object to compare with

Returns:

  • (Boolean)

    True if the objects are equal



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_hHash

Convert the memory to a hash suitable for API requests

Returns:

  • (Hash)

    The memory as a hash



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

Parameters:

  • opts (Hash)

    JSON generate options

Returns:

  • (String)

    The memory as 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