Class: Ollama::Image

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

Overview

Ollama::Image

Represents an image object that can be used in messages sent to the Ollama API. The Image class provides methods to create image objects from various sources including base64 encoded strings, file paths, IO objects, and raw string data.

Examples:

Creating an image from a file path

image = Ollama::Image.for_filename('path/to/image.jpg')

Creating an image from a base64 string

image = Ollama::Image.for_base64('base64encodedstring', path: 'image.jpg')

Creating an image from a string

image = Ollama::Image.for_string('raw image data')

Creating an image from an IO object

File.open('path/to/image.jpg', 'rb') do |io|
  image = Ollama::Image.for_io(io, path: 'image.jpg')
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Image

Initializes a new Image object with the provided data.

Parameters:

  • data (String)

    the image data to store in the object



26
27
28
# File 'lib/ollama/image.rb', line 26

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataString (readonly)

The data attribute reader returns the image data stored in the object.

Returns:

  • (String)

    the raw image data contained within the image object



38
39
40
# File 'lib/ollama/image.rb', line 38

def data
  @data
end

#pathString?

The path attribute stores the file path associated with the image object.

Returns:

  • (String, nil)

    the path to the image file, or nil if not set



33
34
35
# File 'lib/ollama/image.rb', line 33

def path
  @path
end

Class Method Details

.for_base64(data, path: nil) ⇒ Ollama::Image

Creates a new Image object from base64 encoded data.

provided data and path

Parameters:

  • data (String)

    the base64 encoded image data

  • path (String, nil) (defaults to: nil)

    the optional file path associated with the image

Returns:



48
49
50
51
52
# File 'lib/ollama/image.rb', line 48

def for_base64(data, path: nil)
  obj = new(data)
  obj.path = path
  obj
end

.for_filename(path) ⇒ Ollama::Image

Creates a new Image object from a file path by opening the file in binary mode and passing it to the for_io method.

contents of the file at the specified path

Parameters:

  • path (String)

    the file system path to the image file

Returns:



85
86
87
# File 'lib/ollama/image.rb', line 85

def for_filename(path)
  File.open(path, 'rb') { |io| for_io(io, path:) }
end

.for_io(io, path: nil) ⇒ Ollama::Image

Creates a new Image object from an IO object by reading its contents and optionally setting a path.

object’s data and optional path

Parameters:

  • io (IO)

    the IO object to read image data from

  • path (String, nil) (defaults to: nil)

    the optional file path associated with the image

Returns:

  • (Ollama::Image)

    a new Image instance initialized with the IO



73
74
75
76
# File 'lib/ollama/image.rb', line 73

def for_io(io, path: nil)
  path ||= io.path
  for_string(io.read, path:)
end

.for_string(string, path: nil) ⇒ Ollama::Image

Creates a new Image object from a string by encoding it to base64.

encoded string data and optional path

Parameters:

  • string (String)

    the raw string data to be converted into an image

  • path (String, nil) (defaults to: nil)

    the optional file path associated with the image

Returns:



61
62
63
# File 'lib/ollama/image.rb', line 61

def for_string(string, path: nil)
  for_base64(Base64.encode64(string), path:)
end

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass

The == method compares two Image objects for equality based on their data contents.

data, false otherwise

Parameters:

  • other (Ollama::Image)

    the other Image object to compare against

Returns:

  • (TrueClass, FalseClass)

    true if both Image objects have identical



99
100
101
# File 'lib/ollama/image.rb', line 99

def ==(other)
  @data == other.data
end

#to_sString

The to_s method returns the raw image data stored in the Image object.

Returns:

  • (String)

    the raw image data contained within the image object



106
107
108
# File 'lib/ollama/image.rb', line 106

def to_s
  @data
end