Class: Geminize::Models::Conversation
- Inherits:
-
Object
- Object
- Geminize::Models::Conversation
- Defined in:
- lib/geminize/models/conversation.rb
Overview
Represents a conversation with message history
Instance Attribute Summary collapse
-
#created_at ⇒ Time
readonly
When the conversation was created.
-
#id ⇒ String
readonly
Unique identifier for the conversation.
-
#messages ⇒ Array<Message>
readonly
The messages in the conversation.
-
#system_instruction ⇒ String?
System instruction to guide model behavior.
-
#title ⇒ String
Optional title for the conversation.
-
#updated_at ⇒ Time
readonly
When the conversation was last updated.
Class Method Summary collapse
-
.from_hash(hash) ⇒ Conversation
Create a conversation from a hash.
-
.from_json(json) ⇒ Conversation
Create a conversation from a JSON string.
Instance Method Summary collapse
-
#add_message(message) ⇒ Message
Add a message to the conversation.
-
#add_model_message(content) ⇒ ModelMessage
Add a model message to the conversation.
-
#add_user_message(content) ⇒ UserMessage
Add a user message to the conversation.
-
#clear ⇒ self
Clear all messages from the conversation.
-
#has_messages? ⇒ Boolean
Check if the conversation has any messages.
-
#initialize(id = nil, title = nil, messages = nil, created_at = nil, system_instruction = nil) ⇒ Conversation
constructor
Initialize a new conversation.
-
#last_message ⇒ Message?
Get the last message in the conversation.
-
#message_count ⇒ Integer
Get the number of messages in the conversation.
-
#messages_as_hashes ⇒ Array<Hash>
Get the messages as an array of hashes for the API.
-
#to_h ⇒ Hash
Alias for to_hash for consistency with Ruby conventions.
-
#to_hash ⇒ Hash
Convert the conversation to a hash.
-
#to_json(*args) ⇒ String
Serialize the conversation to a JSON string.
Constructor Details
#initialize(id = nil, title = nil, messages = nil, created_at = nil, system_instruction = nil) ⇒ Conversation
Initialize a new conversation
35 36 37 38 39 40 41 42 |
# File 'lib/geminize/models/conversation.rb', line 35 def initialize(id = nil, title = nil, = nil, created_at = nil, system_instruction = nil) @id = id || SecureRandom.uuid @title = title = || [] @created_at = created_at || Time.now @updated_at = @created_at @system_instruction = system_instruction end |
Instance Attribute Details
#created_at ⇒ Time (readonly)
18 19 20 |
# File 'lib/geminize/models/conversation.rb', line 18 def created_at @created_at end |
#id ⇒ String (readonly)
12 13 14 |
# File 'lib/geminize/models/conversation.rb', line 12 def id @id end |
#messages ⇒ Array<Message> (readonly)
24 25 26 |
# File 'lib/geminize/models/conversation.rb', line 24 def end |
#system_instruction ⇒ String?
27 28 29 |
# File 'lib/geminize/models/conversation.rb', line 27 def system_instruction @system_instruction end |
#title ⇒ String
15 16 17 |
# File 'lib/geminize/models/conversation.rb', line 15 def title @title end |
#updated_at ⇒ Time (readonly)
21 22 23 |
# File 'lib/geminize/models/conversation.rb', line 21 def updated_at @updated_at end |
Class Method Details
.from_hash(hash) ⇒ Conversation
Create a conversation from a hash
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/geminize/models/conversation.rb', line 133 def self.from_hash(hash) id = hash["id"] title = hash["title"] created_at = hash["created_at"] ? Time.parse(hash["created_at"]) : Time.now system_instruction = hash["system_instruction"] = [] if hash["messages"]&.is_a?(Array) = hash["messages"].map { |msg_hash| Message.from_hash(msg_hash) } end new(id, title, , created_at, system_instruction) end |
.from_json(json) ⇒ Conversation
Create a conversation from a JSON string
150 151 152 153 |
# File 'lib/geminize/models/conversation.rb', line 150 def self.from_json(json) hash = JSON.parse(json) from_hash(hash) end |
Instance Method Details
#add_message(message) ⇒ Message
Add a message to the conversation
67 68 69 70 71 |
# File 'lib/geminize/models/conversation.rb', line 67 def () << @updated_at = Time.now end |
#add_model_message(content) ⇒ ModelMessage
Add a model message to the conversation
57 58 59 60 61 62 |
# File 'lib/geminize/models/conversation.rb', line 57 def (content) = ModelMessage.new(content) () @updated_at = Time.now end |
#add_user_message(content) ⇒ UserMessage
Add a user message to the conversation
47 48 49 50 51 52 |
# File 'lib/geminize/models/conversation.rb', line 47 def (content) = UserMessage.new(content) () @updated_at = Time.now end |
#clear ⇒ self
Clear all messages from the conversation
99 100 101 102 103 |
# File 'lib/geminize/models/conversation.rb', line 99 def clear = [] @updated_at = Time.now self end |
#has_messages? ⇒ Boolean
Check if the conversation has any messages
87 88 89 |
# File 'lib/geminize/models/conversation.rb', line 87 def !.empty? end |
#last_message ⇒ Message?
Get the last message in the conversation
81 82 83 |
# File 'lib/geminize/models/conversation.rb', line 81 def .last end |
#message_count ⇒ Integer
Get the number of messages in the conversation
93 94 95 |
# File 'lib/geminize/models/conversation.rb', line 93 def .size end |
#messages_as_hashes ⇒ Array<Hash>
Get the messages as an array of hashes for the API
75 76 77 |
# File 'lib/geminize/models/conversation.rb', line 75 def .map(&:to_hash) end |
#to_h ⇒ Hash
Alias for to_hash for consistency with Ruby conventions
120 121 122 |
# File 'lib/geminize/models/conversation.rb', line 120 def to_h to_hash end |
#to_hash ⇒ Hash
Convert the conversation to a hash
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/geminize/models/conversation.rb', line 107 def to_hash { id: @id, title: @title, created_at: @created_at.iso8601, updated_at: @updated_at.iso8601, messages: .map(&:to_hash), system_instruction: @system_instruction } end |
#to_json(*args) ⇒ String
Serialize the conversation to a JSON string
126 127 128 |
# File 'lib/geminize/models/conversation.rb', line 126 def to_json(*args) to_h.to_json(*args) end |