ollama-ruby 1.18.0 → 1.19.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8b59a75052a3d4f15cc99f14a6ffa1172140f7bbd4ef474e20d986843ba02247
4
- data.tar.gz: ed28da23aa8242a86d6b4c3c1aa1d9b10fabb683a074dc451e49a0e7cdafd8bd
3
+ metadata.gz: 70a9d81fbfa0c1cb68774f4f9dd7f521e9dba151cd2c090c4134bc5da6e73bc9
4
+ data.tar.gz: a60220e7b05cf0f76257a438cb240da9584f11e69c8c4bc1e56eef1798253692
5
5
  SHA512:
6
- metadata.gz: c9085a100ed588c9846c4497227812fd6ee62a7ec2023f07c124dd49ddb5418ae0d0634936e44ea764e24a7f18b7c1b7799928daf1dcfbf030e11759b44409eb
7
- data.tar.gz: 8a222ef6114c2e22c7ae6b8ad1d65c5ad638318088aa31580f5de45e2c4a63700fb124b6e59d0a39824a3757a4c909507f8c51c85786be0d6c2ff35c1010c585
6
+ metadata.gz: a08c1a30ea4fdcd62e3d53a2d72cafa63ac19403fe117deb24414eb74ca981f31e06e73eef9747d8e8e6499f8fe04f4f49f12a4d3214ee64f05ff9aec0c17ff5
7
+ data.tar.gz: 71b8eea53e6a7679cdfc321535d1bf8a2ef59117972cd6c1574d4f3067233427edea0730506b14ec8b945d5981da0e60f1183f504568dc647bbb525830481154
data/CHANGES.md CHANGED
@@ -1,12 +1,50 @@
1
1
  # Changes
2
2
 
3
+ ## 2026-03-04 v1.19.1
4
+
5
+ - Fix ps command to fetch models inside loop
6
+
7
+ ## 2026-03-04 v1.19.0
8
+
9
+ - Added API key handling to the Ollama client: introduced a private `api_key`
10
+ attribute, interpolated it into request headers as `"Bearer #@api_key"`, and
11
+ merged the field into the client configuration. Updated `bin/ollama_cli` and
12
+ `bin/ollama_console` to accept an optional `api_key` from environment
13
+ variables, and extended `spec/ollama/client_spec.rb` to test this
14
+ functionality.
15
+ - Implemented an interval option for `ollama_ps`: added `require
16
+ 'term/ansicolor'`, updated help text to include the flag `-i INTERVAL`,
17
+ parsed the `?i` option, and added a loop that clears the terminal with
18
+ `Term::ANSIColor.clear_screen`, prints the model table via
19
+ `ps_table(models)`, sleeps for the specified interval, and repeats while the
20
+ interval is greater than zero.
21
+ - Improved progress output formatting: updated
22
+ `lib/ollama/handlers/progress.rb` to format progress bytes using
23
+ `Tins::Unit.format(_1, format: '%.2f %U', unit: ?B, prefix: :iec_uc)` and
24
+ adjusted the `message` method to include `unit` and `prefix` options.
25
+ - Updated dependencies and test harness: removed system‑wide gem updates from
26
+ `.all_images.yml`, replaced `bundle update --all` with `bundle exec rake
27
+ spec`, added `before:` and `after:` hooks to delete `Gemfile.lock` and echo
28
+ test results, bumped the `all_images` dev dependency to `~> **0.12**`,
29
+ updated the `gem_hadar` dev dependency to `>= **2.17.0**`, and set
30
+ `rubygems_version` to **4.0.3** in `ollama-ruby.gemspec`.
31
+ - Added changelog configuration to the Rakefile: created a `changelog` block
32
+ with `filename` set to `'CHANGES.md'` to enable automated changelog
33
+ generation.
34
+ - Updated Ruby image build matrix: enabled the `ruby:4.0-rc-alpine` image in
35
+ the build matrix with the `*script` template, and later switched the Ruby
36
+ version from **4.0-rc-alpine** to **4.0-alpine**.
37
+ - Updated the `gem_hadar` development dependency to **2.16.3** or higher to
38
+ maintain compatibility with newer features and bug fixes.
39
+
3
40
  ## 2025-12-19 v1.18.0
4
41
 
5
42
  - Updated `gem_hadar` development dependency from version **2.8** to **2.9**
6
43
 
7
44
  ## 2025-12-19 v1.17.0
8
45
 
9
- - Changed `s.required_ruby_version` in the gemspec from "~> 3.1" to ">= 3.1" to allow usage with **Ruby 3.1** and higher, including **4.0**
46
+ - Changed `s.required_ruby_version` in the gemspec from "~> 3.1" to ">= 3.1" to
47
+ allow usage with **Ruby 3.1** and higher, including **4.0**
10
48
  - Updated `s.rubygems_version` from **3.7.2** to **4.0.2**
11
49
  - Replaced `bundle update` with `bundle update --all` in the update command
12
50
  - Added **4.0-rc** release candidate to the Ruby version matrix (commented out)
@@ -72,10 +110,13 @@
72
110
  ## 2025-11-30 v1.12.0
73
111
 
74
112
  - `ollama_cli`
75
- - Added `-i` flag to display Ollama server version using `ollama.version.version`
76
- - Refactored model options handling to use `Ollama::Options.from_hash` or `Ollama::Options.new`
113
+ - Added `-i` flag to display Ollama server version using
114
+ `ollama.version.version`
115
+ - Refactored model options handling to use `Ollama::Options.from_hash` or
116
+ `Ollama::Options.new`
77
117
  - Stored client configuration in `ollama` variable for reuse
78
- - Added `-d` flag for debug mode in `ollama_cli` instead of using environment variable
118
+ - Added `-d` flag for debug mode in `ollama_cli` instead of using
119
+ environment variable
79
120
  - Included `Ollama::DTO` in `Client::Config` class for consistent behavior
