Class: Ollama::Attachment

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

Overview

Typed attachment payload for multimodal inputs.

Each variant normalizes to a single hash accepted by Ollama as the :images entry on a user message: { type: "...", data: "..." }.

Example:

Ollama::Attachment.file("photo.png")
# => { type: "file", data: "photo.png" }

Ollama::Attachment.base64(b64_string)
# => { type: "base64", data: b64_string }

Ollama::Attachment.url("https://...")
# => { type: "url", data: "https://..." }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, data) ⇒ Attachment

Returns a new instance of Attachment.

Parameters:

  • type (String, Symbol)

    one of file, base64, url

  • data (String, #read)


24
25
26
27
# File 'lib/ollama/attachment.rb', line 24

def initialize(type, data)
  @type = type.to_s
  @data = coerce(data)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



20
21
22
# File 'lib/ollama/attachment.rb', line 20

def data
  @data
end

#typeObject (readonly)

Returns the value of attribute type.



20
21
22
# File 'lib/ollama/attachment.rb', line 20

def type
  @type
end

Class Method Details

.base64(data) ⇒ Object

type: "base64", data: base64-encoded string



42
43
44
# File 'lib/ollama/attachment.rb', line 42

def self.base64(data)
  new("base64", data)
end

.file(path) ⇒ Object

type: "file", data: path (string or Pathname). Reads as base64 when a file path is supplied. Uses base64-encoded file when a file path is supplied.



31
32
33
34
35
36
37
38
39
# File 'lib/ollama/attachment.rb', line 31

def self.file(path)
  path = path.to_s
  data = if path.include?("\n") || path.include?("+")
           path
         else
           base64_from_file(path)
         end
  new("file", data)
end

.url(url) ⇒ Object

type: "url", data: remote URL



47
48
49
# File 'lib/ollama/attachment.rb', line 47

def self.url(url)
  new("url", url.to_s)
end

Instance Method Details

#to_hObject



51
52
53
# File 'lib/ollama/attachment.rb', line 51

def to_h
  { "type" => type, "data" => data }
end