Class: OmniAI::Chat::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/omniai/chat/message.rb,
lib/omniai/chat/message/builder.rb

Overview

Used to standardize the process of building message within a prompt:

completion = client.chat do |prompt|
  prompt.user do |message|
    message.text 'What are these photos of?'
    message.url 'https://example.com/cat.jpg', type: "image/jpeg"
    message.url 'https://example.com/dog.jpg', type: "image/jpeg"
    message.file File.open('hamster.jpg'), type: "image/jpeg"
    message.
  end
end

Direct Known Subclasses

ToolCallMessage

Defined Under Namespace

Classes: Builder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content:, role: Role::USER, tool_call_list: nil) ⇒ Message

Returns a new instance of Message.

Parameters:

  • content (String, nil)
  • role (String) (defaults to: Role::USER)
  • tool_call_list (Array<ToolCall>, nil) (defaults to: nil)


53
54
55
56
57
# File 'lib/omniai/chat/message.rb', line 53

def initialize(content:, role: Role::USER, tool_call_list: nil)
  @content = content
  @role = role
  @tool_call_list = tool_call_list
end

Instance Attribute Details

#contentArray<Content>, String

Returns:



42
43
44
# File 'lib/omniai/chat/message.rb', line 42

def content
  @content
end

#roleString

Returns:

  • (String)


45
46
47
# File 'lib/omniai/chat/message.rb', line 45

def role
  @role
end

#tool_call_listArray<ToolCall>?

Returns:



48
49
50
# File 'lib/omniai/chat/message.rb', line 48

def tool_call_list
  @tool_call_list
end

Class Method Details

.build(content = nil, role: Role::USER) {|builder| ... } ⇒ Message

Examples:

prompt.build('What is the capital of Canada?')
prompt.build(role: 'user') do |message|
  message.text 'What is the capital of Canada?'
end

Parameters:

  • content (String, nil) (defaults to: nil)
  • role (Symbol) (defaults to: Role::USER)

Yields:

  • (builder)

Yield Parameters:

Returns:

Raises:

  • (ArgumentError)


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

def self.build(content = nil, role: Role::USER, &block)
  raise ArgumentError, 'content or block is required' if content.nil? && block.nil?

  Builder.build(role:) do |builder|
    builder.text(content) if content
    block&.call(builder)
  end
end

.deserialize(data, context: nil) ⇒ Message

Usage:

Message.deserialize({ role: :user, content: 'Hello!' }) # => #<Message ...>

Parameters:

  • data (Hash)
  • context (Context) (defaults to: nil)

    optional

Returns:



80
81
82
83
84
85
86
87
88
89
# File 'lib/omniai/chat/message.rb', line 80

def self.deserialize(data, context: nil)
  deserialize = context&.deserializer(:message)
  return deserialize.call(data, context:) if deserialize

  role = data['role']
  content = Content.deserialize(data['content'], context:)
  tool_call_list = data['tool_calls']&.map { |subdata| ToolCall.deserialize(subdata, context:) }

  new(content:, role:, tool_call_list:)
end

Instance Method Details

#arrayify(object) ⇒ Array

Parameters:

  • object (Object)

Returns:

  • (Array)


145
146
147
148
149
150
# File 'lib/omniai/chat/message.rb', line 145

def arrayify(object)
  return if object.nil?
  return object if object.is_a?(Array)

  [object]
end

#inspectString

Returns:

  • (String)


60
61
62
# File 'lib/omniai/chat/message.rb', line 60

def inspect
  "#<#{self.class} role=#{@role.inspect} content=#{@content.inspect}>"
end

#role?(role) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/omniai/chat/message.rb', line 110

def role?(role)
  String(@role).eql?(String(role))
end

#serialize(context: nil) ⇒ Hash

Usage:

message.serialize # => { role: :user, content: 'Hello!' }
message.serialize # => { role: :user, content: [{ type: 'text', text: 'Hello!' }] }

Parameters:

  • context (Context) (defaults to: nil)

    optional

Returns:

  • (Hash)


99
100
101
102
103
104
105
106
107
# File 'lib/omniai/chat/message.rb', line 99

def serialize(context: nil)
  serializer = context&.serializer(:message)
  return serializer.call(self, context:) if serializer

  content = @content.is_a?(Array) ? @content.map { |content| content.serialize(context:) } : @content
  tool_calls = @tool_call_list&.map { |tool_call| tool_call.serialize(context:) }

  { role: @role, content:, tool_calls: }.compact
end

#summarizeString

Returns:

  • (String)


65
66
67
68
69
70
# File 'lib/omniai/chat/message.rb', line 65

def summarize
  <<~TEXT
    #{@role}
    #{Content.summarize(@content)}
  TEXT
end

#system?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/omniai/chat/message.rb', line 115

def system?
  role?(Role::SYSTEM)
end

#textString?

Returns:

  • (String, nil)


135
136
137
138
139
140
141
# File 'lib/omniai/chat/message.rb', line 135

def text
  return if @content.nil?
  return @content if @content.is_a?(String)

  parts = arrayify(@content).filter { |content| content.is_a?(Text) }
  parts.map(&:text).join("\n") unless parts.empty?
end

#text?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/omniai/chat/message.rb', line 130

def text?
  !text.nil?
end

#tool?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/omniai/chat/message.rb', line 125

def tool?
  role?(Role::TOOL)
end

#user?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/omniai/chat/message.rb', line 120

def user?
  role?(Role::USER)
end