Class: Geminize::Models::Message
- Inherits:
-
Object
- Object
- Geminize::Models::Message
- Defined in:
- lib/geminize/models/message.rb
Overview
Base class for messages in a conversation
Direct Known Subclasses
Instance Attribute Summary collapse
-
#content ⇒ String
readonly
The content of the message.
-
#role ⇒ String
readonly
The role of the message sender (user or model).
-
#timestamp ⇒ Time
readonly
When the message was created.
Class Method Summary collapse
-
.from_hash(hash) ⇒ Message
Create a message from a hash.
Instance Method Summary collapse
-
#initialize(content, role, timestamp = nil) ⇒ Message
constructor
Initialize a new message.
-
#model? ⇒ Boolean
Check if this message is from the model.
-
#to_h ⇒ Hash
Alias for to_hash for consistency with Ruby conventions.
-
#to_hash ⇒ Hash
Convert the message to a hash suitable for the API.
-
#to_json(*args) ⇒ String
Serialize the message to a JSON string.
-
#user? ⇒ Boolean
Check if this message is from the user.
-
#validate! ⇒ Object
Validate the message parameters.
-
#validate_content! ⇒ Object
Validate the content parameter.
-
#validate_role! ⇒ Object
Validate the role parameter.
Constructor Details
#initialize(content, role, timestamp = nil) ⇒ Message
Initialize a new message
20 21 22 23 24 25 |
# File 'lib/geminize/models/message.rb', line 20 def initialize(content, role, = nil) @content = content @role = role = || Time.now validate! end |
Instance Attribute Details
#content ⇒ String (readonly)
8 9 10 |
# File 'lib/geminize/models/message.rb', line 8 def content @content end |
#role ⇒ String (readonly)
14 15 16 |
# File 'lib/geminize/models/message.rb', line 14 def role @role end |
#timestamp ⇒ Time (readonly)
11 12 13 |
# File 'lib/geminize/models/message.rb', line 11 def end |
Class Method Details
.from_hash(hash) ⇒ Message
Create a message from a hash
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"] = hash["timestamp"] ? Time.parse(hash["timestamp"]) : Time.now case role when "user" UserMessage.new(content, ) when "model" ModelMessage.new(content, ) else new(content, role, ) end end |
Instance Method Details
#model? ⇒ Boolean
Check if this 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
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
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
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
48 49 50 |
# File 'lib/geminize/models/message.rb', line 48 def user? @role == "user" end |
#validate! ⇒ Object
Validate the message parameters
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
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
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 |