Class: RubyConversations::Message

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Concerns::MessageApiAttributes

#message_attributes_for_api, #message_base_attributes, #remote_attributes

Methods included from Concerns::MessageAttributes

#remote_attributes

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
  initialize_message_prompts(prompts_attributes)
end

Instance Attribute Details

#conversation_idObject

Define attributes



13
14
15
# File 'lib/ruby_conversations/message.rb', line 13

def conversation_id
  @conversation_id
end

#created_atObject

Define attributes



13
14
15
# File 'lib/ruby_conversations/message.rb', line 13

def created_at
  @created_at
end

#idObject

Define attributes



13
14
15
# File 'lib/ruby_conversations/message.rb', line 13

def id
  @id
end

#llmObject

Define attributes



13
14
15
# File 'lib/ruby_conversations/message.rb', line 13

def llm
  @llm
end

#message_promptsObject

Define attributes



13
14
15
# File 'lib/ruby_conversations/message.rb', line 13

def message_prompts
  @message_prompts
end

#metadataObject

Define attributes



13
14
15
# File 'lib/ruby_conversations/message.rb', line 13

def 
  @metadata
end

#temperatureObject

Define attributes



13
14
15
# File 'lib/ruby_conversations/message.rb', line 13

def temperature
  @temperature
end

#toolObject

Define attributes



13
14
15
# File 'lib/ruby_conversations/message.rb', line 13

def tool
  @tool
end

#updated_atObject

Define attributes



13
14
15
# File 'lib/ruby_conversations/message.rb', line 13

def updated_at
  @updated_at
end

Instance Method Details

#assistant?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/ruby_conversations/message.rb', line 76

def assistant?
  response.present?
end

#attributesObject

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' => message_prompts.map(&:attributes)
  )
end

#attributes_for_apiObject

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: message_prompts.map(&:attributes_for_api)
  }.merge(message_attributes_for_api).compact
end

#initialize_message_prompts(attributes_array) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/ruby_conversations/message.rb', line 33

def initialize_message_prompts(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 message_prompts_attributes=(attributes_array)
  @message_prompts = []
  initialize_message_prompts(attributes_array)
end

#prompt_inputsObject



80
81
82
# File 'lib/ruby_conversations/message.rb', line 80

def prompt_inputs
  message_prompts.flat_map(&:message_inputs)
end

#user?Boolean

Helper methods for role checking

Returns:

  • (Boolean)


72
73
74
# File 'lib/ruby_conversations/message.rb', line 72

def user?
  request.present? && response.blank?
end