Class: Intelligence::Message

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

Constant Summary collapse

ROLES =
[ :system, :user, :assistant ]

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)


75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/intelligence/message.rb', line 75

def initialize( role, attributes = nil )
  @role = role&.to_sym 
  @contents = []   

  raise ArgumentError.new( "The role is invalid. It must be one of #{ROLES.join( ', ' )}." ) \
    unless ROLES.include?( @role )

  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.



32
33
34
# File 'lib/intelligence/message.rb', line 32

def contents
  @contents
end

#roleObject (readonly)

Returns the value of attribute role.



31
32
33
# File 'lib/intelligence/message.rb', line 31

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 an attribute 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.

Note that there is no corresponding build method because a Message strictly requires a role. It cannot be constructed without one.

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' )
end

end



70
71
72
73
# File 'lib/intelligence/message.rb', line 70

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

Instance Method Details

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



121
122
123
124
# File 'lib/intelligence/message.rb', line 121

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

#each_content(&block) ⇒ Object



117
118
119
# File 'lib/intelligence/message.rb', line 117

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

#empty?Boolean

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

Returns:

  • (Boolean)


92
93
94
# File 'lib/intelligence/message.rb', line 92

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.



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

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

#to_hObject



128
129
130
131
132
133
# File 'lib/intelligence/message.rb', line 128

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)


100
101
102
# File 'lib/intelligence/message.rb', line 100

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