Class: Geminize::Models::Message

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

Overview

Base class for messages in a conversation

Direct Known Subclasses

ModelMessage, UserMessage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, role, timestamp = nil) ⇒ Message

Initialize a new message

Parameters:

  • content (String) —

    The content of the message

  • role (String) —

    The role of the message sender

  • timestamp (Time, nil) (defaults to: nil) —

    When the message was created



20
21
22
23
24
25
# File 'lib/geminize/models/message.rb', line 20

def initialize(content, role, timestamp = nil)
  @content = content
  @role = role
  @timestamp = timestamp || Time.now
  validate!
end

Instance Attribute Details

#content ⇒ String (readonly)

Returns The content of the message.

Returns:

  • (String) —

    The content of the message



8
9
10
# File 'lib/geminize/models/message.rb', line 8

def content
  @content
end

#role ⇒ String (readonly)

Returns The role of the message sender (user or model).

Returns:

  • (String) —

    The role of the message sender (user or model)



14
15
16
# File 'lib/geminize/models/message.rb', line 14

def role
  @role
end

#timestamp ⇒ Time (readonly)

Returns When the message was created.

Returns:

  • (Time) —

    When the message was created



11
12
13
# File 'lib/geminize/models/message.rb', line 11

def timestamp
  @timestamp
end

Class Method Details

.from_hash(hash) ⇒ Message

Create a message from a hash

Parameters:

  • hash (Hash) —

    The hash to create the message from

Returns:

  • (Message) —

    A new message object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/geminize/models/message.rb', line 67

def self.from_hash(hash)
  content = extract_content(hash)
  role = hash["role"]
  timestamp = hash["timestamp"] ? Time.parse(hash["timestamp"]) : Time.now

  case role
  when "user"
    UserMessage.new(content, timestamp)
  when "model"
    ModelMessage.new(content, timestamp)
  else
    new(content, role, timestamp)
  end
end

Instance Method Details

#model? ⇒ Boolean

Check if this message is from the model

Returns:

  • (Boolean) —

    True if the message is from the model



54
55
56
# File 'lib/geminize/models/message.rb', line 54

def model?
  @role == "model"
end

#to_h ⇒ Hash

Alias for to_hash for consistency with Ruby conventions

Returns:

  • (Hash) —

    The message as a hash



42
43
44
# File 'lib/geminize/models/message.rb', line 42

def to_h
  to_hash
end

#to_hash ⇒ Hash

Convert the message to a hash suitable for the API

Returns:

  • (Hash) —

    The message as a hash



29
30
31
32
33
34
35
36
37
38
# File 'lib/geminize/models/message.rb', line 29

def to_hash
  {
    role: @role,
    parts: [
      {
        text: @content
      }
    ]
  }
end

#to_json(*args) ⇒ String

Serialize the message to a JSON string

Returns:

  • (String) —

    The message as a JSON string



60
61
62
# File 'lib/geminize/models/message.rb', line 60

def to_json(*args)
  to_h.to_json(*args)
end

#user? ⇒ Boolean

Check if this message is from the user

Returns:

  • (Boolean) —

    True if the message is from the user



48
49
50
# File 'lib/geminize/models/message.rb', line 48

def user?
  @role == "user"
end

#validate! ⇒ Object

Validate the message parameters

Raises:



96
97
98
99
# File 'lib/geminize/models/message.rb', line 96

def validate!
  validate_content!
  validate_role!
end

#validate_content! ⇒ Object

Validate the content parameter

Raises:



103
104
105
# File 'lib/geminize/models/message.rb', line 103

def validate_content!
  Validators.validate_not_empty!(@content, "Content")
end

#validate_role! ⇒ Object

Validate the role parameter

Raises:



109
110
111
112
113
114
115
116
117
# File 'lib/geminize/models/message.rb', line 109

def validate_role!
  allowed_roles = ["user", "model", "system"]
  unless allowed_roles.include?(@role)
    raise Geminize::ValidationError.new(
      "Role must be one of: #{allowed_roles.join(", ")}",
      "INVALID_ARGUMENT"
    )
  end
end