ollama-ruby 0.4.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  RSpec.describe Ollama::Documents::Splitters::Character do
4
4
  let :splitter do
5
- described_class.new chunk_size: 23
5
+ described_class.new chunk_size: 23, combining_string: ''
6
6
  end
7
7
 
8
8
  it 'can be instantiated' do
@@ -10,29 +10,41 @@ RSpec.describe Ollama::Documents::Splitters::Character do
10
10
  end
11
11
 
12
12
  it 'can split' do
13
- text = [ "A" * 10 ] * 10 * "\n\n"
13
+ text = [ ?A * 10 ] * 10 * "\n\n"
14
14
  result = splitter.split(text)
15
15
  expect(result.count).to eq 5
16
16
  expect(result.to_a.join('')).to eq ?A * 100
17
17
  end
18
18
 
19
+ it 'can split combining with separation' do
20
+ splitter = described_class.new chunk_size: 25, include_separator: false,
21
+ combining_string: ?X
22
+ text = [ ?A * 10 ] * 10 * "\n\n"
23
+ result = splitter.split(text)
24
+ expect(result.count).to eq 5
25
+ expect(result.to_a.join(?B)).to eq\
26
+ "AAAAAAAAAAXAAAAAAAAAAXBAAAAAAAAAAAAAAAAAAAAXBAAAAAAAAAAAAAAAAAAAAXB"\
27
+ "AAAAAAAAAAAAAAAAAAAAXBAAAAAAAAAAAAAAAAAAAAX"
28
+ end
29
+
19
30
  it 'can split including separator' do
20
- splitter = described_class.new chunk_size: 25, include_separator: true
21
- text = [ "A" * 10 ] * 10 * "\n\n"
31
+ splitter = described_class.new chunk_size: 25, include_separator: true,
32
+ combining_string: ''
33
+ text = [ ?A * 10 ] * 10 * "\n\n"
22
34
  result = splitter.split(text)
23
35
  expect(result.count).to eq 5
24
36
  expect(result.to_a.join('')).to eq text
25
37
  end
26
38
 
27
39
  it 'cannot split' do
28
- text = [ "A" * 10 ] * 10 * "\n"
40
+ text = [ ?A * 10 ] * 10 * "\n"
29
41
  result = splitter.split(text)
30
42
  expect(result.count).to eq 1
31
43
  expect(result.to_a.join('').count(?A)).to eq text.count(?A)
32
44
  end
33
45
 
34
46
  it 'cannot split2' do
35
- text = "A" * 25
47
+ text = ?A * 25
36
48
  result = splitter.split(text)
37
49
  expect(result.count).to eq 1
38
50
  expect(result.to_a.join('')).to eq ?A * 25
@@ -48,7 +60,7 @@ end
48
60
 
49
61
  RSpec.describe Ollama::Documents::Splitters::RecursiveCharacter do
50
62
  let :splitter do
51
- described_class.new chunk_size: 23
63
+ described_class.new chunk_size: 23, combining_string: ''
52
64
  end
53
65
 
54
66
  it 'can be instantiated' do
@@ -56,7 +68,7 @@ RSpec.describe Ollama::Documents::Splitters::RecursiveCharacter do
56
68
  end
57
69
 
58
70
  it 'can split' do
59
- text = [ "A" * 10 ] * 10 * "\n\n"
71
+ text = [ ?A * 10 ] * 10 * "\n\n"
60
72
  result = splitter.split(text)
61
73
  expect(result.count).to eq 5
62
74
  expect(result.to_a.join('')).to eq ?A * 100
@@ -65,30 +77,32 @@ RSpec.describe Ollama::Documents::Splitters::RecursiveCharacter do
65
77
  it 'cannot split' do
66
78
  splitter = described_class.new chunk_size: 23, include_separator: true,
67
79
  separators: described_class::DEFAULT_SEPARATORS[0..-2]
68
- text = "A" * 25
80
+ text = ?A * 25
69
81
  result = splitter.split(text)
70
82
  expect(result.count).to eq 1
71
83
  expect(result.to_a.join('')).to eq ?A * 25
72
84
  end
73
85
 
74
86
  it 'can split including separator' do
75
- splitter = described_class.new chunk_size: 25, include_separator: true
76
- text = [ "A" * 10 ] * 10 * "\n\n"
87
+ splitter = described_class.new chunk_size: 25, include_separator: true,
88
+ combining_string: ''
89
+ text = [ ?A * 10 ] * 10 * "\n\n"
77
90
  result = splitter.split(text)
78
91
  expect(result.count).to eq 5
79
92
  expect(result.to_a.join('')).to eq text
80
93
  end
81
94
 
82
95
  it 'can split single newline as well' do
83
- text = [ "A" * 10 ] * 10 * "\n"
96
+ text = [ ?A * 10 ] * 10 * "\n"
84
97
  result = splitter.split(text)
