Class: OmniAI::Chat::Message::Builder

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

Overview

Used to build a message.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role: Role::USER) ⇒ Builder

Returns a new instance of Builder.

Parameters:

  • role (String) (defaults to: Role::USER)


17
18
19
20
# File 'lib/omniai/chat/message/builder.rb', line 17

def initialize(role: Role::USER)
  @role = role
  @content = []
end

Class Method Details

.build(role:) {|builder| ... } ⇒ Message

Parameters:

  • role (String)

Yields:

  • (builder)

Returns:



10
11
12
13
14
# File 'lib/omniai/chat/message/builder.rb', line 10

def self.build(role:)
  builder = new(role:)
  yield(builder)
  builder.build
end

Instance Method Details

#buildMessage

Returns:



23
24
25
# File 'lib/omniai/chat/message/builder.rb', line 23

def build
  Message.new(content: @content, role: @role)
end

#file(io, type) ⇒ File

Examples:

message.file(File.open('hamster.jpg'), type: "image/jpeg")

Parameters:

  • io (IO)
  • type (String)

Returns:



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

def file(io, type)
  File.new(io, type).tap do |file|
    @content << file
  end
end

#text(value) ⇒ Text

Examples:

message.text('What are these photos of?')

Parameters:

  • value (String)

Returns:



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

def text(value)
  Text.new(value).tap do |text|
    @content << text
  end
end

#url(uri, type) ⇒ URL

Examples:

message.url('https://example.com/hamster.jpg', type: "image/jpeg")

Parameters:

  • uri (String)
  • type (String)

Returns:



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

def url(uri, type)
  URL.new(uri, type).tap do |url|
    @content << url
  end
end