Class: OmniAI::Chat::Prompt

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

Overview

Used to standardizes the process of building complex prompts.

Usage:

completion = OmniAI::Chat::Prompt.build do |prompt|
  prompt.system('You are a helpful assistant.')
  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"
  end
end

Defined Under Namespace

Classes: MessageError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messages: []) ⇒ Prompt

Returns a new instance of Prompt.

Parameters:

  • messages (Array<Message>) (defaults to: [])

    optional



56
57
58
# File 'lib/omniai/chat/prompt.rb', line 56

def initialize(messages: [])
  @messages = messages
end

Instance Attribute Details

#messagesArray<Message>

Returns:



22
23
24
# File 'lib/omniai/chat/prompt.rb', line 22

def messages
  @messages
end

Class Method Details

.build {|Prompt| ... } ⇒ Prompt

Usage:

OmniAI::Chat::Prompt.build do |prompt|
  prompt.system('You are an expert in geography.')
  prompt.user('What is the capital of Canada?')
end

Yields:

Returns:



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

def self.build(&block)
  new.tap do |prompt|
    block&.call(prompt)
  end
end

.parse(prompt) ⇒ Prompt

Usage:

OmniAI::Chat::Prompt.parse('What is the capital of Canada?')

Parameters:

  • prompt (nil, String)

Returns:



46
47
48
49
50
51
52
53
# File 'lib/omniai/chat/prompt.rb', line 46

def self.parse(prompt)
  new if prompt.nil?
  return prompt if prompt.is_a?(self)

  new.tap do |instance|
    instance.user(prompt)
  end
end

Instance Method Details

#assistant(content = nil) {|builder| ... } ⇒ Message

Examples:

prompt.assistant('the capital of Canada is Ottawa.')
prompt.assistant do |message|
  message.text 'the capital of Canada is Ottawa.'
end

Parameters:

  • content (String, nil) (defaults to: nil)

Yields:

  • (builder)

Yield Parameters:

Returns:



162
163
164
# File 'lib/omniai/chat/prompt.rb', line 162

def assistant(content = nil, &)
  message(content, role: Role::ASSISTANT, &)
end

#dupPrompt

Returns:



61
62
63
# File 'lib/omniai/chat/prompt.rb', line 61

def dup
  self.class.new(messages: @messages.dup)
end

#inspectString

Returns:

  • (String)


66
67
68
# File 'lib/omniai/chat/prompt.rb', line 66

def inspect
  "#<#{self.class.name} messages=#{@messages.inspect}>"
end

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

Examples:

prompt.message 'What is the capital of Canada?', role: '...'
prompt.message role: '...' 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:



104
105
106
107
108
# File 'lib/omniai/chat/prompt.rb', line 104

def message(content = nil, role: Role::USER, &)
  Message.build(content, role:, &).tap do |message|
    @messages << message
  end
end

#serialize(context: nil) ⇒ Array<Hash>

Usage:

prompt.serialize # => [{ content: "What is the capital of Canada?", role: :user }]

Parameters:

  • context (Context) (defaults to: nil)

    optional

Returns:

  • (Array<Hash>)


82
83
84
85
86
87
# File 'lib/omniai/chat/prompt.rb', line 82

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

  @messages.map { |message| message.serialize(context:) }
end

#summarizeString

Returns:

  • (String)


71
72
73
# File 'lib/omniai/chat/prompt.rb', line 71

def summarize
  @messages.map(&:summarize).join("\n\n")
end

#system(content = nil) {|builder| ... } ⇒ Message

Examples:

prompt.system 'You are a helpful assistant.'
prompt.system do |message|
  message.text 'You are a helpful assistant.'
end

Parameters:

  • content (String, nil) (defaults to: nil)

Yields:

  • (builder)

Yield Parameters:

Returns:



124
125
126
# File 'lib/omniai/chat/prompt.rb', line 124

def system(content = nil, &)
  message(content, role: Role::SYSTEM, &)
end

#user(content = nil) {|builder| ... } ⇒ Message

Examples:

prompt.user('What is the capital of Canada?')
prompt.user do |message|
  message.text 'What is the capital of Canada?'
  message.url 'https://...', type: "image/gif"
  message.file File.open('...'), type: "image/gif"
end

Parameters:

  • content (String, nil) (defaults to: nil)

Yields:

  • (builder)

Yield Parameters:

Returns:



144
145
146
# File 'lib/omniai/chat/prompt.rb', line 144

def user(content = nil, &)
  message(content, role: Role::USER, &)
end