Class: Ollama::Messages
- Inherits:
-
Object
- Object
- Ollama::Messages
- Defined in:
- lib/ollama/messages.rb
Overview
Build the array-of-hash messages shape accepted by Client.chat(messages:).
Example:
= Ollama::Messages.new
.system("You are a senior Ruby engineer.")
.user("Review this code.", attachment: Ollama::Attachment.file("lib.rb"))
.image("What is in this screenshot?", file: "ui.png")
client.chat(messages: .to_h)
Instance Method Summary collapse
-
#add(message) ⇒ Object
Append an already-formed Ollama message hash or any object responding to
to_h. -
#append(role, content) ⇒ Object
Append a message of the given role.
-
#assistant(content) ⇒ Object
Add an assistant message.
-
#each(&block) ⇒ Object
Enumerate messages without exposing the backing array.
-
#image(text = nil, attachment:) ⇒ Object
Add an image-only user message.
-
#initialize(base = nil) ⇒ Messages
constructor
A new instance of Messages.
- #inspect ⇒ Object
-
#replace_or_append(role, content) ⇒ Object
Replace the first message with
roleif present; otherwise append. - #role_for(message) ⇒ Object
-
#size ⇒ Object
Return number of messages stored.
-
#system(content) ⇒ Object
Add/replace the system message.
-
#to_h ⇒ Object
Return the plain messages array accepted by
chat(messages:). -
#user(content, attachment: nil) ⇒ Object
Add a user message, optionally with an attachment.
Constructor Details
#initialize(base = nil) ⇒ Messages
Returns a new instance of Messages.
16 17 18 |
# File 'lib/ollama/messages.rb', line 16 def initialize(base = nil) @messages = Array(base).dup end |
Instance Method Details
#add(message) ⇒ Object
Append an already-formed Ollama message hash or any object responding to
to_h.
83 84 85 86 87 88 |
# File 'lib/ollama/messages.rb', line 83 def add() normalized = .is_a?(Hash) ? .dup : .to_h normalized = { "role" => "user", "content" => normalized.to_s } unless normalized.key?("role") || normalized.key?(:role) @messages << normalized self end |
#append(role, content) ⇒ Object
Append a message of the given role.
50 51 52 53 |
# File 'lib/ollama/messages.rb', line 50 def append(role, content) @messages << { "role" => role, "content" => content.to_s } self end |
#assistant(content) ⇒ Object
Add an assistant message.
56 57 58 |
# File 'lib/ollama/messages.rb', line 56 def assistant(content) append("assistant", content) end |
#each(&block) ⇒ Object
Enumerate messages without exposing the backing array.
101 102 103 |
# File 'lib/ollama/messages.rb', line 101 def each(&block) to_h.each(&block) end |
#image(text = nil, attachment:) ⇒ Object
Add an image-only user message. attachment may be an Attachment or a
path/IO/base64-string. Accepts text as optional user content.
73 74 75 76 77 78 79 |
# File 'lib/ollama/messages.rb', line 73 def image(text = nil, attachment:) = { "role" => "user", "content" => text ? text.to_s : "" } = Array().map { |a| a.is_a?(Attachment) ? a.to_h : a } ["images"] = @messages << self end |
#inspect ⇒ Object
105 106 107 |
# File 'lib/ollama/messages.rb', line 105 def inspect "#<#{self.class.name} size=#{@messages.size}>" end |
#replace_or_append(role, content) ⇒ Object
Replace the first message with role if present; otherwise append.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ollama/messages.rb', line 38 def replace_or_append(role, content) existing = @messages.index { |m| role_for(m) == role } = { "role" => role, "content" => content.to_s } if existing @messages[existing] = else @messages << end self end |
#role_for(message) ⇒ Object
31 32 33 34 35 |
# File 'lib/ollama/messages.rb', line 31 def role_for() return unless .is_a?(Hash) ([:role] || ["role"]).to_s end |
#size ⇒ Object
Return number of messages stored.
96 97 98 |
# File 'lib/ollama/messages.rb', line 96 def size @messages.size end |
#system(content) ⇒ Object
Add/replace the system message. When content is nil, removes any existing
system message.
22 23 24 25 26 27 28 29 |
# File 'lib/ollama/messages.rb', line 22 def system(content) if content.nil? @messages.reject! { |m| role_for(m) == "system" } else replace_or_append("system", content) end self end |
#to_h ⇒ Object
Return the plain messages array accepted by chat(messages:).
91 92 93 |
# File 'lib/ollama/messages.rb', line 91 def to_h @messages.dup end |
#user(content, attachment: nil) ⇒ Object
Add a user message, optionally with an attachment.
61 62 63 64 65 66 67 68 69 |
# File 'lib/ollama/messages.rb', line 61 def user(content, attachment: nil) = { "role" => "user", "content" => content.to_s } if = Array().map { |a| a.is_a?(Attachment) ? a.to_h : a } ["images"] = if .any? end @messages << self end |