Class: OmniAI::Chat::Message
- Inherits:
-
Object
- Object
- OmniAI::Chat::Message
- 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
Defined Under Namespace
Classes: Builder
Instance Attribute Summary collapse
Class Method Summary collapse
- .build(content = nil, role: Role::USER) {|builder| ... } ⇒ Message
-
.deserialize(data, context: nil) ⇒ Message
Usage:.
Instance Method Summary collapse
- #arrayify(object) ⇒ Array
-
#initialize(content:, role: Role::USER, tool_call_list: nil) ⇒ Message
constructor
A new instance of Message.
- #inspect ⇒ String
- #role?(role) ⇒ Boolean
-
#serialize(context: nil) ⇒ Hash
Usage:.
- #summarize ⇒ String
- #system? ⇒ Boolean
- #text ⇒ String?
- #text? ⇒ Boolean
- #tool? ⇒ Boolean
- #user? ⇒ Boolean
Constructor Details
Instance Attribute Details
#content ⇒ Array<Content>, String
42 43 44 |
# File 'lib/omniai/chat/message.rb', line 42 def content @content end |
#role ⇒ String
45 46 47 |
# File 'lib/omniai/chat/message.rb', line 45 def role @role end |
#tool_call_list ⇒ Array<ToolCall>?
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
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 ...>
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
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 |
#inspect ⇒ 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
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:
.serialize # => { role: :user, content: 'Hello!' }
.serialize # => { role: :user, content: [{ type: 'text', text: 'Hello!' }] }
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 |
#summarize ⇒ 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
115 116 117 |
# File 'lib/omniai/chat/message.rb', line 115 def system? role?(Role::SYSTEM) end |
#text ⇒ String?
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
130 131 132 |
# File 'lib/omniai/chat/message.rb', line 130 def text? !text.nil? end |