ollama-ruby 0.8.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -38,6 +38,13 @@ class Ollama::Utils::Fetcher
38
38
  end
39
39
  end
40
40
 
41
+ def self.normalize_url(url)
42
+ url = url.to_s
43
+ url = URI.decode_uri_component(url)
44
+ url = url.sub(/#.*/, '')
45
+ URI::Parser.new.escape(url).to_s
46
+ end
47
+
41
48
  def self.read(filename, &block)
42
49
  if File.exist?(filename)
43
50
  File.open(filename) do |file|
@@ -80,6 +87,7 @@ class Ollama::Utils::Fetcher
80
87
  private
81
88
 
82
89
  def excon(url, **options)
90
+ url = self.class.normalize_url(url)
83
91
  Excon.new(url, options.merge(@http_options))
84
92
  end
85
93
 
@@ -1,6 +1,6 @@
1
1
  module Ollama
2
2
  # Ollama version
3
- VERSION = '0.8.0'
3
+ VERSION = '0.9.1'
4
4
  VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
data/ollama-ruby.gemspec CHANGED
@@ -1,14 +1,14 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: ollama-ruby 0.8.0 ruby lib
2
+ # stub: ollama-ruby 0.9.1 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "ollama-ruby".freeze
6
- s.version = "0.8.0".freeze
6
+ s.version = "0.9.1".freeze
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib".freeze]
10
10
  s.authors = ["Florian Frank".freeze]
11
- s.date = "2024-10-06"
11
+ s.date = "2024-10-19"
12
12
  s.description = "Library that allows interacting with the Ollama API".freeze
13
13
  s.email = "flori@ping.de".freeze
14
14
  s.executables = ["ollama_console".freeze, "ollama_chat".freeze, "ollama_update".freeze, "ollama_cli".freeze]
@@ -24,7 +24,7 @@ Gem::Specification.new do |s|
24
24
 
25
25
  s.specification_version = 4
26
26
 
27
- s.add_development_dependency(%q<gem_hadar>.freeze, ["~> 1.18.0".freeze])
27
+ s.add_development_dependency(%q<gem_hadar>.freeze, ["~> 1.19".freeze])
28
28
  s.add_development_dependency(%q<all_images>.freeze, ["~> 0.4".freeze])
29
29
  s.add_development_dependency(%q<rspec>.freeze, ["~> 3.2".freeze])
30
30
  s.add_development_dependency(%q<webmock>.freeze, [">= 0".freeze])
@@ -54,21 +54,21 @@ RSpec.describe Ollama::Client do
54
54
  end
55
55
 
56
56
  it 'can raise error on connection error' do
57
- allow(excon).to receive(:post).and_raise Excon::Error::Socket
57
+ expect(excon).to receive(:post).and_raise Excon::Error::Socket
58
58
  expect {
59
59
  ollama.generate(model: 'llama3.1', prompt: 'Hello World')
60
60
  }.to raise_error(Ollama::Errors::SocketError)
61
61
  end
62
62
 
63
63
  it 'can raise error on timeout' do
64
- allow(excon).to receive(:post).and_raise Excon::Errors::Timeout
64
+ expect(excon).to receive(:post).and_raise Excon::Errors::Timeout
65
65
  expect {
66
66
  ollama.generate(model: 'llama3.1', prompt: 'Hello World')
67
67
  }.to raise_error(Ollama::Errors::TimeoutError)
68
68
  end
69
69
 
70
70
  it 'can raise a generic error' do
71
- allow(excon).to receive(:post).and_raise Excon::Errors::Error
71
+ expect(excon).to receive(:post).and_raise Excon::Errors::Error
72
72
  expect {
73
73
  ollama.generate(model: 'llama3.1', prompt: 'Hello World')
74
74
  }.to raise_error(Ollama::Errors::Error)
@@ -78,7 +78,7 @@ RSpec.describe Ollama::Documents::RedisBackedMemoryCache do
78
78
  end
79
79
 
80
80
  it 'returns size' do
81
- allow(cache).to receive(:count).and_return 3
81
+ expect(cache).to receive(:count).and_return 3
82
82
  expect(cache.size).to eq 3
83
83
  end
84
84
 
@@ -57,7 +57,7 @@ RSpec.describe Ollama::Documents::RedisCache do
57
57
  key, value = 'foo', { test: true }
58
58
  expect(redis).to receive(:set).with('test-' + key, JSON(value), ex: 3_600)
59
59
  cache[key] = value
60
- allow(redis).to receive(:ttl).with('test-' + key).and_return 3_600
60
+ expect(redis).to receive(:ttl).with('test-' + key).and_return 3_600
61
61
  expect(cache.ttl(key)).to eq 3_600
62
62
  end
63
63
 
@@ -42,7 +42,7 @@ RSpec.describe Ollama::Documents do
42
42
  end
43
43
 
44
44
  it 'can find strings' do
45
- allow(ollama).to receive(:embed).
45
+ expect(ollama).to receive(:embed).
46
46
  with(model:, input: [ 'foo' ], options: nil).
47
47
  and_return(double(embeddings: [ [ 0.1 ] ]))
48
48
  expect(documents << 'foo').to eq documents
@@ -57,7 +57,7 @@ RSpec.describe Ollama::Documents do
57
57
  end
58
58
 
59
59
  it 'can find only tagged strings' do
60
- allow(ollama).to receive(:embed).
60
+ expect(ollama).to receive(:embed).
61
61
  with(model:, input: [ 'foo' ], options: nil).
62
62
  and_return(double(embeddings: [ [ 0.1 ] ]))
63
63
  expect(documents.add('foo', tags: %i[ test ])).to eq documents
@@ -77,10 +77,10 @@ RSpec.describe Ollama::Documents do
77
77
  end
78
78
 
79
79
  it 'can find strings conditionally' do
80
- allow(ollama).to receive(:embed).
80
+ expect(ollama).to receive(:embed).
81
81
  with(model:, input: [ 'foobar' ], options: nil).
82
82
  and_return(double(embeddings: [ [ 0.01 ] ]))
83
- allow(ollama).to receive(:embed).
83
+ expect(ollama).to receive(:embed).
84
84
  with(model:, input: [ 'foo' ], options: nil).
85
85
  and_return(double(embeddings: [ [ 0.1 ] ]))
86
86
  expect(documents << 'foobar').to eq documents
@@ -132,7 +132,7 @@ RSpec.describe Ollama::Documents do
132
132
  end
133
133
 
134
134
  it 'can clear texts with tags' do
135
- allow(ollama).to receive(:embed).
135
+ expect(ollama).to receive(:embed).
136
136
  with(model:, input: %w[ bar ], options: nil).
137
137
  and_return(double(embeddings: [ [ 0.1 ] ]))
138
138
  expect(documents.add('foo', tags: %i[ test ])).to eq documents
@@ -25,7 +25,6 @@ RSpec.describe Ollama::Handlers::Markdown do
25
25
  it 'can markdown response as markdown' do
26
26
  output = double('output', :sync= => true)
27
27
  expect(output).to receive(:print).with("\e[2J", "\e[1;1H", ansi)
28
- expect(output).to receive(:puts)
29
28
  markdown = described_class.new(output:)
30
29
  response = double('response', response: md, done: false)
31
30
  markdown.call(response)
@@ -36,7 +35,6 @@ RSpec.describe Ollama::Handlers::Markdown do
36
35
  it 'can markdown message content as markdown' do
37
36
  output = double('output', :sync= => true)
38
37
  expect(output).to receive(:print).with("\e[2J", "\e[1;1H", ansi)
39
- expect(output).to receive(:puts)
40
38
  markdown = described_class.new(output:)
41
39
  response = double('response', response: nil, message: double(content: md), done: false)
42
40
  markdown.call(response)
@@ -19,7 +19,7 @@ RSpec.describe Ollama::Utils::Fetcher do
19
19
  end
20
20
 
21
21
  it 'can #get with streaming' do
22
- stub_request(:get, 'https://www.example.com/hello').
22
+ stub_request(:get, url).
23
23
  with(headers: fetcher.headers).
24
24
  to_return(
25
25
  status: 200,
@@ -37,7 +37,7 @@ RSpec.describe Ollama::Utils::Fetcher do
37
37
  fetcher = described_class.new(
38
38
  http_options: { ssl_verify_peer: false }
39
39
  ).expose
40
- stub_request(:get, 'https://www.example.com/hello').
40
+ stub_request(:get, url).
41
41
  with(headers: fetcher.headers).
42
42
  to_return(
43
43
  status: 200,
@@ -45,7 +45,7 @@ RSpec.describe Ollama::Utils::Fetcher do
45
45
  headers: { 'Content-Type' => 'text/plain' },
46
46
  )
47
47
  expect(Excon).to receive(:new).with(
48
- 'https://www.example.com/hello',
48
+ url,
49
49
  hash_including(ssl_verify_peer: false)
50
50
  ).and_call_original
51
51
  fetcher.get(url) do |tmp|
@@ -56,7 +56,7 @@ RSpec.describe Ollama::Utils::Fetcher do
56
56
  end
57
57
 
58
58
  it 'can #get and fallback from streaming' do
59
- stub_request(:get, 'https://www.example.com/hello').
59
+ stub_request(:get, url).
60
60
  with(headers: fetcher.headers).
61
61
  to_return(
62
62
  { status: 501 },
@@ -74,9 +74,10 @@ RSpec.describe Ollama::Utils::Fetcher do
74
74
  end
75
75
 
76
76
  it 'can #get and finally fail' do
77
- stub_request(:get, 'https://www.example.com/hello').
77
+ stub_request(:get, url).
78
78
  with(headers: fetcher.headers).
79
79
  to_return(status: 500)
80
+ expect(STDERR).to receive(:puts).with(/cannot.*get.*#{url}/i)
80
81
  fetcher.get(url) do |tmp|
81
82
  expect(tmp).to be_a StringIO
82
83
  expect(tmp.read).to eq ''
@@ -105,11 +106,32 @@ RSpec.describe Ollama::Utils::Fetcher do
105
106
  end
106
107
 
107
108
  it 'can .execute and fail' do
108
- allow(IO).to receive(:popen).and_raise StandardError
109
+ expect(IO).to receive(:popen).and_raise StandardError
110
+ expect(STDERR).to receive(:puts).with(/cannot.*execute.*foobar/i)
109
111
  described_class.execute('foobar') do |file|
110
112
  expect(file).to be_a StringIO
111
113
  expect(file.read).to be_empty
112
114
  expect(file.content_type).to eq 'text/plain'
113
115
  end
114
116
  end
117
+
118
+ describe '.normalize_url' do
119
+ it 'can handle umlauts' do
120
+ expect(described_class.normalize_url('https://foo.de/bär')).to eq(
121
+ 'https://foo.de/b%C3%A4r'
122
+ )
123
+ end
124
+
125
+ it 'can handle escaped umlauts' do
126
+ expect(described_class.normalize_url('https://foo.de/b%C3%A4r')).to eq(
127
+ 'https://foo.de/b%C3%A4r'
128
+ )
129
+ end
130
+
131
+ it 'can remove #anchors' do
132
+ expect(described_class.normalize_url('https://foo.de#bar')).to eq(
133
+ 'https://foo.de'
134
+ )
135
+ end
136
+ end
115
137
  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.8.0
4
+ version: 0.9.1
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-10-06 00:00:00.000000000 Z
11
+ date: 2024-10-19 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.18.0
19
+ version: '1.19'
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.18.0
26
+ version: '1.19'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: all_images
29
29
  requirement: !ruby/object:Gem::Requirement