85
98
  expect(result.count).to eq 5
86
99
  expect(result.to_a.join('')).to eq ?A * 100
87
100
  end
88
101
 
89
102
  it 'can split single newline as well including separator' do
90
- splitter = described_class.new chunk_size: 25, include_separator: true
91
- text = [ "A" * 10 ] * 10 * "\n"
103
+ splitter = described_class.new chunk_size: 25, include_separator: true,
104
+ combining_string: ''
105
+ text = [ ?A * 10 ] * 10 * "\n"
92
106
  result = splitter.split(text)
93
107
  expect(result.count).to eq 5
94
108
  expect(result.to_a.join('')).to eq text
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Ollama::Utils::CacheFetcher do
4
+ let :url do
5
+ 'https://www.example.com/hello'
6
+ end
7
+
8
+ let :cache do
9
+ double('RedisCache')
10
+ end
11
+
12
+ let :fetcher do
13
+ described_class.new(cache).expose
14
+ end
15
+
16
+ it 'can be instantiated' do
17
+ expect(fetcher).to be_a described_class
18
+ end
19
+
20
+ it 'has #get' do
21
+ expect(cache).to receive(:[]).with('body-69ce405ab83f42dffa9fd22bbd47783f').and_return 'world'
22
+ expect(cache).to receive(:[]).with('content_type-69ce405ab83f42dffa9fd22bbd47783f').and_return 'text/plain'
23
+ yielded_io = nil
24
+ block = -> io { yielded_io = io }
25
+ fetcher.get(url, &block)
26
+ expect(yielded_io).to be_a StringIO
27
+ expect(yielded_io.read).to eq 'world'
28
+ end
29
+
30
+ it '#get needs block' do
31
+ expect { fetcher.get(url) }.to raise_error(ArgumentError)
32
+ end
33
+
34
+ it 'has #put' do
35
+ io = StringIO.new('world')
36
+ io.extend(Ollama::Utils::Fetcher::ContentType)
37
+ io.content_type = MIME::Types['text/plain'].first
38
+ expect(cache).to receive(:[]=).with('body-69ce405ab83f42dffa9fd22bbd47783f', 'world')
39
+ expect(cache).to receive(:[]=).with('content_type-69ce405ab83f42dffa9fd22bbd47783f', 'text/plain')
40
+ fetcher.put(url, io)
41
+ end
42
+ end
@@ -6,7 +6,7 @@ RSpec.describe Ollama::Utils::Fetcher do
6
6
  end
7
7
 
8
8
  let :fetcher do
9
- described_class.new
9
+ described_class.new.expose
10
10
  end
11
11
 
12
12
  it 'can be instantiated' do
@@ -33,6 +33,28 @@ RSpec.describe Ollama::Utils::Fetcher do
33
33
  end
34
34
  end
35
35
 
36
+ it 'can #get without ssl peer verification' do
37
+ fetcher = described_class.new(
38
+ http_options: { ssl_verify_peer: false }
39
+ ).expose
40
+ stub_request(:get, 'https://www.example.com/hello').
41
+ with(headers: fetcher.headers).
42
+ to_return(
43
+ status: 200,
44
+ body: 'world',
45
+ headers: { 'Content-Type' => 'text/plain' },
46
+ )
47
+ expect(Excon).to receive(:new).with(
48
+ 'https://www.example.com/hello',
49
+ hash_including(ssl_verify_peer: false)
50
+ ).and_call_original
51
+ fetcher.get(url) do |tmp|
52
+ expect(tmp).to be_a Tempfile
53
+ expect(tmp.read).to eq 'world'
54
+ expect(tmp.content_type).to eq 'text/plain'
55
+ end
56
+ end
57
+
36
58
  it 'can #get and fallback from streaming' do
37
59
  stub_request(:get, 'https://www.example.com/hello').
38
60
  with(headers: fetcher.headers).
@@ -58,6 +80,7 @@ RSpec.describe Ollama::Utils::Fetcher do
58
80
  fetcher.get(url) do |tmp|
59
81
  expect(tmp).to be_a StringIO
60
82
  expect(tmp.read).to eq ''
83
+ expect(tmp.content_type).to eq 'text/plain'
61
84
  end
62
85
  end
63
86
 
@@ -72,4 +95,21 @@ RSpec.describe Ollama::Utils::Fetcher do
72
95
  expect(file.content_type).to eq 'application/x-ruby'
73
96
  end
74
97
  end
