Class: Ollama::Commands::PushBlob

Inherits:
Object
  • Object
show all
Includes:
Digester
Defined in:
lib/ollama/commands/push_blob.rb

Overview

A command class that represents the push blob API endpoint for Ollama.

This class is used to upload raw binary files (blobs) to the Ollama server. It accepts the content to be uploaded and automatically calculates the required SHA256 digest to target the correct API endpoint.

Examples:

Preparing a push blob command with a file

command = Ollama::Commands::PushBlob.new(body: File.open('model.gguf', 'rb'))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body:, digest: nil) ⇒ PushBlob

The initialize method sets up a new instance and calculates the content digest.

This method ensures the body is an IO-like object and computes the SHA256 hash of the content to be used as the target endpoint path.

Parameters:

  • body (IO, String)

    the binary content or file handle to upload

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

    optional precomputed SHA256 digest. If provided, it takes precedence over computing one from the body.



23
24
25
26
27
28
# File 'lib/ollama/commands/push_blob.rb', line 23

def initialize(body:, digest: nil)
  @body   = body.respond_to?(:read) ? body : StringIO.new(body.to_str)
  digest = prefix_sha256(digest)
  @digest = digest || compute_digest(@body)
  @stream = false
end

Instance Attribute Details

#bodyObject (readonly)

The body attribute reader returns the binary content to be uploaded to the Ollama server.



34
35
36
# File 'lib/ollama/commands/push_blob.rb', line 34

def body
  @body
end

#client=(value) ⇒ Object (writeonly)

The client attribute writer allows setting the client instance associated with the object.



50
51
52
# File 'lib/ollama/commands/push_blob.rb', line 50

def client=(value)
  @client = value
end

#digestString (readonly)

The digest attribute reader returns the target blob digest.

Returns:

  • (String)

    the SHA256 digest of the blob



39
40
41
# File 'lib/ollama/commands/push_blob.rb', line 39

def digest
  @digest
end

#streamFalseClass (readonly)

The stream attribute reader returns the streaming behavior setting.

Returns:

  • (FalseClass)

    the streaming behavior flag (always false for this command)



44
45
46
# File 'lib/ollama/commands/push_blob.rb', line 44

def stream
  @stream
end

Instance Method Details

#pathString

The path method returns the API endpoint path for pushing blobs.

This method interpolates the digest into the URL to target a specific blob.

Returns:

  • (String)

    the API endpoint path ‘/api/blobs/<digest>’



57
58
59
# File 'lib/ollama/commands/push_blob.rb', line 57

def path
  "/api/blobs/#{digest}"
end

#perform(handler) ⇒ self

The perform method is not used directly for file uploads in this implementation, as the client uses a specialized streaming request method to handle IO. However, it’s kept for interface consistency.

Parameters:

  • handler (Ollama::Handler)

    the handler object responsible for processing API responses

Returns:

  • (self)

    returns the current instance after initiating the request



69
70
71
72
73
# File 'lib/ollama/commands/push_blob.rb', line 69

def perform(handler)
  @client.blob_exists?(digest) or @client.upload_file(path:, body:, handler:)
  handler.result = digest
  self
end