Class: Intelligence::Message

Inherits:
Object
  • Object
show all
Extended by:
AdaptiveConfiguration::Configurable
Defined in:
lib/intelligence/message.rb

Constant Summary collapse

ROLES =
[ :system, :user, :assistant ]
CONFIGURATION =
Proc.new do
  parameter :role, Symbol, required: true 
  group :content, array: true, as: :contents do 
    parameter :type, Symbol, default: :text 
    parameter :text, String 
    parameter :content_type, String
    parameter :bytes, String 
    parameter :uri, URI
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role, attributes = nil) ⇒ Message

Returns a new instance of Message.

Raises:

  • (ArgumentError)


59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/intelligence/message.rb', line 59

def initialize( role, attributes = nil )
  raise ArgumentError.new( "The role is invalid. It must be one of #{ROLES.join( ', ' )}." ) \
    unless ROLES.include?( role&.to_sym )

      @role = role.to_sym 
  @contents = []   

  if attributes && attributes[ :contents ]
    attributes[ :contents ].each do | content |
      @contents << MessageContent.build!( content[ :type ], content )
    end 
  end
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



21
22
23
# File 'lib/intelligence/message.rb', line 21

def contents
  @contents
end

#roleObject (readonly)

Returns the value of attribute role.



20
21
22
# File 'lib/intelligence/message.rb', line 20

def role
  @role
end

Class Method Details

.build!(attributes = nil, &block) ⇒ Object

The build! class method constructs and returns a new Message instance. The build! method accepts message attributes and a block, which may be combined when constructing a Message. The role is required, either as a paramter or in the block. If the role is not present, an exception will be raised.

The block offers the role method, as well as a content method permiting the caller to set the role and add content respectivelly.

examples

message = Message.build!( role: :user )

message = Message.build! do

role :user 
content do 
   type :text 
   text 'this is a user message'
end

end

message = Message.build!( role: :user ) do

content text: 'this is a user message'

end

message = Message.build!( role: :user ) do

content text: 'what do you see in this image?'
content type: :binary do
  content_type 'image/png'
  bytes File.binread( '99_red_balloons.png' )


54
55
56
57
# File 'lib/intelligence/message.rb', line 54

def self.build!( attributes = nil, &block )
  configuration = self.configure!( attributes, &block ).to_h
  self.new( configuration[ :role ], configuration )
end

Instance Method Details

#append_content(content) ⇒ Object Also known as: <<



112
113
114
115
# File 'lib/intelligence/message.rb', line 112

def append_content( content )
  @contents.push( content ) unless content.nil?
  self 
end

#each_content(&block) ⇒ Object



108
109
110
# File 'lib/intelligence/message.rb', line 108

def each_content( &block )
  @contents.each( &block )
end

#empty?Boolean

The empty? method return true if the message has no content.

Returns:

  • (Boolean)


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

def empty?
  @contents.empty?
end

#textObject

The text method is a convenience that returns all text content in the message joined with a newline. Any non-text content is skipped. If there is no text content an empty string is returned.



93
94
95
96
97
98
99
# File 'lib/intelligence/message.rb', line 93

def text  
  result = []
  each_content do | content |
    result << content.text if content.is_a?( MessageContent::Text )
  end
  result.join( "\n" ) 
end

#to_hObject



101
102
103
104
105
106
# File 'lib/intelligence/message.rb', line 101

def to_h
  { 
    role: @role,
    contents: @contents.map { | c | c.to_h }
  }
end

#valid?Boolean

The valid? method returns true if the message has a valid role, has content, and the content is valid.

Returns:

  • (Boolean)


84
85
86
# File 'lib/intelligence/message.rb', line 84

def valid?
  ROLES.include?( @role ) && !@contents.empty? && @contents.all?{ | contents | contents.valid? }
end