80
121
  - Improved documentation formatting in `dto.rb` file
81
122
  - Added documentation that `think` can be "high", "medium", "low" instead of just `true`
@@ -502,7 +543,7 @@ and introduced new context files for project structure documentation in
502
543
  + Normalizes URL by decoding URI components, removing anchors, and escaping special characters
503
544
  + `excon` method now calls `normalize_url` on the provided URL
504
545
  + Added specs for `normalize_url` in `fetcher_spec.rb`
505
- * Remove Ollama top level Namespace b/c we include it from `ollama_chat`:
546
+ * Remove Ollama top level Namespace b/c we include it from `ollama_chat`:
506
547
  + Removed the top-level namespace
507
548
 
508
549
  ## 2024-10-18 v0.9.0
@@ -554,7 +595,7 @@ and introduced new context files for project structure documentation in
554
595
  + Moved case order
555
596
  + Renamed `/collection clear [tag]|change` to `/collection (clear|change)`
556
597
  + Improved help message, added /info
557
- * **Update README.md**
598
+ * **Update README.md**
558
599
  + Update README.md to reflect changed/added commands
559
600
  * **Add support for reading PostScript**
560
601
  + Extracted `pdf_read` method to read PDF files using `PDF::Reader`
@@ -572,7 +613,7 @@ and introduced new context files for project structure documentation in
572
613
  + Updated `choose` method to handle cases better where no entry was chosen
573
614
  * **Fix Redis cache expiration logic**
574
615
  + Update `set` method to delete key expiration time is less than 1 second.
575
- * **Update dependencies and add source tracking**
616
+ * **Update dependencies and add source tracking**
576
617
  - Remove `sorted_set` dependency from Rakefile
577
618
  - Modify `Ollama::Documents` class to track source of tags
578
619
  - Update `Ollama::Utils::Tags` class to include source in tag output and add methods for tracking source
@@ -787,28 +828,28 @@ and introduced new context files for project structure documentation in
787
828
 
788
829
  * Update dependencies and date in gemspec files:
789
830
  - Updated `complex_config` dependency to '~> 0.22'
790
- * Refactor FollowChat#eval_stats to add bold eval rates
791
- * Improve formatting in eval_stats using bold and color for better
831
+ * Refactor `FollowChat#eval_stats` to add bold eval rates
832
+ * Improve formatting in `eval_stats` using bold and color for better
792
833
  readability.
793
- * Update import_document and add_image methods to handle nil values
834
+ * Update `import_document` and `add_image` methods to handle nil values
794
835
  correctly.
795
836
  * Update width method in utils/width.rb to use uncolor when checking line
796
837
  length.
797
838
  * Refactor eval stats output in FollowChat class
798
839
  - Add indentation to eval stats output for better readability
799
840
  * FollowChat evaluation stats refactored
800
- - Removed hardcoded eval_stats hash and replaced with method call
841
+ - Removed hardcoded `eval_stats` hash and replaced with method call
801
842
  `eval_stats(response)`
802
843
  - Added new method `eval_stats(response)` to calculate evaluation statistics
803
844
  - Calculates eval duration, prompt eval duration, total duration, and load
804
845
  duration
805
846
  - Adds eval count, prompt eval count, eval rate, and prompt eval rate
806
- * Use default to_s tree representation of config.
807
- * Update complex_config dependency to ~> 0.21, >= 0.21.1 in Rakefile
808
- * Update complex_config dependency to ~> 0.21, >= 0.21.1 in
847
+ * Use default `to_s` tree representation of config.
848
+ * Update `complex_config` dependency to ~> 0.21, >= 0.21.1 in Rakefile
849
+ * Update `complex_config` dependency to ~> 0.21, >= 0.21.1 in
809
850
  ollama-ruby.gemspec
810
851
  * Update dependencies and configuration display
811
- * Update 'complex_config' dependency to '~> 0.21'
852
+ * Update `complex_config` dependency to '~> 0.21'
812
853
  * Change OllamaChatConfig to display configuration as a tree instead of yaml
813
854
  * Improve /web search command
814
855
  * Update infobar dependency to ~> 0.8
@@ -858,9 +899,10 @@ and introduced new context files for project structure documentation in
858
899
  + Updated `/summarize` command to handle cases where summarization fails
859
900
  + Fix bug in parsing content type of source document
860
901
  * **Refactored Options Class and Usage**
861
- + Renamed `options` variable to use `Options[]` method in ollama_chat script
902
+ + Renamed `options` variable to use `Options[]` method in `ollama_chat`
903
+ script
862
904
  + Added `[](value)` method to Ollama::Options class for casting hashes
863
- + Updated options_spec.rb with tests for casting hashes and error handling
905
+ + Updated `options_spec.rb` with tests for casting hashes and error handling
864
906
  * **Refactored Web Search Command**
865
907
  + Added support for specifying a page number in `/web` command
866
908
  + Updated regular expression to match new format
@@ -870,10 +912,11 @@ and introduced new context files for project structure documentation in
870
912
  + Renamed `json_create` method to `from_hash` in Ollama::DTO class
871
913
  + Updated `as_json` method to remove now unnecessary hash creation
872
914
  * **Message and Tool Spec Changes**
873
- + Removed `json_class` from JSON serialization in message_spec
874
- + Removed `json_class` from JSON serialization in tool_spec
915
+ + Removed `json_class` from JSON serialization in `message_spec`
916
+ + Removed `json_class` from JSON serialization in `tool_spec`
875
917
  * **Command Spec Changes**
876
- + Removed `json_class` from JSON serialization in various command specs (e.g. generate_spec, pull_spec, etc.)
918
+ + Removed `json_class` from JSON serialization in various command specs (e.g.
919
+ `generate_spec`, `pull_spec`, etc.)
877
920
  * **Miscellaneous Changes**
878
921
  + Improved width calculation for text truncation
