ollama-ruby 1.11.0 → 1.12.0

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: 204e16b85ab8d61e7baee8fcfe42db2ece4b2898180a019419d7847a2cb2711f
4
- data.tar.gz: 6e7c9135a0047cdd945a6ce2781665c993f808d2c044aaf164c9c71ed1705410
3
+ metadata.gz: d979768964258e0f21d54b480b0afcd23c01ceefb75be3f828cb0440c45cd5d1
4
+ data.tar.gz: b07ae78174a0614b146cd3cc7e4aeefc60ab86bc307b1b967f40c04841cce557
5
5
  SHA512:
6
- metadata.gz: e38ed0cc5e945d5b7bfd3e41d325612d080dabaf544c2457c8270fe836ed223bd536cb58af4dd806fc6e233dc3e694c6d58620d3816721623de03533d19c7d21
7
- data.tar.gz: c14520ca5f1cd8ecf5317489e71b32575e687b107b5b6c030052333ea95e9fcc608079273703895f597b80a81ea195ffb7bc9899283d2b778e79bc851c9a117d
6
+ metadata.gz: 2251a33cfdd40e1c53ebd9edb06a71994c68ef5a27ed82b35d691525459930e771a10dc93a0c8cc665e93980949a5abb60379f274954cee78a3edfeb4d92183e
7
+ data.tar.gz: a956187b7c3cb96f9398ed3677e332251b21db0407c21468b04c540e9a1e925a3cb4a4ff7bfe2e946164d80a262e8cd6ce5219cb3bd51625bc39e8841fed2933
data/CHANGES.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Changes
2
2
 
3
+ ## 2025-11-30 v1.12.0
4
+
5
+ - `ollama_cli`
6
+ - Added `-i` flag to display Ollama server version using `ollama.version.version`
7
+ - Refactored model options handling to use `Ollama::Options.from_hash` or `Ollama::Options.new`
8
+ - Stored client configuration in `ollama` variable for reuse
9
+ - Added `-d` flag for debug mode in `ollama_cli` instead of using environment variable
10
+ - Included `Ollama::DTO` in `Client::Config` class for consistent behavior
11
+ - Improved documentation formatting in `dto.rb` file
12
+ - Added documentation that `think` can be "high", "medium", "low" instead of just `true`
13
+
3
14
  ## 2025-11-05 v1.11.0
4
15
 
5
16
  - Replaced `tabulo` gem with `terminal-table` **version 3.0** for table
data/bin/ollama_cli CHANGED
@@ -22,13 +22,13 @@
22
22
  # - OLLAMA_MODEL_OPTIONS: JSON model options
23
23
  # - OLLAMA_SYSTEM: Default system prompt
24
24
  # - OLLAMA_PROMPT: Default user prompt
25
- # - DEBUG: Set to 1 for verbose output
26
25
 
27
26
  require 'ollama'
28
27
  include Ollama
29
28
  require 'tins'
30
29
  include Tins::GO
31
30
  require 'tins/xt/secure_write'
31
+ require 'tins/xt/full'
32
32
  require 'json'
33
33
  require 'tmpdir'
34
34
 
@@ -124,29 +124,40 @@ def usage
124
124
  -H HANDLER the handler to use for the response, defaults to ChatStart
125
125
  -S use streaming for generation
126
126
  -T use thinking for generation
127
+ -d enable debug mode
128
+ -i display ollama server version information
127
129
  -h this help
128
130
 
129
131
  EOT
130
132
  exit 0
131
133
  end
132
134
 
133
- opts = go 'u:m:M:s:p:P:H:c:STh', defaults: { ?H => 'ChatStart' }
135
+ opts = go 'u:m:M:s:p:P:H:c:STdih', defaults: { ?H => 'ChatStart' }
134
136
 
135
137
  opts[?h] and usage
136
138
 
137
139
  base_url = opts[?u] || ENV['OLLAMA_URL'] || 'http://%s' % ENV.fetch('OLLAMA_HOST')
138
140
  client_config = Client::Config[
139
- { base_url: } |
141
+ { base_url:, debug: !!opts[?d] } |
140
142
  JSON(get_file_argument(opts[?c], default: ENV['OLLAMA_CLIENT']).full? || '{}')
141
143
  ]
