Class: Intelligence::Conversation

Inherits:
Object
  • Object
show all
Includes:
DynamicSchema::Buildable, DynamicSchema::Definable
Defined in:
lib/intelligence/conversation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil) ⇒ Conversation

Returns a new instance of Conversation.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/intelligence/conversation.rb', line 15

def initialize( attributes = nil )
 
  @messages = []
  if attributes
    if attributes[ :system_message ]&.any? 
      system_message = Message.new( 
        attributes[ :system_message ][ :role ],  
        attributes[ :system_message ]
      )
      @system_message = system_message unless system_message.empty?
    end

    attributes[ :messages ]&.each do | message_attributes |
      @messages << Message.new( message_attributes[ :role ], message_attributes )
    end
  end
  
end

Instance Attribute Details

#messagesObject (readonly)

Returns the value of attribute messages.



13
14
15
# File 'lib/intelligence/conversation.rb', line 13

def messages
  @messages
end

#system_messageObject

Returns the value of attribute system_message.



12
13
14
# File 'lib/intelligence/conversation.rb', line 12

def system_message
  @system_message
end

Instance Method Details

#append_message(*messages) ⇒ Object Also known as: <<



50
51
52
53
# File 'lib/intelligence/conversation.rb', line 50

def append_message( *messages )
  @messages.concat( messages.flatten )
  self 
end

#has_messages?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/intelligence/conversation.rb', line 38

def has_messages?
  !@messages.empty?
end

#has_system_message?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/intelligence/conversation.rb', line 34

def has_system_message?
  ( @system_message || false ) && !@system_message.empty?
end

#to_hObject



57
58
59
60
61
62
# File 'lib/intelligence/conversation.rb', line 57

def to_h
  result = {}
  result[ :system_message ] = @system_message.to_h if @system_message
  result[ :messages ] = @messages.map { | m | m.to_h } 
  result
end