879
922
  + Updated FollowChat class to display evaluation statistics
@@ -886,26 +929,36 @@ and introduced new context files for project structure documentation in
886
929
 
887
930
  #### Significant Changes
888
931
 
889
- * **Document Splitting and Embedding Functionality**: Added `Ollama::Documents` class with methods for adding documents, checking existence, deleting documents, and finding similar documents.
932
+ * **Document Splitting and Embedding Functionality**: Added `Ollama::Documents`
933
+ class with methods for adding documents, checking existence, deleting
934
+ documents, and finding similar documents.
890
935
  + Introduced two types of caches: `MemoryCache` and `RedisCache`
891
- + Implemented `SemanticSplitter` class to split text into sentences based on semantic similarity
892
- * **Improved Ollama Chat Client**: Added support for document embeddings and web/file RAG
936
+ + Implemented `SemanticSplitter` class to split text into sentences based on
937
+ semantic similarity
938
+ * **Improved Ollama Chat Client**: Added support for document embeddings and
939
+ web/file RAG
893
940
  + Allowed configuration per yaml file
894
941
  + Parse user input for URLs or files to send images to multimodal models
895
- * **Redis Docker Service**: Set `REDIS_URL` environment variable to `redis://localhost:9736`
942
+ * **Redis Docker Service**: Set `REDIS_URL` environment variable to
943
+ `redis://localhost:9736`
896
944
  + Added Redis service to `docker-compose.yml`
897
- * **Status Display and Progress Updates**: Added infobar.label = response.status when available
898
- + Updated infobar with progress message on each call if total and completed are set
945
+ * **Status Display and Progress Updates**: Added infobar.label =
946
+ response.status when available
947
+ + Updated infobar with progress message on each call if total and completed
948
+ are set
899
949
  + Display error message from response.error if present
900
- * **Refactored Chat Commands**: Simplified regular expression patterns for `/pop`, `/save`, `/load`, and `/image` commands
950
+ * **Refactored Chat Commands**: Simplified regular expression patterns for
951
+ `/pop`, `/save`, `/load`, and `/image` commands
901
952
  + Added whitespace to some command patterns for better readability
902
953
 
903
954
  #### Other Changes
904
955
 
905
- * Added `Character` and `RecursiveCharacter` splitter classes to split text into chunks based on character separators
956
+ * Added `Character` and `RecursiveCharacter` splitter classes to split text
957
+ into chunks based on character separators
906
958
  * Added RSpec tests for the Ollama::Documents class(es)
907
- * Updated dependencies and added new methods for calculating breakpoint thresholds and sentence embeddings
908
- * Added 'ollama_update' to executables in Rakefile
959
+ * Updated dependencies and added new methods for calculating breakpoint
960
+ thresholds and sentence embeddings
961
+ * Added `ollama_update` to executables in Rakefile
909
962
  * Started using webmock
910
963
  * Refactored chooser and add fetcher specs
911
964
  * Added tests for Ollama::Utils::Fetcher
data/Rakefile CHANGED
@@ -21,6 +21,10 @@ GemHadar do
21
21
  executables << 'ollama_console' << 'ollama_update' << 'ollama_cli' <<
22
22
  'ollama_browse' << 'ollama_ps'
23
23
 
24
+ changelog do
25
+ filename 'CHANGES.md'
26
+ end
27
+
24
28
  github_workflows(
25
29
  'static.yml' => {}
26
30
  )
@@ -36,7 +40,7 @@ GemHadar do
36
40
  dependency 'ostruct', '~> 0.0'
37
41
  dependency 'terminal-table', '~> 3.0'
38
42
  dependency 'nokogiri', '~> 1.0'
39
- development_dependency 'all_images', '~> 0.6'
43
+ development_dependency 'all_images', '~> 0.12'
40
44
  development_dependency 'rspec', '~> 3.2'
41
45
  development_dependency 'kramdown', '~> 2.0'
42
46
  development_dependency 'webmock'
data/bin/ollama_cli CHANGED
@@ -44,7 +44,7 @@ module Ollama::Handlers
44
44
  # specified output.
45
45
  #
46
46
  # @param output [IO] the output stream to write markdown content to,
47
- # defaults to $stdout
47
+ # defaults to $stdout
48
48
  def initialize(output: $stdout)
49
49
  super
50
50
  @output.sync = true
@@ -137,9 +137,10 @@ opts = go 'u:m:M:s:p:P:H:c:I:STdih', defaults: { ?H => 'ChatStart' }
137
137
 
138
138
  opts[?h] and usage
139
139
 
140
- base_url = opts[?u] || ENV['OLLAMA_URL'] || 'http://%s' % ENV.fetch('OLLAMA_HOST')
140
+ base_url = opts[?u] || ENV['OLLAMA_URL'] || 'http://%s' % ENV.fetch('OLLAMA_HOST')
141
+ api_key = ENV['OLLAMA_API_KEY']
141
142
  client_config = Client::Config[
142
- { base_url:, debug: !!opts[?d] } |
143
+ { base_url:, api_key:, debug: !!opts[?d] } |
143
144
  JSON(get_file_argument(opts[?c], default: ENV['OLLAMA_CLIENT']).full? || '{}')
144
145
  ]
145
146
  model = (opts[?m] || ENV['OLLAMA_MODEL']).full? || 'llama3.1'
data/bin/ollama_console CHANGED
@@ -40,7 +40,17 @@ end
40
40
  #
41
41
  # @return [ Hash ] a hash containing the merged configuration settings
42
42
  def client_config
43
- Client::Config[ { base_url: } | JSON(ENV.fetch('OLLAMA_CLIENT', '{}')) ]
43
+ Client::Config[
44
+ { base_url:, api_key: } | JSON(ENV.fetch('OLLAMA_CLIENT', '{}')).transform_keys(&:to_sym)
45
+ ]
46
+ end
47
+
48
+ # Retrieves the Ollama API key from the environment variable OLLAMA_API_KEY.
49
+ #
50
+ # @return [ String, nil ] the API key value or nil if the environment variable
51
+ # is not set
52
+ def api_key
53
+ ENV['OLLAMA_API_KEY']
44
54
  end