142
- model = opts[?m] || ENV.fetch('OLLAMA_MODEL', 'llama3.1')
143
- options = Ollama::Options.from_hash(JSON(
144
- get_file_argument(opts[?M], default: ENV['OLLAMA_MODEL_OPTIONS'])
145
- ))
146
- system = get_file_argument(opts[?s], default: ENV['OLLAMA_SYSTEM'])
147
- prompt = get_file_argument(opts[?p], default: ENV['OLLAMA_PROMPT'])
148
-
149
- if prompt.nil?
144
+ model = (opts[?m] || ENV['OLLAMA_MODEL']).full? || 'llama3.1'
145
+ model_options = JSON(get_file_argument(opts[?M], default: ENV['OLLAMA_MODEL_OPTIONS']))
146
+ options = if model_options.is_a?(Hash)
147
+ Ollama::Options.from_hash(model_options)
148
+ else
149
+ Ollama::Options.new
150
+ end
151
+ system = get_file_argument(opts[?s], default: ENV['OLLAMA_SYSTEM'])
152
+ prompt = get_file_argument(opts[?p], default: ENV['OLLAMA_PROMPT'])
153
+
154
+ ollama = Client.configure_with(client_config)
155
+
156
+ case
157
+ when opts[?i]
158
+ puts ollama.version.version
159
+ exit
160
+ when prompt.nil?
150
161
  prompt = STDIN.read
151
162
  else
152
163
  vars = prompt.named_placeholders
@@ -163,13 +174,14 @@ else
163
174
  prompt = prompt.named_placeholders_interpolate(values, default:)
164
175
  end
165
176
 
166
- if ENV['DEBUG'].to_i == 1
177
+ if opts[?d]
167
178
  puts <<~EOT
168
- base_url = #{base_url.inspect}
169
- model = #{model.inspect}
170
- system = #{system.inspect}
171
- prompt = #{prompt.inspect}
172
- options = #{options.to_json}
179
+ base_url = #{base_url.inspect}
180
+ model = #{model.inspect}
181
+ system = #{system.inspect}
182
+ prompt = #{prompt.inspect}
183
+ options = #{options.to_json}
184
+ client_config = #{client_config.to_json}
173
185
  EOT
174
186
  end
175
187
 
@@ -183,7 +195,7 @@ handler = case
183
195
  handler
184
196
  end
185
197
 
