Class: RubyConversations::Message
- Inherits:
-
Object
- Object
- RubyConversations::Message
- Includes:
- ActiveModel::Model, Concerns::MessageApiAttributes, Concerns::MessageAttributes
- Defined in:
- lib/ruby_conversations/message.rb
Overview
Represents a message in a conversation, either from the user or AI
Constant Summary collapse
- ROLES =
Constants
{ user: 'user', assistant: 'assistant' }.freeze
Constants included from Concerns::MessageAttributes
Concerns::MessageAttributes::ATTRIBUTE_NAMES
Instance Attribute Summary collapse
-
#conversation_id ⇒ Object
Define attributes.
-
#created_at ⇒ Object
Define attributes.
-
#id ⇒ Object
Define attributes.
-
#llm ⇒ Object
Define attributes.
-
#message_prompts ⇒ Object
Define attributes.
-
#metadata ⇒ Object
Define attributes.
-
#temperature ⇒ Object
Define attributes.
-
#tool ⇒ Object
Define attributes.
-
#updated_at ⇒ Object
Define attributes.
Instance Method Summary collapse
- #assistant? ⇒ Boolean
-
#attributes ⇒ Object
Attributes method for serialization/logging.
-
#attributes_for_api ⇒ Object
Method for API serialization.
-
#initialize(attributes = {}) ⇒ Message
constructor
A new instance of Message.
- #initialize_message_prompts(attributes_array) ⇒ Object
- #message_prompts_attributes=(attributes_array) ⇒ Object (also: #ai_message_prompts_attributes=)
- #prompt_inputs ⇒ Object
-
#user? ⇒ Boolean
Helper methods for role checking.
Methods included from Concerns::MessageApiAttributes
#message_attributes_for_api, #message_base_attributes, #remote_attributes
Methods included from Concerns::MessageAttributes
Constructor Details
#initialize(attributes = {}) ⇒ Message
Returns a new instance of Message.
23 24 25 26 27 28 29 30 31 |
# File 'lib/ruby_conversations/message.rb', line 23 def initialize(attributes = {}) @message_prompts = [] prompts_attributes = extract_nested_attributes!(attributes, :message_prompts) # Also check for Rails-style nested attributes prompts_attributes ||= extract_nested_attributes!(attributes, :ai_message_prompts_attributes) super (prompts_attributes) end |
Instance Attribute Details
#conversation_id ⇒ Object
Define attributes
13 14 15 |
# File 'lib/ruby_conversations/message.rb', line 13 def conversation_id @conversation_id end |
#created_at ⇒ Object
Define attributes
13 14 15 |
# File 'lib/ruby_conversations/message.rb', line 13 def created_at @created_at end |
#id ⇒ Object
Define attributes
13 14 15 |
# File 'lib/ruby_conversations/message.rb', line 13 def id @id end |
#llm ⇒ Object
Define attributes
13 14 15 |
# File 'lib/ruby_conversations/message.rb', line 13 def llm @llm end |
#message_prompts ⇒ Object
Define attributes
13 14 15 |
# File 'lib/ruby_conversations/message.rb', line 13 def @message_prompts end |
#metadata ⇒ Object
Define attributes
13 14 15 |
# File 'lib/ruby_conversations/message.rb', line 13 def @metadata end |
#temperature ⇒ Object
Define attributes
13 14 15 |
# File 'lib/ruby_conversations/message.rb', line 13 def temperature @temperature end |
#tool ⇒ Object
Define attributes
13 14 15 |
# File 'lib/ruby_conversations/message.rb', line 13 def tool @tool end |
#updated_at ⇒ Object
Define attributes
13 14 15 |
# File 'lib/ruby_conversations/message.rb', line 13 def updated_at @updated_at end |
Instance Method Details
#assistant? ⇒ Boolean
76 77 78 |
# File 'lib/ruby_conversations/message.rb', line 76 def assistant? response.present? end |
#attributes ⇒ Object
Attributes method for serialization/logging
50 51 52 53 54 55 56 57 |
# File 'lib/ruby_conversations/message.rb', line 50 def attributes base_attributes.merge( 'llm' => llm, 'temperature' => temperature, 'tool' => tool, 'ai_message_prompts_attributes' => .map(&:attributes) ) end |
#attributes_for_api ⇒ Object
Method for API serialization
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ruby_conversations/message.rb', line 60 def attributes_for_api { id: id, conversation_id: conversation_id, metadata: , llm: llm, tool: tool, ai_message_prompts_attributes: .map(&:attributes_for_api) }.merge().compact end |
#initialize_message_prompts(attributes_array) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/ruby_conversations/message.rb', line 33 def (attributes_array) (attributes_array || []).each do |attrs| next if attrs.blank? @message_prompts << RubyConversations::MessagePrompt.new(attrs) end end |
#message_prompts_attributes=(attributes_array) ⇒ Object Also known as: ai_message_prompts_attributes=
41 42 43 44 |
# File 'lib/ruby_conversations/message.rb', line 41 def (attributes_array) @message_prompts = [] (attributes_array) end |
#prompt_inputs ⇒ Object
80 81 82 |
# File 'lib/ruby_conversations/message.rb', line 80 def prompt_inputs .flat_map(&:message_inputs) end |
#user? ⇒ Boolean
Helper methods for role checking
72 73 74 |
# File 'lib/ruby_conversations/message.rb', line 72 def user? request.present? && response.blank? end |