45
55
 
46
56
  # The ollama method provides access to a configured Ollama client instance.
data/bin/ollama_ps CHANGED
@@ -34,6 +34,7 @@
34
34
  require 'ollama'
35
35
  include Ollama
36
36
  require 'terminal-table'
37
+ require 'term/ansicolor'
37
38
  require 'tins'
38
39
  include Tins::GO
39
40
 
@@ -49,6 +50,7 @@ def usage
49
50
  Usage: #{File.basename($0)} [OPTIONS]
50
51
 
51
52
  -f FORMAT output format: json, yaml, or table (default)
53
+ -i INTERVAL output the ps table every INTERVAL seconds
52
54
  -h this help
53
55
 
54
56
  EOT
@@ -188,17 +190,25 @@ def ps_table(models)
188
190
  puts table
189
191
  end
190
192
 
191
- opts = go 'f:h'
193
+ opts = go 'f:i:h'
192
194
 
193
195
  opts[?h] and usage
194
196
 
195
- models = fetch_ps_models or exit
196
-
197
197
  case opts[?f]
198
198
  when 'json'
199
+ models = fetch_ps_models or exit
199
200
  puts JSON.pretty_generate(interpret_models(models))
200
201
  when 'yaml'
202
+ models = fetch_ps_models or exit
201
203
  YAML.dump(interpret_models(models), STDOUT)
202
204
  else
203
- ps_table(interpret_models(models))
205
+ n = opts[?i].to_i
206
+ begin
207
+ print Term::ANSIColor.clear_screen, Term::ANSIColor.move_home if n > 0
208
+ if models = fetch_ps_models.full?
209
+ ps_table(interpret_models(models))
210
+ end
211
+ n == 0 and break
212
+ sleep n
213
+ end while n
204
214
  end
@@ -27,7 +27,7 @@ module Ollama::Client::Command
27
27
  # @param stream_handler [ Class, nil ] the handler class to use for streaming responses, if applicable
28
28
  #
29
29
  # @note Create Command `name`, if `stream` was true, set `stream_handler`
30
- # as default, otherwise `default_handler`.
30
+ # as default, otherwise `default_handler`.
31
31
  #
32
32
  # @return [ self ] returns the receiver after defining the command method
33
33
  def command(name, default_handler:, stream_handler: nil)
@@ -66,7 +66,7 @@ module Ollama::Client::Configuration
66
66
  # @param value [ Hash ] a hash containing the attribute names and their values
67
67
  #
68
68
  # @return [ self ] a new instance of the class initialized with the provided
69
- # attributes
69
+ # attributes
70
70
  def self.[](value)
71
71
  new(**value.to_h)
72
72
  end
@@ -77,6 +77,12 @@ module Ollama::Client::Configuration
77
77
  # @attr [ URI ] the new base URL to be set for API requests
78
78
  attr_accessor :base_url
79
79
 
80
+ # The api_key attribute allows setting and retrieving the API key for
81
+ # Ollama API authentication.
82
+ #
83
+ # @attr [ String, nil ] the API key used for authenticating requests
84
+ attr_accessor :api_key
85
+
80
86
  # The output attribute accessor allows reading and setting the output stream
81
87
  # used for handling responses and messages.
82
88
  #
@@ -126,13 +132,14 @@ module Ollama::Client::Configuration
126
132
  # settings.
127
133
 
128
134
  # @param config [ Ollama::Client::Configuration::Config ] the configuration
129
- # object containing client settings
135
+ # object containing client settings
130
136
  #
131
137
  # @return [ Ollama::Client ] a new client instance configured with the
132
- # provided settings
138
+ # provided settings
133
139
  def configure_with(config)
134
140
  new(
135
141
  base_url: config.base_url,
142
+ api_key: config.api_key,
136
143
  output: config.output,
137
144
  connect_timeout: config.connect_timeout,
138
145
  read_timeout: config.read_timeout,
data/lib/ollama/client.rb CHANGED
@@ -35,13 +35,14 @@ class Ollama::Client
35
35
  # configuration options, making them available for use in subsequent client operations.
36
36
  #
37
37
  # @param base_url [ String, nil ] the base URL of the Ollama API endpoint, defaults to nil
38
+ # @param api_key [ String, nil ] the API key for the Ollama API, defaults to nil
38
39
  # @param output [ IO ] the output stream to be used for handling responses, defaults to $stdout
39
40
  # @param connect_timeout [ Integer, nil ] the connection timeout value in seconds, defaults to nil
40
41
  # @param read_timeout [ Integer, nil ] the read timeout value in seconds, defaults to nil
41
42
  # @param write_timeout [ Integer, nil ] the write timeout value in seconds, defaults to nil
42
43
  # @param debug [ Boolean, nil ] the debug flag indicating whether debug output is enabled, defaults to nil
43
44
  # @param user_agent [ String, nil ] the user agent string to be used for API requests, defaults to nil
44
- def initialize(base_url: nil, output: $stdout, connect_timeout: nil, read_timeout: nil, write_timeout: nil, debug: nil, user_agent: nil)
45
+ def initialize(base_url: nil, api_key: nil, output: $stdout, connect_timeout: nil, read_timeout: nil, write_timeout: nil, debug: nil, user_agent: nil)
45
46
  base_url.nil? and base_url = ENV.fetch('OLLAMA_URL') do
46
47
  raise ArgumentError,
47
48
  'missing :base_url parameter or OLLAMA_URL environment variable'
@@ -52,8 +53,8 @@ class Ollama::Client
52
53
  @ssl_verify_peer = base_url.query.to_s.split(?&).inject({}) { |h, l|
53
54
  h.merge Hash[*l.split(?=)]
54
55
  }['ssl_verify_peer'] != 'false'
55
- @base_url, @output, @connect_timeout, @read_timeout, @write_timeout, @debug, @user_agent =
56
- base_url, output, connect_timeout, read_timeout, write_timeout, debug, user_agent
56
+ @base_url, @api_key, @output, @connect_timeout, @read_timeout, @write_timeout, @debug, @user_agent =
57
+ base_url, api_key, output, connect_timeout, read_timeout, write_timeout, debug, user_agent
57
58
  end
58
59
 
59
60
  # The output attribute accessor allows reading and setting the output stream
@@ -68,6 +69,13 @@ class Ollama::Client
68
69
  # @return [ URI ] the base URL configured for API requests
69
70
  attr_reader :base_url
70
71
 
72
+ # The api_key attribute reader returns the API key used for authenticating
73
+ # requests.
74
+ #
75
+ # @attr_reader :api_key
76
+ attr_reader :api_key
77
+ private :api_key
78
+
71
79
  # The ssl_verify_peer? method checks whether SSL peer verification is enabled.
72
80
  #
73
81
  # This method returns a boolean value indicating if the client should verify
@@ -217,7 +225,7 @@ class Ollama::Client
217
225
  {
218
226
  'User-Agent' => @user_agent || self.class.user_agent,
219
227
  'Content-Type' => 'application/json; charset=utf-8',
220
- }
228
+ } | @api_key.full? { { 'Authorization' => "Bearer #@api_key" } }
221
229
  end