186
- Client.configure_with(client_config).generate(
198
+ ollama.generate(
187
199
  model:,
188
200
  system:,
189
201
  prompt:,
data/bin/ollama_console CHANGED
@@ -32,17 +32,52 @@ def base_url
32
32
  ENV['OLLAMA_URL'] || 'http://%s' % ENV.fetch('OLLAMA_HOST')
33
33
  end
34
34
 
35
+ # The client_config method retrieves and merges client configuration settings.
36
+ #
37
+ # This method fetches the base URL from the instance variable and combines it
38
+ # with configuration data parsed from the OLLAMA_CLIENT environment variable.
39
+ # If the environment variable is not set, it defaults to an empty JSON object.
40
+ #
41
+ # @return [ Hash ] a hash containing the merged configuration settings
42
+ def client_config
43
+ Client::Config[ { base_url: } | JSON(ENV.fetch('OLLAMA_CLIENT', '{}')) ]
44
+ end
45
+
35
46
  # The ollama method provides access to a configured Ollama client instance.
36
47
  #
37
48
  # This method initializes and returns a singleton instance of the Ollama client,
38
49
  # using the base URL specified in the base_url parameter. If an instance already
39
50
  # exists, it returns the cached version instead of creating a new one.
40
51
  #
41
- # @param base_url [ String ] the base URL for the Ollama API endpoint
42
52
  # @return [ Ollama::Client ] a configured Ollama client instance
43
53
  def ollama
44
- $ollama ||= Client.new(base_url:)
54
+ $ollama ||= Client.configure_with(client_config)
45
55
  end
56
+
57
+ # The model method retrieves the Ollama model name from the environment
58
+ #
59
+ # This method returns the name of the Ollama model to be used for API requests,
60
+ # falling back to a default value of 'llama3.1' if the OLLAMA_MODEL environment
61
+ # variable is not set.
62
+ #
63
+ # @return [ String ] the name of the Ollama model to use, either from the
64
+ # OLLAMA_MODEL environment variable or the default 'llama3.1'
65
+ def model
66
+ ENV.fetch('OLLAMA_MODEL', 'llama3.1')
67
+ end
68
+
69
+ # The options method retrieves and parses model options from the environment.
70
+ #
71
+ # This method fetches the OLLAMA_MODEL_OPTIONS environment variable, parses it
72
+ # as JSON, and converts it into an Ollama::Options object for use with model
73
+ # commands.
74
+ #
75
+ # @return [ Ollama::Options ] an options object initialized with values from
76
+ # the environment
77
+ def options
78
+ Ollama::Options.from_hash(JSON(ENV.fetch('OLLAMA_MODEL_OPTIONS', '{}')))
79
+ end
80
+
46
81
  IRB.setup nil
47
82
  IRB.conf[:MAIN_CONTEXT] = IRB::Irb.new.context
48
83
  IRB.conf[:HISTORY_FILE] = File.join(ENV.fetch('HOME'), '.ollama_console-history')
@@ -17,8 +17,6 @@ require 'ollama/json_loader'
17
17
  # config = Ollama::Client::Config.load_from_json('path/to/config.json')
18
18
  # client = Ollama::Client.configure_with(config)
19
19
  module Ollama::Client::Configuration
20
-
21
-
22
20
  # A class that encapsulates configuration settings for Ollama clients.
23
21
  #
24
22
  # This class provides a structured way to define and manage various
@@ -37,6 +35,7 @@ module Ollama::Client::Configuration
37
35
  # @example Loading configuration from a JSON file
38
36
  # config = Ollama::Client::Config.load_from_json('path/to/config.json')
39
37
  class Config
38
+ include Ollama::DTO
40
39
  extend Ollama::JSONLoader
41
40
 
42
41
  # The initialize method sets up a new configuration instance with the
@@ -39,7 +39,8 @@ class Ollama::Commands::Chat
39
39
  # @param options [ Ollama::Options, nil ] configuration parameters for the model
40
40
  # @param stream [ TrueClass, FalseClass, nil ] whether to enable streaming for the operation
41
41
  # @param keep_alive [ String, nil ] duration to keep the model loaded in memory
42
- # @param think [ Boolean, nil ] whether to enable thinking mode for reasoning
42
+ # @param think [ Boolean, String, nil ] whether to enable thinking mode for
43
+ # generation. Can be "high", "medium", "low" instead of true
43
44
  def initialize(model:, messages:, tools: nil, format: nil, options: nil, stream: nil, keep_alive: nil, think: nil)
44
45
  @model, @messages, @tools, @format, @options, @stream, @keep_alive, @think =
45
46
  model, as_array_of_hashes(messages), as_array_of_hashes(tools),
@@ -85,7 +86,7 @@ class Ollama::Commands::Chat
85
86
 
86
87
  # The think attribute reader returns the thinking mode setting associated with the object.
87
88
  #
88
- # @return [ Boolean, nil ] whether thinking mode is enabled for reasoning
89
+ # @return [ Boolean, String, nil ] whether thinking mode is enabled for reasoning
89
90
  attr_reader :think
90
91
 
91
92
  # The client attribute writer allows setting the client instance associated
@@ -98,7 +99,6 @@ class Ollama::Commands::Chat
98
99
  # @attr_writer [ Ollama::Client ] the assigned client instance
99
100
  attr_writer :client
100
101
 
101
-
102
102
  # The perform method executes a command request using the specified handler.
103
103
  #
104
104
  # This method initiates a POST request to the Ollama API's chat endpoint,
@@ -42,7 +42,8 @@ class Ollama::Commands::Generate
42
42
  # @param stream [ Boolean, nil ] whether to stream responses (default: false)
43
43
  # @param raw [ Boolean, nil ] whether to return raw output without formatting
44
44
  # @param keep_alive [ String, nil ] duration to keep the model loaded in memory
45
- # @param think [ Boolean, nil ] whether to enable thinking mode for generation
45
+ # @param think [ Boolean, String, nil ] whether to enable thinking mode for
46
+ # generation. Can be "high", "medium", "low" instead of true
46
47
  def initialize(model:, prompt:, suffix: nil, images: nil, format: nil, options: nil, system: nil, template: nil, context: nil, stream: nil, raw: nil, keep_alive: nil, think: nil)
47
48
  @model, @prompt, @suffix, @images, @format, @options, @system, @template, @context, @stream, @raw, @keep_alive, @think =
48
49
  model, prompt, suffix, (Array(images) if images), format, options, system, template, context, stream, raw, keep_alive, think
@@ -110,7 +111,7 @@ class Ollama::Commands::Generate
110
111
 
111
112
  # The think attribute reader returns whether thinking mode is enabled for generation.
112
113
  #
113
- # @return [ Boolean, nil ] whether to enable thinking mode for generation
114
+ # @return [ Boolean, String, nil ] whether to enable thinking mode for generation
114
115
  attr_reader :think
115
116
 
116
117
  # The client attribute writer allows setting the client instance associated
@@ -130,7 +131,6 @@ class Ollama::Commands::Generate
130
131
  # method for actual HTTP communication.
131
132
  #
132
133
  # @param handler [ Ollama::Handler ] the handler to process responses from the API
133
- # @return [ void ]
134
134
  def perform(handler)
135
135
  @client.request(method: :post, path: self.class.path, body: to_json, stream:, handler:)
136
136
  end
data/lib/ollama/dto.rb CHANGED
@@ -47,11 +47,22 @@ module Ollama::DTO
47
47
  # attributes set.
48
48
  #
49
49
  # @param names [ Array<Symbol> ] one or more attribute names to be declared
50
- # as readable and registered
50
+ # as readable and registered
51
51
  def attr_reader(*names)
52
52
  super
53
53
  attributes.merge(names.map(&:to_sym))
54
54
  end
55
+
56
+ # The attr_accessor method extends the functionality of the standard
57
+ # attr_accessor by also registering the declared attributes in the class's
58
+ # attributes set.
59
+ #
60
+ # @param names [ Array<Symbol> ] one or more attribute names to be declared
61
+ # as readable and registered
62
+ def attr_accessor(*names)
63
+ super
64
+ attributes.merge(names.map(&:to_sym))
65
+ end
55
66
  end
56
67
 
57
68
  # The as_array_of_hashes method converts an object into an array of hashes.
@@ -63,7 +74,7 @@ module Ollama::DTO
63
74
  # @param obj [ Object ] the object to be converted
64
75
  #
65
76
  # @return [ Array<Hash>, nil ] an array of hashes if the conversion was
66
- # possible, or nil otherwise
77
+ # possible, or nil otherwise
67
78
  def as_array_of_hashes(obj)
68
79
  if obj.respond_to?(:to_hash)
69
80
  [ obj.to_hash ]
@@ -80,7 +91,7 @@ module Ollama::DTO
80
91
  # @param obj [ Object ] the object to be converted to a hash
81
92
  #
82
93
  # @return [ Hash, nil ] the hash representation of the object or nil if the
83
- # object does not respond to to_hash
94
+ # object does not respond to to_hash
84
95
  def as_hash(obj)
85
96
  obj&.to_hash
86
97
  end
@@ -94,7 +105,7 @@ module Ollama::DTO
94
105
  # @param obj [ Object ] the object to be converted to an array
95
106
  #
96
107
  # @return [ Array, nil ] an array containing the object or its elements, or
97
- # nil if the input is nil
108
+ # nil if the input is nil
98
109
  def as_array(obj)
99
110
  if obj.nil?
100
111
  obj
@@ -126,7 +137,7 @@ module Ollama::DTO
126
137
  # @param other [ Object ] the object to compare against
127
138
  #
128
139
  # @return [ TrueClass, FalseClass ] true if both objects have identical JSON
129
- # representations, false otherwise
140
+ # representations, false otherwise
130
141
  def ==(other)
131
142
  as_json == other.as_json
132
143
  end
@@ -140,7 +151,7 @@ module Ollama::DTO
140
151
  # if an object, such as a DTO, has been initialized with any values.
141
152
  #
142
153
  # @return [ TrueClass, FalseClass ] true if the object has no attributes,
143
- # false otherwise
154
+ # false otherwise
144
155
  def empty?
145
156
  to_hash.empty?
146
157
  end
@@ -1,6 +1,6 @@
1
1
  module Ollama
2
2
  # Ollama version
3
- VERSION = '1.11.0'
3
+ VERSION = '1.12.0'
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.11.0 ruby lib
2
+ # stub: ollama-ruby 1.12.0 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "ollama-ruby".freeze
6
- s.version = "1.11.0".freeze
6
+ s.version = "1.12.0".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]
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.11.0
4
+ version: 1.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank