Class: Ollama::Handlers::Progress

Inherits:
Object
  • Object
show all
Includes:
Concern, Term::ANSIColor
Defined in:
lib/ollama/handlers/progress.rb

Overview

A handler that displays progress information for streaming operations.

This class is designed to provide visual feedback during long-running operations such as model creation, pulling, or pushing. It uses a progress bar to show the current status and estimated time remaining, making it easier to monitor the progress of these operations in terminal environments.

Examples:

Displaying progress for a model creation operation

ollama.create(model: 'my-model', stream: true, &Progress)

Instance Attribute Summary

Attributes included from Concern

#output, #result

Instance Method Summary collapse

Methods included from Concern

#to_proc

Constructor Details

#initialize(output: $stdout) ⇒ Progress

The initialize method sets up a new handler instance with the specified output destination and initializes internal state for progress tracking.

Parameters:

  • output (IO) (defaults to: $stdout)

    the output stream to be used for handling responses, defaults to $stdout



21
22
23
24
25
26
# File 'lib/ollama/handlers/progress.rb', line 21

def initialize(output: $stdout)
  super
  @current     = 0
  @total       = nil
  @last_status = nil
end

Instance Method Details

#call(response) ⇒ self

The call method processes a response by updating progress information and displaying status updates.

This method handles the display of progress information for streaming operations by updating the progress bar with current completion status, handling status changes, and displaying any error messages that occur during the operation. It manages internal state to track progress and ensures proper formatting of output.

progress information

response

Parameters:

Returns:

  • (self)

    returns the handler instance itself after processing the



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ollama/handlers/progress.rb', line 42

def call(response)
  infobar.display.output = @output
  if status = response.status
    infobar.label = status
  end
  if response.total && response.completed
    if !@last_status or @last_status != status
      @last_status and infobar.newline
      @last_status = status
      @current = 0
      @total = response.total
      infobar.counter.reset(total: @total, current: @current)
    end
    infobar.counter.progress(by: response.completed - @current)
    @current = response.completed
    infobar.update(
      message: message(response.completed, response.total),
      force: true
    )
  end
  if error = response.error
    infobar.puts bold { "Error: " } + red { error }
  end
  self
end

#message(current, total) ⇒ String (private)

The message method formats progress information into a descriptive string.

This method takes current and total values and creates a formatted progress message that includes the current value, total value, time elapsed, estimated time remaining, and the current rate of progress.

Parameters:

  • current (Integer)

    the current progress value

  • total (Integer)

    the total progress value

Returns:

  • (String)

    a formatted progress message containing current status, time information, and estimated completion details



81
82
83
84
85
86
# File 'lib/ollama/handlers/progress.rb', line 81

def message(current, total)
  progress = '%s/%s' % [ current, total ].map {
    Tins::Unit.format(_1, format: '%.2f %U')
  }
  '%l ' + progress + ' in %te, ETA %e @%E'
end