222
230
 
223
231
  # The user_agent method generates a formatted user agent string for API requests.
@@ -107,7 +107,7 @@ class Ollama::Commands::Chat
107
107
  # scenarios based on the command's configuration.
108
108
  #
109
109
  # @param handler [ Ollama::Handler ] the handler object responsible for processing API
110
- # responses
110
+ # responses
111
111
  #
112
112
  # @return [ self ] returns the current instance after initiating the request
113
113
  def perform(handler)
@@ -68,7 +68,7 @@ class Ollama::Commands::Copy
68
68
  # copy commands do not support streaming.
69
69
  #
70
70
  # @param handler [ Ollama::Handler ] the handler object responsible for processing API
71
- # responses
71
+ # responses
72
72
  #
73
73
  # @return [ self ] returns the current instance after initiating the request
74
74
  def perform(handler)
@@ -123,7 +123,7 @@ class Ollama::Commands::Create
123
123
  # scenarios based on the command's configuration.
124
124
  #
125
125
  # @param handler [ Ollama::Handler ] the handler object responsible for processing API
126
- # responses
126
+ # responses
127
127
  #
128
128
  # @return [ self ] returns the current instance after initiating the request
129
129
  def perform(handler)
@@ -62,7 +62,7 @@ class Ollama::Commands::Delete
62
62
  # delete commands do not support streaming.
63
63
  #
64
64
  # @param handler [ Ollama::Handler ] the handler object responsible for processing API
65
- # responses
65
+ # responses
66
66
  #
67
67
  # @return [ self ] returns the current instance after initiating the request
68
68
  def perform(handler)
@@ -99,7 +99,7 @@ class Ollama::Commands::Embed
99
99
  # embedding commands do not support streaming.
100
100
  #
101
101
  # @param handler [ Ollama::Handler ] the handler object responsible for processing API
102
- # responses
102
+ # responses
103
103
  #
104
104
  # @return [ self ] returns the current instance after initiating the request
105
105
  def perform(handler)
@@ -82,7 +82,7 @@ class Ollama::Commands::Embeddings
82
82
  # embeddings commands do not support streaming.
83
83
  #
84
84
  # @param handler [ Ollama::Handler ] the handler object responsible for processing API
85
- # responses
85
+ # responses
86
86
  #
87
87
  # @return [ self ] returns the current instance after initiating the request
88
88
  def perform(handler)
@@ -65,7 +65,7 @@ class Ollama::Commands::Ps
65
65
  # ps commands do not support streaming.
66
66
  #
67
67
  # @param handler [ Ollama::Handler ] the handler object responsible for processing API
68
- # responses
68
+ # responses
69
69
  #
70
70
  # @return [ self ] returns the current instance after initiating the request
71
71
  def perform(handler)
@@ -10,7 +10,7 @@
10
10
  # @param stream_handler [ Class, nil ] the handler class to use for streaming responses, if applicable
11
11
  #
12
12
  # @note Create Command `name`, if `stream` was true, set `stream_handler`
13
- # as default, otherwise `default_handler`.
13
+ # as default, otherwise `default_handler`.
14
14
  #
15
15
  # @return [ self ] returns the receiver after defining the command method
16
16
  class Ollama::Commands::Pull
@@ -81,7 +81,7 @@ class Ollama::Commands::Pull
81
81
  # non-streaming scenarios based on the command's configuration.
82
82
  #
83
83
  # @param handler [ Ollama::Handler ] the handler object responsible for processing API
84
- # responses
84
+ # responses
85
85
  #
86
86
  # @return [ self ] returns the current instance after initiating the request
87
87
  def perform(handler)
@@ -40,7 +40,7 @@ class Ollama::Commands::Tags
40
40
  # associated with the object.
41
41
  #
42
42
  # @return [ TrueClass, FalseClass ] the streaming behavior flag, indicating
43
- # whether streaming is enabled for the command execution
43
+ # whether streaming is enabled for the command execution
44
44
  attr_reader :stream
45
45
 
46
46
  # The client attribute writer allows setting the client instance associated
@@ -18,7 +18,7 @@ class Ollama::Commands::Version
18
18
  # operations.
19
19
  #
20
20
  # @return [ String ] the API endpoint path '/api/version' for version
21
- # requests
21
+ # requests
22
22
  def self.path
23
23
  '/api/version'
24
24
  end
@@ -37,7 +37,7 @@ class Ollama::Commands::Version
37
37
  # associated with the object.
38
38
  #
39
39
  # @return [ TrueClass, FalseClass ] the streaming behavior flag, indicating
40
- # whether streaming is enabled for the command execution
40
+ # whether streaming is enabled for the command execution
41
41
  attr_reader :stream
42
42
 
43
43
  # The client attribute writer allows setting the client instance associated
@@ -15,7 +15,7 @@ class Ollama::Handlers::Collector
15
15
  # output destination and initializes an empty array for collecting responses.
16
16
  #
17
17
  # @param output [ IO ] the output stream to be used for handling responses,
18
- # defaults to $stdout
18
+ # defaults to $stdout
19
19
  def initialize(output: $stdout)
20
20
  super
21
21
  @array = []
@@ -57,7 +57,7 @@ module Ollama::Handlers::Concern
57
57
  # in contexts where a proc is expected.
58
58
  #
59
59
  # @return [ Proc ] a proc that wraps the handler's call method for response
60
- # processing
60
+ # processing
61
61
  def to_proc
62
62
  -> response { call(response) }
63
63
  end
@@ -67,10 +67,10 @@ module Ollama::Handlers::Concern
67
67
  # response.
68
68
  #
69
69
  # @param response [ Ollama::Response ] the response object to be processed by the
70
- # handler
70
+ # handler
71
71
  #
72
72
  # @return [ self ] returns the handler instance itself after processing the
73
- # response
73
+ # response
74
74
  def call(response)
75
75
  new.call(response)
76
76
  end
@@ -19,10 +19,10 @@ class Ollama::Handlers::DumpYAML
19
19
  # YAML format for debugging or inspection purposes.
20
20
  #
21
21
  # @param response [ Ollama::Response ] the response object to be serialized
22
- # and output
22
+ # and output
23
23
  #
24
24
  # @return [ self ] returns the handler instance itself after processing the
25
- # response
25
+ # response
26
26
  def call(response)
27
27
  @output.puts Psych.dump(response)
28
28
  self
@@ -36,10 +36,10 @@ class Ollama::Handlers::Markdown
36
36
  # single-message display.
37
37
  #
38
38
  # @param response [ Ollama::Response ] the response object containing content
39
- # to be rendered
39
+ # to be rendered
40
40
  #
41
41
  # @return [ self ] returns the handler instance itself after processing the
42
- # response
42
+ # response
43
43
  def call(response)
44
44
  if content = response.response || response.message&.content
45
45
  if @stream
@@ -35,10 +35,10 @@ class Ollama::Handlers::Progress
35
35
  # ensures proper formatting of output.
36
36
  #
37
37
  # @param response [ Ollama::Response ] the response object containing
38
- # progress information
38
+ # progress information
39
39
  #
40
40
  # @return [ self ] returns the handler instance itself after processing the
41
- # response
41
+ # response
42
42
  def call(response)
43
43
  infobar.display.output = @output
44
44
  if status = response.status
@@ -80,7 +80,7 @@ class Ollama::Handlers::Progress
80
80
  # time information, and estimated completion details
81
81
  def message(current, total)
82
82
  progress = '%s/%s' % [ current, total ].map {
83
- Tins::Unit.format(_1, format: '%.2f %U')
83
+ Tins::Unit.format(_1, format: '%.2f %U', unit: ?B, prefix: :iec_uc)
84
84
  }
85
85
  '%l ' + progress + ' in %te, ETA %e @%E'
86
86
  end
@@ -23,7 +23,7 @@ class Ollama::Handlers::Say
23
23
  # @param output [ IO, nil ] the output stream to be used for handling responses, defaults to nil
24
24
  # @param voice [ String ] the voice to be used for speech synthesis, defaults to 'Samantha'
25
25
  # @param interactive [ TrueClass, FalseClass, String, nil ] the interactive
26
- # mode setting for speech synthesis, defaults to nil
26
+ # mode setting for speech synthesis, defaults to nil
27
27
  def initialize(output: nil, voice: 'Samantha', interactive: nil)
28
28
  @voice = voice
29
29
  @interactive = interactive
@@ -43,7 +43,7 @@ class Ollama::Handlers::Say
43
43
  # associated with the object.
44
44
  #
45
45
  # @return [ TrueClass, FalseClass, String, nil ] the interactive mode value
46
- # stored in the instance variable
46
+ # stored in the instance variable
47
47
  attr_reader :interactive
48
48
 
49
49
  # The call method processes a response by printing its content to the output stream.
@@ -107,7 +107,7 @@ class Ollama::Handlers::Say
107
107
  #
108
108
  # @param voice [ String ] the voice to be used for speech synthesis
109
109
  # @param interactive [ TrueClass, FalseClass, String, nil ] the interactive
110
- # mode setting for speech synthesis
110
+ # mode setting for speech synthesis
111
111
  #
112
112
  # @return [ Array<String> ] an array containing the command and its arguments
113
113
  def command(voice:, interactive:)
@@ -15,7 +15,7 @@ class Ollama::Handlers::Single
15
15
  # output destination and initializes an empty array for collecting responses.
16
16
  #
17
17
  # @param output [ IO ] the output stream to be used for handling responses,
18
- # defaults to $stdout
18
+ # defaults to $stdout
19
19
  def initialize(output: $stdout)
20
20
  super
21
21
  @array = []
data/lib/ollama/image.rb CHANGED
@@ -44,7 +44,7 @@ class Ollama::Image
44
44
  # @param path [ String, nil ] the optional file path associated with the image
45
45
  #
46
46
  # @return [ Ollama::Image ] a new Image instance initialized with the
47
- # provided data and path
47
+ # provided data and path
48
48
  def for_base64(data, path: nil)
49
49
  obj = new(data)
50
50
  obj.path = path
