Class: OmniAI::Chat::Prompt
- Inherits:
-
Object
- Object
- OmniAI::Chat::Prompt
- Defined in:
- lib/omniai/chat/prompt.rb
Overview
Used to standardize the process of building complex prompts.
Usage:
completion = OmniAI::Chat::Prompt.build do |prompt|
prompt.system('You are a helpful assistant.')
prompt.user do ||
.text 'What are these photos of?'
.url 'https://example.com/cat.jpg', type: "image/jpeg"
.url 'https://example.com/dog.jpg', type: "image/jpeg"
.file File.open('hamster.jpg'), type: "image/jpeg"
end
end
Defined Under Namespace
Classes: MessageError
Instance Attribute Summary collapse
Class Method Summary collapse
-
.build {|Prompt| ... } ⇒ Prompt
Usage:.
-
.parse(prompt) ⇒ Prompt
Usage:.
Instance Method Summary collapse
- #assistant(content = nil) {|builder| ... } ⇒ Message
- #dup ⇒ Prompt
-
#initialize(messages: []) ⇒ Prompt
constructor
A new instance of Prompt.
- #inspect ⇒ String
- #message(content = nil, role: Role::USER) {|builder| ... } ⇒ Message
-
#serialize(context: nil) ⇒ Array<Hash>
Usage:.
- #summarize ⇒ String
- #system(content = nil) {|builder| ... } ⇒ Message
- #user(content = nil) {|builder| ... } ⇒ Message
Constructor Details
#initialize(messages: []) ⇒ Prompt
Returns a new instance of Prompt.
56 57 58 |
# File 'lib/omniai/chat/prompt.rb', line 56 def initialize(messages: []) = end |
Instance Attribute Details
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
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?')
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
162 163 164 |
# File 'lib/omniai/chat/prompt.rb', line 162 def assistant(content = nil, &) (content, role: Role::ASSISTANT, &) end |
#dup ⇒ Prompt
61 62 63 |
# File 'lib/omniai/chat/prompt.rb', line 61 def dup self.class.new(messages: .dup) end |
#inspect ⇒ 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
104 105 106 107 108 |
# File 'lib/omniai/chat/prompt.rb', line 104 def (content = nil, role: Role::USER, &) Message.build(content, role:, &).tap do || << end end |
#serialize(context: nil) ⇒ Array<Hash>
Usage:
prompt.serialize # => [{ content: "What is the capital of Canada?", role: :user }]
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 .map { || .serialize(context:) } end |
#summarize ⇒ String
71 72 73 |
# File 'lib/omniai/chat/prompt.rb', line 71 def summarize .map(&:summarize).join("\n\n") end |
#system(content = nil) {|builder| ... } ⇒ Message
124 125 126 |
# File 'lib/omniai/chat/prompt.rb', line 124 def system(content = nil, &) (content, role: Role::SYSTEM, &) end |
#user(content = nil) {|builder| ... } ⇒ Message
144 145 146 |
# File 'lib/omniai/chat/prompt.rb', line 144 def user(content = nil, &) (content, role: Role::USER, &) end |