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
Returns a new instance of Fetcher.
13
14
15
16
|
# File 'lib/ollama/utils/fetcher.rb', line 13
def initialize
@started = false
@streaming = true
end
|
Class Method Details
.get(url, &block) ⇒ Object
18
19
20
|
# File 'lib/ollama/utils/fetcher.rb', line 18
def self.get(url, &block)
new.get(url, &block)
end
|
.read(filename, &block) ⇒ Object
96
97
98
99
100
101
102
103
104
|
# File 'lib/ollama/utils/fetcher.rb', line 96
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/ollama/utils/fetcher.rb', line 74
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
66
67
68
69
70
71
72
|
# File 'lib/ollama/utils/fetcher.rb', line 66
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
22
23
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
|
# File 'lib/ollama/utils/fetcher.rb', line 22
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}"
unless e.is_a?(RuntimeError)
STDERR.puts "#{e.backtrace * ?\n}"
end
yield nil
end
|
56
57
58
59
60
|
# File 'lib/ollama/utils/fetcher.rb', line 56
def
{
'User-Agent' => Ollama::Client.user_agent,
}
end
|
#message(current, total) ⇒ Object
89
90
91
92
93
94
|
# File 'lib/ollama/utils/fetcher.rb', line 89
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
62
63
64
|
# File 'lib/ollama/utils/fetcher.rb', line 62
def middlewares
(Excon.defaults[:middlewares] + [ Excon::Middleware::RedirectFollower ]).uniq
end
|