Module: OllamaChat::MessageMixin
- Included in:
- Message
- Defined in:
- lib/ollama_chat/message.rb
Overview
Mixin to provide write access to attributes that are read-only in the base class and add utility methods for content cleaning.
Instance Attribute Summary collapse
- #content ⇒ Object
- #group_uuid ⇒ Object
- #images ⇒ Object
- #sender_name ⇒ Object
- #thinking ⇒ Object
- #tool_calls ⇒ Object
Instance Method Summary collapse
-
#as_json(*a) ⇒ Hash
Converts the message to a JSON-compatible hash, including the sender name and group UUID.
-
#group_time ⇒ Time
Extracts the timestamp embedded within the UUIDv7 group identifier.
-
#initialize(**attributes) ⇒ Object
Initializes a new message, ensuring the sender_name is set if provided.
-
#initialize_group_uuid ⇒ OllamaChat::Message
Ensures that the message has a
group_uuidby generating a UUIDv7 if missing. -
#token_estimate(strip_thinking: false) ⇒ OllamaChat::TokenEstimator::Estimate
Estimates the token count for this message's text content.
-
#tool? ⇒ Boolean
Returns true if the message is a tool message.
Instance Attribute Details
#content ⇒ Object
26 |
# File 'lib/ollama_chat/message.rb', line 26 attr_writer :content |
#group_uuid ⇒ Object
44 45 46 |
# File 'lib/ollama_chat/message.rb', line 44 def group_uuid @group_uuid end |
#images ⇒ Object
34 |
# File 'lib/ollama_chat/message.rb', line 34 attr_writer :images |
#sender_name ⇒ Object
39 40 41 |
# File 'lib/ollama_chat/message.rb', line 39 def sender_name @sender_name end |
#thinking ⇒ Object
30 |
# File 'lib/ollama_chat/message.rb', line 30 attr_writer :thinking |
#tool_calls ⇒ Object
50 51 52 |
# File 'lib/ollama_chat/message.rb', line 50 def tool_calls @tool_calls end |
Instance Method Details
#as_json(*a) ⇒ Hash
Converts the message to a JSON-compatible hash, including the sender name and group UUID.
93 94 95 |
# File 'lib/ollama_chat/message.rb', line 93 def as_json(*a) { sender_name:, group_uuid:, tool_calls:, }.compact | super end |
#group_time ⇒ Time
Extracts the timestamp embedded within the UUIDv7 group identifier.
65 66 67 |
# File 'lib/ollama_chat/message.rb', line 65 def group_time Time.at((group_uuid.delete(?-)[0, 16].to_i(16) >> 16) / 1000.0) if group_uuid end |
#initialize(**attributes) ⇒ Object
Initializes a new message, ensuring the sender_name is set if provided.
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/ollama_chat/message.rb', line 11 def initialize(**attributes) super if sender_name = attributes[:sender_name] self.sender_name = sender_name end if group_uuid = attributes[:group_uuid] self.group_uuid = group_uuid end if tool_calls = attributes[:tool_calls] self.tool_calls = tool_calls end end |
#initialize_group_uuid ⇒ OllamaChat::Message
Ensures that the message has a group_uuid by generating a UUIDv7 if
missing. Returns self to allow for method chaining.
56 57 58 59 |
# File 'lib/ollama_chat/message.rb', line 56 def initialize_group_uuid self.group_uuid ||= OllamaChat::UUIDV7.generate self end |
#token_estimate(strip_thinking: false) ⇒ OllamaChat::TokenEstimator::Estimate
Estimates the token count for this message's text content.
75 76 77 78 79 |
# File 'lib/ollama_chat/message.rb', line 75 def token_estimate(strip_thinking: false) bytes = content.to_s.bytesize bytes += thinking.to_s.bytesize unless strip_thinking OllamaChat::TokenEstimator.estimate(bytes) end |
#tool? ⇒ Boolean
Returns true if the message is a tool message.
85 86 87 |
# File 'lib/ollama_chat/message.rb', line 85 def tool? tool_name.present? end |