Class: Ollama::Utils::Fetcher

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

Defined Under Namespace

Modules: HeaderExtension Classes: RetryWithoutStreaming

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(debug: false, http_options: {}) ⇒ Fetcher

Returns a new instance of Fetcher.



71
72
73
74
75
76
# File 'lib/ollama/utils/fetcher.rb', line 71

def initialize(debug: false, http_options: {})
  @debug        = debug
  @started      = false
  @streaming    = true
  @http_options = http_options
end

Class Method Details

.execute(command, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ollama/utils/fetcher.rb', line 51

def self.execute(command, &block)
  Tempfile.open do |tmp|
    IO.popen(command) do |command|
      until command.eof?
        tmp.write command.read(1 << 14)
      end
      tmp.rewind
      tmp.extend(Ollama::Utils::Fetcher::HeaderExtension)
      tmp.content_type = MIME::Types['text/plain'].first
      block.(tmp)
    end
  end
rescue => e
  STDERR.puts "Cannot execute #{command.inspect} (#{e})"
  if @debug && !e.is_a?(RuntimeError)
    STDERR.puts "#{e.backtrace * ?\n}"
  end
  yield HeaderExtension.failed
end

.get(url, **options, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ollama/utils/fetcher.rb', line 23

def self.get(url, **options, &block)
  cache = options.delete(:cache) and
    cache = Ollama::Utils::CacheFetcher.new(cache)
  if result = cache&.get(url, &block)
    infobar.puts "Getting #{url.to_s.inspect} from cache."
    return result
  else
    new(**options).send(:get, url) do |tmp|
      result = block.(tmp)
      if cache && !tmp.is_a?(StringIO)
        tmp.rewind
        cache.put(url, tmp)
      end
      result
    end
  end
end

.read(filename, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/ollama/utils/fetcher.rb', line 41

def self.read(filename, &block)
  if File.exist?(filename)
    File.open(filename) do |file|
      file.extend(Ollama::Utils::Fetcher::HeaderExtension)
      file.content_type = MIME::Types.type_for(filename).first
      block.(file)
    end
  end
end