98
+
99
+ it 'can .execute' do
100
+ described_class.execute('echo -n hello world') do |file|
101
+ expect(file).to be_a Tempfile
102
+ expect(file.read).to eq 'hello world'
103
+ expect(file.content_type).to eq 'text/plain'
104
+ end
105
+ end
106
+
107
+ it 'can .execute and fail' do
108
+ allow(IO).to receive(:popen).and_raise StandardError
109
+ described_class.execute('foobar') do |file|
110
+ expect(file).to be_a StringIO
111
+ expect(file.read).to be_empty
112
+ expect(file.content_type).to eq 'text/plain'
113
+ end
114
+ end
75
115
  end
data/spec/spec_helper.rb CHANGED
@@ -5,6 +5,7 @@ if ENV['START_SIMPLECOV'].to_i == 1
5
5
  end
6
6
  end
7
7
  require 'rspec'
8
+ require 'tins/xt/expose'
8
9
  begin
9
10
  require 'debug'
10
11
  rescue LoadError
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.4.0
4
+ version: 0.6.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-09-21 00:00:00.000000000 Z
11
+ date: 2024-09-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.17.1
19
+ version: 1.18.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.17.1
26
+ version: 1.18.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: all_images
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -304,6 +304,48 @@ dependencies:
304
304
  - - "~>"
305
305
  - !ruby/object:Gem::Version
306
306
  version: '2.0'
307
+ - !ruby/object:Gem::Dependency
308
+ name: logger
309
+ requirement: !ruby/object:Gem::Requirement
310
+ requirements:
311
+ - - "~>"
312
+ - !ruby/object:Gem::Version
313
+ version: '1.0'
314
+ type: :runtime
315
+ prerelease: false
316
+ version_requirements: !ruby/object:Gem::Requirement
317
+ requirements:
318
+ - - "~>"
319
+ - !ruby/object:Gem::Version
320
+ version: '1.0'
321
+ - !ruby/object:Gem::Dependency
322
+ name: json
323
+ requirement: !ruby/object:Gem::Requirement
324
+ requirements:
325
+ - - "~>"
326
+ - !ruby/object:Gem::Version
327
+ version: '2.0'
328
+ type: :runtime
329
+ prerelease: false
330
+ version_requirements: !ruby/object:Gem::Requirement
331
+ requirements:
332
+ - - "~>"
333
+ - !ruby/object:Gem::Version
334
+ version: '2.0'
335
+ - !ruby/object:Gem::Dependency
336
+ name: xdg
337
+ requirement: !ruby/object:Gem::Requirement
338
+ requirements:
339
+ - - "~>"
340
+ - !ruby/object:Gem::Version
341
+ version: '7.0'
342
+ type: :runtime
343
+ prerelease: false
344
+ version_requirements: !ruby/object:Gem::Requirement
345
+ requirements:
346
+ - - "~>"
347
+ - !ruby/object:Gem::Version
348
+ version: '7.0'
307
349
  description: Library that allows interacting with the Ollama API
308
350
  email: flori@ping.de
309
351
  executables:
@@ -359,6 +401,7 @@ extra_rdoc_files:
359
401
  - lib/ollama/tool/function/parameters.rb
360
402
  - lib/ollama/tool/function/parameters/property.rb
361
403
  - lib/ollama/utils/ansi_markdown.rb
404
+ - lib/ollama/utils/cache_fetcher.rb
362
405
  - lib/ollama/utils/chooser.rb
363
406
  - lib/ollama/utils/colorize_texts.rb
364
407
  - lib/ollama/utils/fetcher.rb
@@ -425,6 +468,7 @@ files:
425
468
  - lib/ollama/tool/function/parameters.rb
426
469
  - lib/ollama/tool/function/parameters/property.rb
427
470
  - lib/ollama/utils/ansi_markdown.rb
471
+ - lib/ollama/utils/cache_fetcher.rb
428
472
  - lib/ollama/utils/chooser.rb
429
473
  - lib/ollama/utils/colorize_texts.rb
430
474
  - lib/ollama/utils/fetcher.rb
@@ -471,6 +515,7 @@ files:
471
515
  - spec/ollama/options_spec.rb
472
516
  - spec/ollama/tool_spec.rb
473
517
  - spec/ollama/utils/ansi_markdown_spec.rb
518
+ - spec/ollama/utils/cache_fetcher_spec.rb
474
519
  - spec/ollama/utils/fetcher_spec.rb
475
520
  - spec/ollama/utils/file_argument_spec.rb
476
521
  - spec/ollama/utils/tags_spec.rb
@@ -538,6 +583,7 @@ test_files:
538
583
  - spec/ollama/options_spec.rb
539
584
  - spec/ollama/tool_spec.rb
540
585
  - spec/ollama/utils/ansi_markdown_spec.rb
586
+ - spec/ollama/utils/cache_fetcher_spec.rb
541
587
  - spec/ollama/utils/fetcher_spec.rb
542
588
  - spec/ollama/utils/file_argument_spec.rb
543
589
  - spec/ollama/utils/tags_spec.rb