Class: OmniAI::Chat::Prompt
- Inherits:
-
Object
- Object
- OmniAI::Chat::Prompt
- 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 ||
.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: []) @messages = end |
Instance Attribute Details
Class Method Details
.build {|Prompt| ... } ⇒ Prompt
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 |
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: @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 || @messages << 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 @messages.map { || .serialize(context:) } end |
#summarize ⇒ String
71 72 73 |
# File 'lib/omniai/chat/prompt.rb', line 71 def summarize @messages.map(&:summarize).join("\n\n") end |