Class: Ollama::Utils::Fetcher
- Inherits:
-
Object
- Object
- Ollama::Utils::Fetcher
show all
- Defined in:
- lib/ollama/utils/fetcher.rb
Defined Under Namespace
Modules: ContentType
Classes: RetryWithoutStreaming
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(debug: false) ⇒ Fetcher
Returns a new instance of Fetcher.
14
15
16
17
18
|
# File 'lib/ollama/utils/fetcher.rb', line 14
def initialize(debug: false)
@debug = debug
@started = false
@streaming = true
end
|
Class Method Details
.get(url, **options, &block) ⇒ Object
20
21
22
|
# File 'lib/ollama/utils/fetcher.rb', line 20
def self.get(url, **options, &block)
new(**options).get(url, &block)
end
|
.read(filename, &block) ⇒ Object
98
99
100
101
102
103
104
105
106
|
# File 'lib/ollama/utils/fetcher.rb', line 98
def self.read(filename, &block)
if File.exist?(filename)
File.open(filename) do |file|
file.extend(Ollama::Utils::Fetcher::ContentType)
file.content_type = MIME::Types.type_for(filename).first
block.(file)
end
end
end
|
Instance Method Details
#callback(tmp) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/ollama/utils/fetcher.rb', line 76
def callback(tmp)
-> chunk, remaining_bytes, total_bytes do
total = total_bytes or next
current = total_bytes - remaining_bytes
if @started
infobar.counter.progress(by: total - current)
else
@started = true
infobar.counter.reset(total:, current:)
end
infobar.update(message: message(current, total), force: true)
tmp.print(chunk)
end
end
|
#decorate_io(tmp, response) ⇒ Object
68
69
70
71
72
73
74
|
# File 'lib/ollama/utils/fetcher.rb', line 68
def decorate_io(tmp, response)
tmp.rewind
tmp.extend(ContentType)
if content_type = MIME::Types[response.['content-type']].first
tmp.content_type = content_type
end
end
|
#get(url, &block) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/ollama/utils/fetcher.rb', line 24
def get(url, &block)
response = nil
Tempfile.open do |tmp|
infobar.label = 'Getting'
if @streaming
response = Excon.get(url, headers:, response_block: callback(tmp))
response.status != 200 || !@started and raise RetryWithoutStreaming
decorate_io(tmp, response)
infobar.finish
block.(tmp)
else
response = Excon.get(url, headers:, middlewares:)
if response.status != 200
raise "invalid response status code"
end
body = response.body
tmp.print body
infobar.update(message: message(body.size, body.size), force: true)
decorate_io(tmp, response)
infobar.finish
block.(tmp)
end
end
rescue RetryWithoutStreaming
@streaming = false
retry
rescue => e
STDERR.puts "Cannot get #{url.to_s.inspect} (#{e}): #{response&.status_line || 'n/a'}"
if @debug && !e.is_a?(RuntimeError)
STDERR.puts "#{e.backtrace * ?\n}"
end
yield StringIO.new.extend(ContentType)
end
|
58
59
60
61
62
|
# File 'lib/ollama/utils/fetcher.rb', line 58
def
{
'User-Agent' => Ollama::Client.user_agent,
}
end
|
#message(current, total) ⇒ Object
91
92
93
94
95
96
|
# File 'lib/ollama/utils/fetcher.rb', line 91
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
|
#middlewares ⇒ Object
64
65
66
|
# File 'lib/ollama/utils/fetcher.rb', line 64
def middlewares
(Excon.defaults[:middlewares] + [ Excon::Middleware::RedirectFollower ]).uniq
end
|