@@ -57,7 +57,7 @@ class Ollama::Image
57
57
  # @param path [ String, nil ] the optional file path associated with the image
58
58
  #
59
59
  # @return [ Ollama::Image ] a new Image instance initialized with the
60
- # encoded string data and optional path
60
+ # encoded string data and optional path
61
61
  def for_string(string, path: nil)
62
62
  for_base64(Base64.strict_encode64(string), path:)
63
63
  end
@@ -69,7 +69,7 @@ class Ollama::Image
69
69
  # @param path [ String, nil ] the optional file path associated with the image
70
70
  #
71
71
  # @return [ Ollama::Image ] a new Image instance initialized with the IO
72
- # object's data and optional path
72
+ # object's data and optional path
73
73
  def for_io(io, path: nil)
74
74
  path ||= io.path
75
75
  for_string(io.read, path:)
@@ -81,7 +81,7 @@ class Ollama::Image
81
81
  # @param path [ String ] the file system path to the image file
82
82
  #
83
83
  # @return [ Ollama::Image ] a new Image instance initialized with the
84
- # contents of the file at the specified path
84
+ # contents of the file at the specified path
85
85
  def for_filename(path)
86
86
  File.open(path, 'rb') { |io| for_io(io, path:) }
87
87
  end
@@ -95,7 +95,7 @@ class Ollama::Image
95
95
  # @param other [ Ollama::Image ] the other Image object to compare against
96
96
  #
97
97
  # @return [ TrueClass, FalseClass ] true if both Image objects have identical
98
- # data, false otherwise
98
+ # data, false otherwise
99
99
  def ==(other)
100
100
  @data == other.data
101
101
  end
@@ -154,7 +154,7 @@ class Ollama::Options
154
154
  # @param value [ Hash ] a hash containing the attribute names and their values
155
155
  #
156
156
  # @return [ self ] a new instance of the class initialized with the provided
157
- # attributes
157
+ # attributes
158
158
  def self.[](value)
159
159
  new(**value.to_h)
160
160
  end
@@ -34,7 +34,7 @@ class Ollama::Tool::Function::Parameters::Property
34
34
  # The enum attribute reader returns the enumeration values associated with the object.
35
35
  #
36
36
  # @return [ Array<String>, nil ] an array of valid string values that the
37
- # property can take, or nil if not set
37
+ # property can take, or nil if not set
38
38
  attr_reader :enum
39
39
 
40
40
  # The initialize method sets up a new Property instance with the specified
@@ -33,7 +33,7 @@ class Ollama::Tool::Function::Parameters
33
33
  # associated with the object.
34
34
  #
35
35
  # @return [ Array<String>, nil ] an array of required parameter names, or nil
36
- # if not set
36
+ # if not set
37
37
  attr_reader :required
38
38
 
39
39
  # The initialize method sets up a new Parameters instance with the specified
data/lib/ollama/tool.rb CHANGED
@@ -23,14 +23,14 @@ class Ollama::Tool
23
23
  # The type attribute reader returns the type associated with the tool.
24
24
  #
25
25
  # @return [ String ] the type of tool, typically 'function' for function
26
- # calling capabilities
26
+ # calling capabilities
27
27
  attr_reader :type
28
28
 
29
29
  # The function attribute reader returns the function definition associated
30
30
  # with the tool.
31
31
  #
32
32
  # @return [ Hash ] the function definition as a hash, containing details such
33
- # as the function's name, description, parameters, and required fields
33
+ # as the function's name, description, parameters, and required fields
34
34
  attr_reader :function
35
35
 
36
36
  # The initialize method sets up a new Tool instance with the specified type
@@ -38,7 +38,7 @@ class Ollama::Tool
38
38
  #
39
39
  # @param type [ String ] the type of tool being created
40
40
  # @param function [ Ollama::Tool::Function ] the function definition
41
- # associated with the tool
41
+ # associated with the tool
42
42
  def initialize(type:, function:)
43
43
  @type, @function = type, function.to_hash
44
44
  end
@@ -1,6 +1,6 @@
1
1
  module Ollama
2
2
  # Ollama version
3
- VERSION = '1.18.0'
3
+ VERSION = '1.19.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,9 +1,9 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: ollama-ruby 1.18.0 ruby lib
2
+ # stub: ollama-ruby 1.19.1 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "ollama-ruby".freeze
6
- s.version = "1.18.0".freeze
6
+ s.version = "1.19.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]
@@ -18,14 +18,14 @@ Gem::Specification.new do |s|
18
18
  s.licenses = ["MIT".freeze]
19
19
  s.rdoc_options = ["--title".freeze, "Ollama-ruby - Interacting with the Ollama API".freeze, "--main".freeze, "README.md".freeze]
20
20
  s.required_ruby_version = Gem::Requirement.new(">= 3.1".freeze)
21
- s.rubygems_version = "4.0.2".freeze
21
+ s.rubygems_version = "4.0.3".freeze
22
22
  s.summary = "Interacting with the Ollama API".freeze
23
23
  s.test_files = ["spec/ollama/client/doc_spec.rb".freeze, "spec/ollama/client_spec.rb".freeze, "spec/ollama/commands/chat_spec.rb".freeze, "spec/ollama/commands/copy_spec.rb".freeze, "spec/ollama/commands/create_spec.rb".freeze, "spec/ollama/commands/delete_spec.rb".freeze, "spec/ollama/commands/embed_spec.rb".freeze, "spec/ollama/commands/embeddings_spec.rb".freeze, "spec/ollama/commands/generate_spec.rb".freeze, "spec/ollama/commands/ps_spec.rb".freeze, "spec/ollama/commands/pull_spec.rb".freeze, "spec/ollama/commands/push_spec.rb".freeze, "spec/ollama/commands/show_spec.rb".freeze, "spec/ollama/commands/tags_spec.rb".freeze, "spec/ollama/commands/version_spec.rb".freeze, "spec/ollama/handlers/collector_spec.rb".freeze, "spec/ollama/handlers/dump_json_spec.rb".freeze, "spec/ollama/handlers/dump_yaml_spec.rb".freeze, "spec/ollama/handlers/markdown_spec.rb".freeze, "spec/ollama/handlers/nop_spec.rb".freeze, "spec/ollama/handlers/print_spec.rb".freeze, "spec/ollama/handlers/progress_spec.rb".freeze, "spec/ollama/handlers/say_spec.rb".freeze, "spec/ollama/handlers/single_spec.rb".freeze, "spec/ollama/image_spec.rb".freeze, "spec/ollama/message_spec.rb".freeze, "spec/ollama/options_spec.rb".freeze, "spec/ollama/tool_spec.rb".freeze, "spec/spec_helper.rb".freeze]
