Class: Ollama::Messages

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama/messages.rb

Overview

Build the array-of-hash messages shape accepted by Client.chat(messages:).

Example:

messages = 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: messages.to_h)

Instance Method Summary collapse

Constructor Details

#initialize(base = nil) ⇒ Messages

Returns a new instance of Messages.

Parameters:

  • base (Array<Hash>, nil) (defaults to: nil)

    starting messages to copy into this builder



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(message)
  normalized = message.is_a?(Hash) ? message.dup : message.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:)
  message = { "role" => "user", "content" => text ? text.to_s : "" }
  attachments = Array(attachment).map { |a| a.is_a?(Attachment) ? a.to_h : a }
  message["images"] = attachments
  @messages << message
  self
end

#inspectObject



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 }
  message = { "role" => role, "content" => content.to_s }
  if existing
    @messages[existing] = message
  else
    @messages << message
  end
  self
end

#role_for(message) ⇒ Object



31
32
33
34
35
# File 'lib/ollama/messages.rb', line 31

def role_for(message)
  return unless message.is_a?(Hash)

  (message[:role] || message["role"]).to_s
end

#sizeObject

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_hObject

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)
  message = { "role" => "user", "content" => content.to_s }
  if attachment
    attachments = Array(attachment).map { |a| a.is_a?(Attachment) ? a.to_h : a }
    message["images"] = attachments if attachments.any?
  end
  @messages << message
  self
end