ollama-ruby 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,119 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Ollama::Documents do
4
+ let :ollama do
5
+ double('Ollama::Client')
6
+ end
7
+
8
+ let :model do
9
+ 'mxbai-embed-large'
10
+ end
11
+
12
+ let :documents do
13
+ described_class.new ollama:, model:
14
+ end
15
+
16
+ it 'can be instantiated' do
17
+ expect(documents).to be_a described_class
18
+ end
19
+
20
+ it 'no texts can be added to it' do
21
+ expect(documents.add([])).to eq documents
22
+ end
23
+
24
+ it 'texts can be added to it' do
25
+ expect(ollama).to receive(:embed).
26
+ with(model:, input: %w[ foo bar ], options: nil).
27
+ and_return(double(embeddings: [ [ 0.1 ], [ 0.2 ] ]))
28
+ expect(documents.add(%w[ foo bar ])).to eq documents
29
+ expect(documents.exist?('foo')).to eq true
30
+ expect(documents.exist?('bar')).to eq true
31
+ expect(documents['foo']).to be_a Ollama::Documents::Record
32
+ end
33
+
34
+ it 'a text can be added to it' do
35
+ expect(ollama).to receive(:embed).
36
+ with(model:, input: %w[ foo ], options: nil).
37
+ and_return(double(embeddings: [ [ 0.1 ] ]))
38
+ expect(documents << 'foo').to eq documents
39
+ expect(documents.exist?('foo')).to eq true
40
+ expect(documents.exist?('bar')).to eq false
41
+ expect(documents['foo']).to be_a Ollama::Documents::Record
42
+ end
43
+
44
+ it 'can find strings' do
45
+ allow(ollama).to receive(:embed).
46
+ with(model:, input: [ 'foo' ], options: nil).
47
+ and_return(double(embeddings: [ [ 0.1 ] ]))
48
+ expect(documents << 'foo').to eq documents
49
+ expect(ollama).to receive(:embed).
50
+ with(model:, input: 'foo', options: nil).
51
+ and_return(double(embeddings: [ [ 0.1 ] ]))
52
+ records = documents.find('foo')
53
+ expect(records).to eq [
54
+ Ollama::Documents::Record[text: 'foo', embedding: [ 0.1 ], similarity: 1.0 ]
55
+ ]
56
+ expect(records[0].to_s).to eq '#<Ollama::Documents::Record "foo" 1.0>'
57
+ end
58
+
59
+ it 'can find only tagged strings' do
60
+ allow(ollama).to receive(:embed).
61
+ with(model:, input: [ 'foo' ], options: nil).
62
+ and_return(double(embeddings: [ [ 0.1 ] ]))
63
+ expect(documents.add('foo', tags: %i[ test ])).to eq documents
64
+ expect(ollama).to receive(:embed).
65
+ with(model:, input: 'foo', options: nil).
66
+ and_return(double(embeddings: [ [ 0.1 ] ]))
67
+ records = documents.find('foo', tags: %i[ nix ])
68
+ expect(records).to eq []
69
+ expect(ollama).to receive(:embed).
70
+ with(model:, input: 'foo', options: nil).
71
+ and_return(double(embeddings: [ [ 0.1 ] ]))
72
+ records = documents.find('foo', tags: %i[ test ])
73
+ expect(records).to eq [
74
+ Ollama::Documents::Record[text: 'foo', embedding: [ 0.1 ], similarity: 1.0 ]
75
+ ]
76
+ expect(records[0].to_s).to eq '#<Ollama::Documents::Record "foo" #test 1.0>'
77
+ end
78
+
79
+ context 'it uses cache' do
80
+ before do
81
+ allow(ollama).to receive(:embed).
82
+ with(model:, input: %w[ foo ], options: nil).
83
+ and_return(double(embeddings: [ [ 0.1 ] ]))
84
+ end
85
+
86
+ it 'can delete texts' do
87
+ expect(documents << 'foo').to eq documents
88
+ expect {
89
+ documents.delete('foo')
90
+ }.to change { documents.exist?('foo') }.from(true).to(false)
91
+ end
92
+
93
+ it 'tracks size' do
94
+ expect {
95
+ expect(documents << 'foo').to eq documents
96
+ }.to change { documents.size }.from(0).to(1)
97
+ end
98
+
99
+ it 'can clear texts' do
100
+ expect(documents << 'foo').to eq documents
101
+ expect {
102
+ documents.clear
103
+ }.to change { documents.size }.from(1).to(0)
104
+ end
105
+
106
+ it 'returns collections' do
107
+ expect(documents.collections).to eq [ :default ]
108
+ end
109
+
110
+ it 'can change collection' do
111
+ expect(documents.instance_eval { @cache }).to receive(:prefix=).
112
+ with(/#@collection/).and_call_original
113
+ expect { documents.collection = :new_collection }.
114
+ to change { documents.collection }.
115
+ from(:default).
116
+ to(:new_collection)
117
+ end
118
+ end
119
+ end
@@ -7,7 +7,7 @@ RSpec.describe Ollama::Handlers::Progress do
7
7
  end
8
8
 
9
9
  it 'can display progress' do
10
- response = double('response', status: 'testing', completed: 23, total: 666)
10
+ response = double('response', status: 'testing', completed: 23, total: 666, error: nil)
11
11
  expect(infobar.counter).to receive(:progress).with(by: 23).and_call_original
12
12
  expect(infobar.display).to receive(:update).and_call_original
13
13
  described_class.new.call(response)
@@ -16,7 +16,7 @@ RSpec.describe Ollama::Handlers::Progress do
16
16
  it 'can display errors in progress' do
17
17
  response = double('response', error: 'foo', status: nil, completed: nil, total: nil)
18
18
  progress = described_class.new
19
- expect(progress.output).to receive(:puts).with(/Error: .*foo/)
19
+ expect(infobar).to receive(:puts).with(/Error: .*foo/)
20
20
  progress.call(response)
21
21
  end
22
22
  end
@@ -20,4 +20,8 @@ RSpec.describe Ollama::Image do
20
20
  expect(image.to_s.sum).to eq 42460
21
21
  expect(image.to_s[0, 40]).to eq '/9j/4AAQSkZJRgABAQAASABIAAD/4QBYRXhpZgAA'
22
22
  end
23
+
24
+ it 'tracks path of file' do
25
+ expect(image.path).to eq asset('kitten.jpg')
26
+ end
23
27
  end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Ollama::Utils::Fetcher do
4
+ let :url do
5
+ 'https://www.example.com/hello'
6
+ end
7
+
8
+ let :fetcher do
9
+ described_class.new
10
+ end
11
+
12
+ it 'can be instantiated' do
13
+ expect(fetcher).to be_a described_class
14
+ end
15
+
16
+ it 'has .get' do
17
+ expect(described_class).to receive(:new).and_return double(get: true)
18
+ described_class.get(url)
19
+ end
20
+
21
+ it 'can #get with streaming' do
22
+ stub_request(:get, 'https://www.example.com/hello').
23
+ with(headers: fetcher.headers).
24
+ to_return(
25
+ status: 200,
26
+ body: 'world',
27
+ headers: { 'Content-Type' => 'text/plain' },
28
+ )
29
+ fetcher.get(url) do |tmp|
30
+ expect(tmp).to be_a Tempfile
31
+ expect(tmp.read).to eq 'world'
32
+ expect(tmp.content_type).to eq 'text/plain'
33
+ end
34
+ end
35
+
36
+ it 'can #get and fallback from streaming' do
37
+ stub_request(:get, 'https://www.example.com/hello').
38
+ with(headers: fetcher.headers).
39
+ to_return(
40
+ { status: 501 },
41
+ {
42
+ status: 200,
43
+ body: 'world',
44
+ headers: { 'Content-Type' => 'text/plain' },
45
+ }
46
+ )
47
+ fetcher.get(url) do |tmp|
48
+ expect(tmp).to be_a Tempfile
49
+ expect(tmp.read).to eq 'world'
50
+ expect(tmp.content_type).to eq 'text/plain'
51
+ end
52
+ end
53
+
54
+ it 'can #get and finally fail' do
55
+ stub_request(:get, 'https://www.example.com/hello').
56
+ with(headers: fetcher.headers).
57
+ to_return(status: 500)
58
+ fetcher.get(url) do |tmp|
59
+ expect(tmp).to be_nil
60
+ end
61
+ end
62
+
63
+ it 'can redirect' do
64
+ expect(fetcher.middlewares).to include Excon::Middleware::RedirectFollower
65
+ end
66
+
67
+ it 'can .read' do
68
+ described_class.read(__FILE__) do |file|
69
+ expect(file).to be_a File
70
+ expect(file.read).to include 'can .read'
71
+ expect(file.content_type).to eq 'application/x-ruby'
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Ollama::Utils::Tags do
4
+ it 'can be instantiated' do
5
+ expect(described_class.new).to be_a described_class
6
+ end
7
+
8
+ it 'can contain unique tags and is sorted' do
9
+ tags = described_class.new([ 'bar', 'foo'])
10
+ expect(tags.to_a).to eq %w[ bar foo ]
11
+ end
12
+
13
+ it 'tags can be added to it' do
14
+ tags = described_class.new([ 'foo' ])
15
+ tags.add 'bar'
16
+ expect(tags.to_a).to eq %w[ bar foo ]
17
+ tags.merge [ 'baz', 'baz2' ]
18
+ expect(tags.to_a).to eq %w[ bar baz baz2 foo ]
19
+ end
20
+
21
+ it 'can be output nicely' do
22
+ expect(described_class.new(%w[foo bar]).to_s).to eq '#bar #foo'
23
+ end
24
+ end
data/spec/spec_helper.rb CHANGED
@@ -9,8 +9,16 @@ begin
9
9
  require 'debug'
10
10
  rescue LoadError
11
11
  end
12
+ require 'webmock/rspec'
13
+ WebMock.disable_net_connect!
12
14
  require 'ollama'
13
15
 
14
16
  def asset(name)
15
17
  File.join(__dir__, 'assets', name)
16
18
  end
19
+
20
+ RSpec.configure do |config|
21
+ config.before(:suite) do
22
+ infobar.show = nil
23
+ end
24
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ollama-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-16 00:00:00.000000000 Z
11
+ date: 2024-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gem_hadar
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 1.16.1
19
+ version: 1.17.0
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 1.16.1
26
+ version: 1.17.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: all_images
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: excon
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -136,11 +150,138 @@ dependencies:
136
150
  - - "~>"
137
151
  - !ruby/object:Gem::Version
138
152
  version: '3.0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: redis
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '5.0'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '5.0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: numo-narray
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '0.9'
174
+ type: :runtime
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '0.9'
181
+ - !ruby/object:Gem::Dependency
182
+ name: more_math
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: '1.1'
188
+ type: :runtime
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: '1.1'
195
+ - !ruby/object:Gem::Dependency
196
+ name: sorted_set
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - "~>"
200
+ - !ruby/object:Gem::Version
201
+ version: '1.0'
202
+ type: :runtime
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - "~>"
207
+ - !ruby/object:Gem::Version
208
+ version: '1.0'
209
+ - !ruby/object:Gem::Dependency
210
+ name: mime-types
211
+ requirement: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - "~>"
214
+ - !ruby/object:Gem::Version
215
+ version: '3.0'
216
+ type: :runtime
217
+ prerelease: false
218
+ version_requirements: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - "~>"
221
+ - !ruby/object:Gem::Version
222
+ version: '3.0'
223
+ - !ruby/object:Gem::Dependency
224
+ name: reverse_markdown
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - "~>"
228
+ - !ruby/object:Gem::Version
229
+ version: '2.0'
230
+ type: :runtime
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - "~>"
235
+ - !ruby/object:Gem::Version
236
+ version: '2.0'
237
+ - !ruby/object:Gem::Dependency
238
+ name: complex_config
239
+ requirement: !ruby/object:Gem::Requirement
240
+ requirements:
241
+ - - "~>"
242
+ - !ruby/object:Gem::Version
243
+ version: '0.20'
244
+ type: :runtime
245
+ prerelease: false
246
+ version_requirements: !ruby/object:Gem::Requirement
247
+ requirements:
248
+ - - "~>"
249
+ - !ruby/object:Gem::Version
250
+ version: '0.20'
251
+ - !ruby/object:Gem::Dependency
252
+ name: search_ui
253
+ requirement: !ruby/object:Gem::Requirement
254
+ requirements:
255
+ - - "~>"
256
+ - !ruby/object:Gem::Version
257
+ version: '0.0'
258
+ type: :runtime
259
+ prerelease: false
260
+ version_requirements: !ruby/object:Gem::Requirement
261
+ requirements:
262
+ - - "~>"
263
+ - !ruby/object:Gem::Version
264
+ version: '0.0'
265
+ - !ruby/object:Gem::Dependency
266
+ name: amatch
267
+ requirement: !ruby/object:Gem::Requirement
268
+ requirements:
269
+ - - "~>"
270
+ - !ruby/object:Gem::Version
271
+ version: 0.4.1
272
+ type: :runtime
273
+ prerelease: false
274
+ version_requirements: !ruby/object:Gem::Requirement
275
+ requirements:
276
+ - - "~>"
277
+ - !ruby/object:Gem::Version
278
+ version: 0.4.1
139
279
  description: Library that allows interacting with the Ollama API
140
280
  email: flori@ping.de
141
281
  executables:
142
282
  - ollama_console
143
283
  - ollama_chat
284
+ - ollama_update
144
285
  extensions: []
145
286
  extra_rdoc_files:
146
287
  - README.md
@@ -160,6 +301,11 @@ extra_rdoc_files:
160
301
  - lib/ollama/commands/push.rb
161
302
  - lib/ollama/commands/show.rb
162
303
  - lib/ollama/commands/tags.rb
304
+ - lib/ollama/documents.rb
305
+ - lib/ollama/documents/memory_cache.rb
306
+ - lib/ollama/documents/redis_cache.rb
307
+ - lib/ollama/documents/splitters/character.rb
308
+ - lib/ollama/documents/splitters/semantic.rb
163
309
  - lib/ollama/dto.rb
164
310
  - lib/ollama/errors.rb
165
311
  - lib/ollama/handlers.rb
@@ -182,15 +328,24 @@ extra_rdoc_files:
182
328
  - lib/ollama/tool/function/parameters.rb
183
329
  - lib/ollama/tool/function/parameters/property.rb
184
330
  - lib/ollama/utils/ansi_markdown.rb
331
+ - lib/ollama/utils/chooser.rb
332
+ - lib/ollama/utils/colorize_texts.rb
333
+ - lib/ollama/utils/fetcher.rb
334
+ - lib/ollama/utils/math.rb
335
+ - lib/ollama/utils/tags.rb
185
336
  - lib/ollama/utils/width.rb
186
337
  - lib/ollama/version.rb
187
338
  files:
339
+ - ".envrc"
188
340
  - Gemfile
189
341
  - LICENSE
190
342
  - README.md
191
343
  - Rakefile
192
344
  - bin/ollama_chat
193
345
  - bin/ollama_console
346
+ - bin/ollama_update
347
+ - config/redis.conf
348
+ - docker-compose.yml
194
349
  - lib/ollama.rb
195
350
  - lib/ollama/client.rb
196
351
  - lib/ollama/client/command.rb
@@ -207,6 +362,11 @@ files:
207
362
  - lib/ollama/commands/push.rb
208
363
  - lib/ollama/commands/show.rb
209
364
  - lib/ollama/commands/tags.rb
365
+ - lib/ollama/documents.rb
366
+ - lib/ollama/documents/memory_cache.rb
367
+ - lib/ollama/documents/redis_cache.rb
368
+ - lib/ollama/documents/splitters/character.rb
369
+ - lib/ollama/documents/splitters/semantic.rb
210
370
  - lib/ollama/dto.rb
211
371
  - lib/ollama/errors.rb
212
372
  - lib/ollama/handlers.rb
@@ -229,9 +389,15 @@ files:
229
389
  - lib/ollama/tool/function/parameters.rb
230
390
  - lib/ollama/tool/function/parameters/property.rb
231
391
  - lib/ollama/utils/ansi_markdown.rb
392
+ - lib/ollama/utils/chooser.rb
393
+ - lib/ollama/utils/colorize_texts.rb
394
+ - lib/ollama/utils/fetcher.rb
395
+ - lib/ollama/utils/math.rb
396
+ - lib/ollama/utils/tags.rb
232
397
  - lib/ollama/utils/width.rb
233
398
  - lib/ollama/version.rb
234
399
  - ollama-ruby.gemspec
400
+ - spec/assets/embeddings.json
235
401
  - spec/assets/kitten.jpg
236
402
  - spec/ollama/client/doc_spec.rb
237
403
  - spec/ollama/client_spec.rb
@@ -247,6 +413,11 @@ files:
247
413
  - spec/ollama/commands/push_spec.rb
248
414
  - spec/ollama/commands/show_spec.rb
249
415
  - spec/ollama/commands/tags_spec.rb
416
+ - spec/ollama/documents/memory_cache_spec.rb
417
+ - spec/ollama/documents/redis_cache_spec.rb
418
+ - spec/ollama/documents/splitters/character_spec.rb
419
+ - spec/ollama/documents/splitters/semantic_spec.rb
420
+ - spec/ollama/documents_spec.rb
250
421
  - spec/ollama/handlers/collector_spec.rb
251
422
  - spec/ollama/handlers/dump_json_spec.rb
252
423
  - spec/ollama/handlers/dump_yaml_spec.rb
@@ -261,6 +432,8 @@ files:
261
432
  - spec/ollama/options_spec.rb
262
433
  - spec/ollama/tool_spec.rb
263
434
  - spec/ollama/utils/ansi_markdown_spec.rb
435
+ - spec/ollama/utils/fetcher_spec.rb
436
+ - spec/ollama/utils/tags_spec.rb
264
437
  - spec/spec_helper.rb
265
438
  homepage: https://github.com/flori/ollama-ruby
266
439
  licenses:
@@ -304,6 +477,11 @@ test_files:
304
477
  - spec/ollama/commands/push_spec.rb
305
478
  - spec/ollama/commands/show_spec.rb
306
479
  - spec/ollama/commands/tags_spec.rb
480
+ - spec/ollama/documents/memory_cache_spec.rb
481
+ - spec/ollama/documents/redis_cache_spec.rb
482
+ - spec/ollama/documents/splitters/character_spec.rb
483
+ - spec/ollama/documents/splitters/semantic_spec.rb
484
+ - spec/ollama/documents_spec.rb
307
485
  - spec/ollama/handlers/collector_spec.rb
308
486
  - spec/ollama/handlers/dump_json_spec.rb
309
487
  - spec/ollama/handlers/dump_yaml_spec.rb
@@ -318,4 +496,6 @@ test_files:
318
496
  - spec/ollama/options_spec.rb
319
497
  - spec/ollama/tool_spec.rb
320
498
  - spec/ollama/utils/ansi_markdown_spec.rb
499
+ - spec/ollama/utils/fetcher_spec.rb
500
+ - spec/ollama/utils/tags_spec.rb
321
501
  - spec/spec_helper.rb