24
24
 
25
25
  s.specification_version = 4
26
26
 
27
- s.add_development_dependency(%q<gem_hadar>.freeze, ["~> 2.9".freeze])
28
- s.add_development_dependency(%q<all_images>.freeze, ["~> 0.6".freeze])
27
+ s.add_development_dependency(%q<gem_hadar>.freeze, [">= 2.17.0".freeze])
28
+ s.add_development_dependency(%q<all_images>.freeze, ["~> 0.12".freeze])
29
29
  s.add_development_dependency(%q<rspec>.freeze, ["~> 3.2".freeze])
30
30
  s.add_development_dependency(%q<kramdown>.freeze, ["~> 2.0".freeze])
31
31
  s.add_development_dependency(%q<webmock>.freeze, [">= 0".freeze])
@@ -21,6 +21,17 @@ describe Ollama::Client do
21
21
  expect(client.output).to be $stdout
22
22
  end
23
23
 
24
+ it 'can be instantiated with config and api_key' do
25
+ config = Ollama::Client::Config[base_url: base_url, api_key: 'the-secret']
26
+ client = described_class.configure_with(config).expose
27
+ expect(client).to be_a described_class
28
+ expect(client.base_url.to_s).to eq base_url
29
+ expect(client.api_key).to eq 'the-secret'
30
+ expect(client.headers).to include(
31
+ 'Authorization' => 'Bearer the-secret'
32
+ )
33
+ end
34
+
24
35
  it 'can be instantiated with config loaded from JSON' do
25
36
  config = Ollama::Client::Config.load_from_json(asset('client.json'))
26
37
  config.base_url = base_url
@@ -34,14 +45,11 @@ describe Ollama::Client do
34
45
  expect(client.instance_variable_get(:@read_timeout)).to eq 3_600
35
46
  end
36
47
 
37
- it 'can be configured via environment variable' do
38
- old_base_url = ENV['OLLAMA_URL']
48
+ it 'can be configured via environment variable', protect_env: true do
39
49
  ENV.delete('OLLAMA_URL')
40
50
  expect { described_class.new }.to raise_error(ArgumentError)
41
51
  ENV['OLLAMA_URL'] = base_url
42
52
  expect(described_class.new).to be_a described_class
43
- ensure
44
- ENV['OLLAMA_URL'] = old_base_url
45
53
  end
46
54
 
47
55
  it 'can disable ssl peer verification' do
data/spec/spec_helper.rb CHANGED
@@ -14,7 +14,38 @@ def asset(name)
14
14
  File.join(__dir__, 'assets', name)
15
15
  end
16
16
 
17
+ # A module that provides functionality for protecting environment variables
18
+ # during tests.
19
+ #
20
+ # This module ensures that environment variable changes made during test
21
+ # execution are automatically restored to their original values after the test
22
+ # completes. It is designed to prevent side effects between tests that modify
23
+ # environment variables, maintaining a clean testing environment.
24
+ module ProtectEnvVars
25
+ # The apply method creates a lambda that protects environment variables
26
+ # during test execution.
27
+ #
28
+ # @return [Proc] a lambda that wraps test execution with environment variable
29
+ # preservation
30
+ def self.apply
31
+ -> example do
32
+ if example.metadata[:protect_env]
33
+ begin
34
+ stored_env = ENV.to_h
35
+ example.run
36
+ ensure
37
+ ENV.replace(stored_env)
38
+ end
39
+ else
40
+ example.run
41
+ end
42
+ end
43
+ end
44
+ end
45
+
17
46
  RSpec.configure do |config|
47
+ config.around(&ProtectEnvVars.apply)
48
+
18
49
  config.before(:suite) do
19
50
  infobar.show = nil
20
51
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ollama-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.18.0
4
+ version: 1.19.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank
@@ -13,30 +13,30 @@ dependencies:
13
13
  name: gem_hadar
14
14
  requirement: !ruby/object:Gem::Requirement
15
15
  requirements:
16
- - - "~>"
16
+ - - ">="
17
17
  - !ruby/object:Gem::Version
18
- version: '2.9'
18
+ version: 2.17.0
19
19
  type: :development
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
- - - "~>"
23
+ - - ">="
24
24
  - !ruby/object:Gem::Version
25
- version: '2.9'
25
+ version: 2.17.0
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: all_images
28
28
  requirement: !ruby/object:Gem::Requirement
29
29
  requirements:
30
30
  - - "~>"
31
31
  - !ruby/object:Gem::Version
32
- version: '0.6'
32
+ version: '0.12'
33
33
  type: :development
34
34
  prerelease: false
35
35
  version_requirements: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '0.6'
39
+ version: '0.12'
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: rspec
42
42
  requirement: !ruby/object:Gem::Requirement
@@ -413,7 +413,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
413
413
  - !ruby/object:Gem::Version
414
414
  version: '0'
415
415
  requirements: []
416
- rubygems_version: 4.0.2
416
+ rubygems_version: 4.0.3
417
417
  specification_version: 4
418
418
  summary: Interacting with the Ollama API
419
419
  test_files: